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>
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 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
65 struct signal_frame __user
*sf
;
66 unsigned long up_psr
, pc
, npc
;
68 __siginfo_fpu_t __user
*fpu_save
;
69 __siginfo_rwin_t __user
*rwin_save
;
72 /* Always make any pending restarted system calls return -EINTR */
73 current
->restart_block
.fn
= do_no_restart_syscall
;
75 synchronize_user_stack();
77 sf
= (struct signal_frame __user
*) regs
->u_regs
[UREG_FP
];
79 /* 1. Make sure we are not getting garbage from the user */
80 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)))
83 if (((unsigned long) sf
) & 3)
86 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
87 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
92 /* 2. Restore the state */
94 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
96 /* User can only change condition codes and FPU enabling in %psr. */
97 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
98 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
100 /* Prevent syscall restart. */
101 pt_regs_clear_syscall(regs
);
103 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
105 err
|= restore_fpu_state(regs
, fpu_save
);
106 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
108 err
|= restore_rwin_state(rwin_save
);
110 /* This is pretty much atomic, no amount locking would prevent
111 * the races which exist anyways.
113 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
114 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
115 (_NSIG_WORDS
-1) * sizeof(unsigned int));
120 set_current_blocked(&set
);
124 force_sig(SIGSEGV
, current
);
127 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
129 struct rt_signal_frame __user
*sf
;
130 unsigned int psr
, pc
, npc
;
131 __siginfo_fpu_t __user
*fpu_save
;
132 __siginfo_rwin_t __user
*rwin_save
;
136 synchronize_user_stack();
137 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
138 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)) ||
139 (((unsigned long) sf
) & 0x03))
142 err
= __get_user(pc
, &sf
->regs
.pc
);
143 err
|= __get_user(npc
, &sf
->regs
.npc
);
144 err
|= ((pc
| npc
) & 0x03);
146 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
147 err
|= __get_user(psr
, &sf
->regs
.psr
);
149 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
150 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
152 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
154 /* Prevent syscall restart. */
155 pt_regs_clear_syscall(regs
);
157 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
158 if (!err
&& fpu_save
)
159 err
|= restore_fpu_state(regs
, fpu_save
);
160 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
161 err
|= restore_altstack(&sf
->stack
);
169 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
170 if (!err
&& rwin_save
) {
171 if (restore_rwin_state(rwin_save
))
175 set_current_blocked(&set
);
178 force_sig(SIGSEGV
, current
);
181 /* Checks if the fp is valid */
182 static inline int invalid_frame_pointer(void __user
*fp
, int fplen
)
184 if ((((unsigned long) fp
) & 7) || !__access_ok((unsigned long)fp
, fplen
))
190 static inline void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
, unsigned long framesize
)
192 unsigned long sp
= regs
->u_regs
[UREG_FP
];
195 * If we are on the alternate signal stack and would overflow it, don't.
196 * Return an always-bogus address instead so we will die with SIGSEGV.
198 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
199 return (void __user
*) -1L;
201 /* This is the X/Open sanctioned signal stack switching. */
202 sp
= sigsp(sp
, ksig
) - framesize
;
204 /* Always align the stack frame. This handles two cases. First,
205 * sigaltstack need not be mindful of platform specific stack
206 * alignment. Second, if we took this signal because the stack
207 * is not aligned properly, we'd like to take the signal cleanly
212 return (void __user
*) sp
;
215 static int setup_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
218 struct signal_frame __user
*sf
;
219 int sigframe_size
, err
, wsaved
;
222 /* 1. Make sure everything is clean */
223 synchronize_user_stack();
225 wsaved
= current_thread_info()->w_saved
;
227 sigframe_size
= sizeof(*sf
);
229 sigframe_size
+= sizeof(__siginfo_fpu_t
);
231 sigframe_size
+= sizeof(__siginfo_rwin_t
);
233 sf
= (struct signal_frame __user
*)
234 get_sigframe(ksig
, regs
, sigframe_size
);
236 if (invalid_frame_pointer(sf
, sigframe_size
)) {
243 /* 2. Save the current process state */
244 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
246 err
|= __put_user(0, &sf
->extra_size
);
249 __siginfo_fpu_t __user
*fp
= tail
;
251 err
|= save_fpu_state(regs
, fp
);
252 err
|= __put_user(fp
, &sf
->fpu_save
);
254 err
|= __put_user(0, &sf
->fpu_save
);
257 __siginfo_rwin_t __user
*rwp
= tail
;
258 tail
+= sizeof(*rwp
);
259 err
|= save_rwin_state(wsaved
, rwp
);
260 err
|= __put_user(rwp
, &sf
->rwin_save
);
262 err
|= __put_user(0, &sf
->rwin_save
);
265 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
266 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
267 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
269 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
270 sizeof(struct reg_window32
));
272 struct reg_window32
*rp
;
274 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
275 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
280 /* 3. signal handler back-trampoline and parameters */
281 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
282 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
283 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
284 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
286 /* 4. signal handler */
287 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
288 regs
->npc
= (regs
->pc
+ 4);
290 /* 5. return to kernel instructions */
291 if (ksig
->ka
.ka_restorer
)
292 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
294 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
296 /* mov __NR_sigreturn, %g1 */
297 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
300 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
304 /* Flush instruction space. */
305 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
310 static int setup_rt_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
313 struct rt_signal_frame __user
*sf
;
314 int sigframe_size
, wsaved
;
319 synchronize_user_stack();
320 wsaved
= current_thread_info()->w_saved
;
321 sigframe_size
= sizeof(*sf
);
323 sigframe_size
+= sizeof(__siginfo_fpu_t
);
325 sigframe_size
+= sizeof(__siginfo_rwin_t
);
326 sf
= (struct rt_signal_frame __user
*)
327 get_sigframe(ksig
, regs
, sigframe_size
);
328 if (invalid_frame_pointer(sf
, sigframe_size
)) {
334 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
335 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
336 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
340 err
|= __put_user(psr
, &sf
->regs
.psr
);
341 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
342 err
|= __put_user(0, &sf
->extra_size
);
345 __siginfo_fpu_t __user
*fp
= tail
;
347 err
|= save_fpu_state(regs
, fp
);
348 err
|= __put_user(fp
, &sf
->fpu_save
);
350 err
|= __put_user(0, &sf
->fpu_save
);
353 __siginfo_rwin_t __user
*rwp
= tail
;
354 tail
+= sizeof(*rwp
);
355 err
|= save_rwin_state(wsaved
, rwp
);
356 err
|= __put_user(rwp
, &sf
->rwin_save
);
358 err
|= __put_user(0, &sf
->rwin_save
);
360 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
362 /* Setup sigaltstack */
363 err
|= __save_altstack(&sf
->stack
, regs
->u_regs
[UREG_FP
]);
366 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
367 sizeof(struct reg_window32
));
369 struct reg_window32
*rp
;
371 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
372 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
375 err
|= copy_siginfo_to_user(&sf
->info
, &ksig
->info
);
380 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
381 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
382 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
383 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
385 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
386 regs
->npc
= (regs
->pc
+ 4);
388 if (ksig
->ka
.ka_restorer
)
389 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
391 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
393 /* mov __NR_sigreturn, %g1 */
394 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
397 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
401 /* Flush instruction space. */
402 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
408 handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
410 sigset_t
*oldset
= sigmask_to_save();
413 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
414 err
= setup_rt_frame(ksig
, regs
, oldset
);
416 err
= setup_frame(ksig
, regs
, oldset
);
417 signal_setup_done(err
, ksig
, 0);
420 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
421 struct sigaction
*sa
)
423 switch(regs
->u_regs
[UREG_I0
]) {
424 case ERESTART_RESTARTBLOCK
:
426 no_system_call_restart
:
427 regs
->u_regs
[UREG_I0
] = EINTR
;
431 if (!(sa
->sa_flags
& SA_RESTART
))
432 goto no_system_call_restart
;
435 regs
->u_regs
[UREG_I0
] = orig_i0
;
441 /* Note that 'init' is a special process: it doesn't get signals it doesn't
442 * want to handle. Thus you cannot kill init even with a SIGKILL even by
445 static void do_signal(struct pt_regs
*regs
, unsigned long orig_i0
)
451 /* It's a lot of work and synchronization to add a new ptrace
452 * register for GDB to save and restore in order to get
453 * orig_i0 correct for syscall restarts when debugging.
455 * Although it should be the case that most of the global
456 * registers are volatile across a system call, glibc already
457 * depends upon that fact that we preserve them. So we can't
458 * just use any global register to save away the orig_i0 value.
460 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
461 * preserved across a system call trap by various pieces of
464 * %g7 is used as the "thread register". %g6 is not used in
465 * any fixed manner. %g6 is used as a scratch register and
466 * a compiler temporary, but it's value is never used across
467 * a system call. Therefore %g6 is usable for orig_i0 storage.
469 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
470 regs
->u_regs
[UREG_G6
] = orig_i0
;
472 has_handler
= get_signal(&ksig
);
474 /* If the debugger messes with the program counter, it clears
475 * the software "in syscall" bit, directing us to not perform
479 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
)) {
481 orig_i0
= regs
->u_regs
[UREG_G6
];
486 syscall_restart(orig_i0
, regs
, &ksig
.ka
.sa
);
487 handle_signal(&ksig
, regs
);
489 if (restart_syscall
) {
490 switch (regs
->u_regs
[UREG_I0
]) {
494 /* replay the system call when we are done */
495 regs
->u_regs
[UREG_I0
] = orig_i0
;
498 pt_regs_clear_syscall(regs
);
499 case ERESTART_RESTARTBLOCK
:
500 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
503 pt_regs_clear_syscall(regs
);
506 restore_saved_sigmask();
510 void do_notify_resume(struct pt_regs
*regs
, unsigned long orig_i0
,
511 unsigned long thread_info_flags
)
513 if (thread_info_flags
& _TIF_SIGPENDING
)
514 do_signal(regs
, orig_i0
);
515 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
516 clear_thread_flag(TIF_NOTIFY_RESUME
);
517 tracehook_notify_resume(regs
);
521 asmlinkage
int do_sys_sigstack(struct sigstack __user
*ssptr
,
522 struct sigstack __user
*ossptr
,
527 /* First see if old state is wanted. */
529 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
530 &ossptr
->the_stack
) ||
531 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
535 /* Now see if we want to update the new state. */
539 if (get_user(ss_sp
, &ssptr
->the_stack
))
541 /* If the current stack was set with sigaltstack, don't
542 swap stacks while we are on it. */
544 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
547 /* Since we don't know the extent of the stack, and we don't
548 track onstack-ness, but rather calculate it, we must
549 presume a size. Ho hum this interface is lossy. */
550 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
551 current
->sas_ss_size
= SIGSTKSZ
;