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 */
28 #include <asm/switch_to.h>
32 extern void fpsave(unsigned long *fpregs
, unsigned long *fsr
,
33 void *fpqueue
, unsigned long *fpqdepth
);
34 extern void fpload(unsigned long *fpregs
, unsigned long *fsr
);
37 struct sparc_stackf ss
;
39 __siginfo_fpu_t __user
*fpu_save
;
40 unsigned long insns
[2] __attribute__ ((aligned (8)));
41 unsigned int extramask
[_NSIG_WORDS
- 1];
42 unsigned int extra_size
; /* Should be 0 */
43 __siginfo_rwin_t __user
*rwin_save
;
44 } __attribute__((aligned(8)));
46 struct rt_signal_frame
{
47 struct sparc_stackf ss
;
51 __siginfo_fpu_t __user
*fpu_save
;
52 unsigned int insns
[2];
54 unsigned int extra_size
; /* Should be 0 */
55 __siginfo_rwin_t __user
*rwin_save
;
56 } __attribute__((aligned(8)));
59 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
60 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
62 static int _sigpause_common(old_sigset_t set
)
65 siginitset(&blocked
, set
);
66 return sigsuspend(&blocked
);
69 asmlinkage
int sys_sigsuspend(old_sigset_t set
)
71 return _sigpause_common(set
);
74 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
76 struct signal_frame __user
*sf
;
77 unsigned long up_psr
, pc
, npc
;
79 __siginfo_fpu_t __user
*fpu_save
;
80 __siginfo_rwin_t __user
*rwin_save
;
83 /* Always make any pending restarted system calls return -EINTR */
84 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
86 synchronize_user_stack();
88 sf
= (struct signal_frame __user
*) regs
->u_regs
[UREG_FP
];
90 /* 1. Make sure we are not getting garbage from the user */
91 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)))
94 if (((unsigned long) sf
) & 3)
97 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
98 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
103 /* 2. Restore the state */
105 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
107 /* User can only change condition codes and FPU enabling in %psr. */
108 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
109 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
111 /* Prevent syscall restart. */
112 pt_regs_clear_syscall(regs
);
114 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
116 err
|= restore_fpu_state(regs
, fpu_save
);
117 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
119 err
|= restore_rwin_state(rwin_save
);
121 /* This is pretty much atomic, no amount locking would prevent
122 * the races which exist anyways.
124 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
125 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
126 (_NSIG_WORDS
-1) * sizeof(unsigned int));
131 set_current_blocked(&set
);
135 force_sig(SIGSEGV
, current
);
138 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
140 struct rt_signal_frame __user
*sf
;
141 unsigned int psr
, pc
, npc
;
142 __siginfo_fpu_t __user
*fpu_save
;
143 __siginfo_rwin_t __user
*rwin_save
;
149 synchronize_user_stack();
150 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
151 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)) ||
152 (((unsigned long) sf
) & 0x03))
155 err
= __get_user(pc
, &sf
->regs
.pc
);
156 err
|= __get_user(npc
, &sf
->regs
.npc
);
157 err
|= ((pc
| npc
) & 0x03);
159 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
160 err
|= __get_user(psr
, &sf
->regs
.psr
);
162 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
163 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
165 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
167 /* Prevent syscall restart. */
168 pt_regs_clear_syscall(regs
);
170 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
171 if (!err
&& fpu_save
)
172 err
|= restore_fpu_state(regs
, fpu_save
);
173 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
175 err
|= __copy_from_user(&st
, &sf
->stack
, sizeof(stack_t
));
183 /* It is more difficult to avoid calling this function than to
184 * call it and ignore errors.
188 do_sigaltstack((const stack_t __user
*) &st
, NULL
, (unsigned long)sf
);
191 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
192 if (!err
&& rwin_save
) {
193 if (restore_rwin_state(rwin_save
))
197 set_current_blocked(&set
);
200 force_sig(SIGSEGV
, current
);
203 /* Checks if the fp is valid */
204 static inline int invalid_frame_pointer(void __user
*fp
, int fplen
)
206 if ((((unsigned long) fp
) & 7) || !__access_ok((unsigned long)fp
, fplen
))
212 static inline void __user
*get_sigframe(struct sigaction
*sa
, struct pt_regs
*regs
, unsigned long framesize
)
214 unsigned long sp
= regs
->u_regs
[UREG_FP
];
217 * If we are on the alternate signal stack and would overflow it, don't.
218 * Return an always-bogus address instead so we will die with SIGSEGV.
220 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
221 return (void __user
*) -1L;
223 /* This is the X/Open sanctioned signal stack switching. */
224 if (sa
->sa_flags
& SA_ONSTACK
) {
225 if (sas_ss_flags(sp
) == 0)
226 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
231 /* Always align the stack frame. This handles two cases. First,
232 * sigaltstack need not be mindful of platform specific stack
233 * alignment. Second, if we took this signal because the stack
234 * is not aligned properly, we'd like to take the signal cleanly
239 return (void __user
*) sp
;
242 static int setup_frame(struct k_sigaction
*ka
, struct pt_regs
*regs
,
243 int signo
, sigset_t
*oldset
)
245 struct signal_frame __user
*sf
;
246 int sigframe_size
, err
, wsaved
;
249 /* 1. Make sure everything is clean */
250 synchronize_user_stack();
252 wsaved
= current_thread_info()->w_saved
;
254 sigframe_size
= sizeof(*sf
);
256 sigframe_size
+= sizeof(__siginfo_fpu_t
);
258 sigframe_size
+= sizeof(__siginfo_rwin_t
);
260 sf
= (struct signal_frame __user
*)
261 get_sigframe(&ka
->sa
, regs
, sigframe_size
);
263 if (invalid_frame_pointer(sf
, sigframe_size
))
264 goto sigill_and_return
;
268 /* 2. Save the current process state */
269 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
271 err
|= __put_user(0, &sf
->extra_size
);
274 __siginfo_fpu_t __user
*fp
= tail
;
276 err
|= save_fpu_state(regs
, fp
);
277 err
|= __put_user(fp
, &sf
->fpu_save
);
279 err
|= __put_user(0, &sf
->fpu_save
);
282 __siginfo_rwin_t __user
*rwp
= tail
;
283 tail
+= sizeof(*rwp
);
284 err
|= save_rwin_state(wsaved
, rwp
);
285 err
|= __put_user(rwp
, &sf
->rwin_save
);
287 err
|= __put_user(0, &sf
->rwin_save
);
290 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
291 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
292 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
294 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
295 sizeof(struct reg_window32
));
297 struct reg_window32
*rp
;
299 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
300 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
305 /* 3. signal handler back-trampoline and parameters */
306 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
307 regs
->u_regs
[UREG_I0
] = signo
;
308 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
309 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
311 /* 4. signal handler */
312 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
313 regs
->npc
= (regs
->pc
+ 4);
315 /* 5. return to kernel instructions */
317 regs
->u_regs
[UREG_I7
] = (unsigned long)ka
->ka_restorer
;
319 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
321 /* mov __NR_sigreturn, %g1 */
322 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
325 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
329 /* Flush instruction space. */
330 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
339 force_sigsegv(signo
, current
);
343 static int setup_rt_frame(struct k_sigaction
*ka
, struct pt_regs
*regs
,
344 int signo
, sigset_t
*oldset
, siginfo_t
*info
)
346 struct rt_signal_frame __user
*sf
;
347 int sigframe_size
, wsaved
;
352 synchronize_user_stack();
353 wsaved
= current_thread_info()->w_saved
;
354 sigframe_size
= sizeof(*sf
);
356 sigframe_size
+= sizeof(__siginfo_fpu_t
);
358 sigframe_size
+= sizeof(__siginfo_rwin_t
);
359 sf
= (struct rt_signal_frame __user
*)
360 get_sigframe(&ka
->sa
, regs
, sigframe_size
);
361 if (invalid_frame_pointer(sf
, sigframe_size
))
365 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
366 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
367 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
371 err
|= __put_user(psr
, &sf
->regs
.psr
);
372 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
373 err
|= __put_user(0, &sf
->extra_size
);
376 __siginfo_fpu_t
*fp
= tail
;
378 err
|= save_fpu_state(regs
, fp
);
379 err
|= __put_user(fp
, &sf
->fpu_save
);
381 err
|= __put_user(0, &sf
->fpu_save
);
384 __siginfo_rwin_t
*rwp
= tail
;
385 tail
+= sizeof(*rwp
);
386 err
|= save_rwin_state(wsaved
, rwp
);
387 err
|= __put_user(rwp
, &sf
->rwin_save
);
389 err
|= __put_user(0, &sf
->rwin_save
);
391 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
393 /* Setup sigaltstack */
394 err
|= __put_user(current
->sas_ss_sp
, &sf
->stack
.ss_sp
);
395 err
|= __put_user(sas_ss_flags(regs
->u_regs
[UREG_FP
]), &sf
->stack
.ss_flags
);
396 err
|= __put_user(current
->sas_ss_size
, &sf
->stack
.ss_size
);
399 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
400 sizeof(struct reg_window32
));
402 struct reg_window32
*rp
;
404 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
405 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
408 err
|= copy_siginfo_to_user(&sf
->info
, info
);
413 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
414 regs
->u_regs
[UREG_I0
] = signo
;
415 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
416 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
418 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
419 regs
->npc
= (regs
->pc
+ 4);
422 regs
->u_regs
[UREG_I7
] = (unsigned long)ka
->ka_restorer
;
424 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
426 /* mov __NR_sigreturn, %g1 */
427 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
430 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
434 /* Flush instruction space. */
435 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
444 force_sigsegv(signo
, current
);
449 handle_signal(unsigned long signr
, struct k_sigaction
*ka
,
450 siginfo_t
*info
, struct pt_regs
*regs
)
452 sigset_t
*oldset
= sigmask_to_save();
455 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
456 err
= setup_rt_frame(ka
, regs
, signr
, oldset
, info
);
458 err
= setup_frame(ka
, regs
, signr
, oldset
);
463 signal_delivered(signr
, info
, ka
, regs
, 0);
466 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
467 struct sigaction
*sa
)
469 switch(regs
->u_regs
[UREG_I0
]) {
470 case ERESTART_RESTARTBLOCK
:
472 no_system_call_restart
:
473 regs
->u_regs
[UREG_I0
] = EINTR
;
477 if (!(sa
->sa_flags
& SA_RESTART
))
478 goto no_system_call_restart
;
481 regs
->u_regs
[UREG_I0
] = orig_i0
;
487 /* Note that 'init' is a special process: it doesn't get signals it doesn't
488 * want to handle. Thus you cannot kill init even with a SIGKILL even by
491 static void do_signal(struct pt_regs
*regs
, unsigned long orig_i0
)
493 struct k_sigaction ka
;
498 /* It's a lot of work and synchronization to add a new ptrace
499 * register for GDB to save and restore in order to get
500 * orig_i0 correct for syscall restarts when debugging.
502 * Although it should be the case that most of the global
503 * registers are volatile across a system call, glibc already
504 * depends upon that fact that we preserve them. So we can't
505 * just use any global register to save away the orig_i0 value.
507 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
508 * preserved across a system call trap by various pieces of
511 * %g7 is used as the "thread register". %g6 is not used in
512 * any fixed manner. %g6 is used as a scratch register and
513 * a compiler temporary, but it's value is never used across
514 * a system call. Therefore %g6 is usable for orig_i0 storage.
516 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
517 regs
->u_regs
[UREG_G6
] = orig_i0
;
519 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
521 /* If the debugger messes with the program counter, it clears
522 * the software "in syscall" bit, directing us to not perform
526 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
)) {
528 orig_i0
= regs
->u_regs
[UREG_G6
];
534 syscall_restart(orig_i0
, regs
, &ka
.sa
);
535 handle_signal(signr
, &ka
, &info
, regs
);
538 if (restart_syscall
&&
539 (regs
->u_regs
[UREG_I0
] == ERESTARTNOHAND
||
540 regs
->u_regs
[UREG_I0
] == ERESTARTSYS
||
541 regs
->u_regs
[UREG_I0
] == ERESTARTNOINTR
)) {
542 /* replay the system call when we are done */
543 regs
->u_regs
[UREG_I0
] = orig_i0
;
546 pt_regs_clear_syscall(regs
);
548 if (restart_syscall
&&
549 regs
->u_regs
[UREG_I0
] == ERESTART_RESTARTBLOCK
) {
550 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
553 pt_regs_clear_syscall(regs
);
556 /* if there's no signal to deliver, we just put the saved sigmask
559 restore_saved_sigmask();
562 void do_notify_resume(struct pt_regs
*regs
, unsigned long orig_i0
,
563 unsigned long thread_info_flags
)
565 if (thread_info_flags
& _TIF_SIGPENDING
)
566 do_signal(regs
, orig_i0
);
567 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
568 clear_thread_flag(TIF_NOTIFY_RESUME
);
569 tracehook_notify_resume(regs
);
574 do_sys_sigstack(struct sigstack __user
*ssptr
, struct sigstack __user
*ossptr
,
579 /* First see if old state is wanted. */
581 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
582 &ossptr
->the_stack
) ||
583 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
587 /* Now see if we want to update the new state. */
591 if (get_user(ss_sp
, &ssptr
->the_stack
))
593 /* If the current stack was set with sigaltstack, don't
594 swap stacks while we are on it. */
596 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
599 /* Since we don't know the extent of the stack, and we don't
600 track onstack-ness, but rather calculate it, we must
601 presume a size. Ho hum this interface is lossy. */
602 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
603 current
->sas_ss_size
= SIGSTKSZ
;