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 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
64 struct signal_frame __user
*sf
;
65 unsigned long up_psr
, pc
, npc
;
67 __siginfo_fpu_t __user
*fpu_save
;
68 __siginfo_rwin_t __user
*rwin_save
;
71 /* Always make any pending restarted system calls return -EINTR */
72 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
74 synchronize_user_stack();
76 sf
= (struct signal_frame __user
*) regs
->u_regs
[UREG_FP
];
78 /* 1. Make sure we are not getting garbage from the user */
79 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)))
82 if (((unsigned long) sf
) & 3)
85 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
86 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
91 /* 2. Restore the state */
93 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
95 /* User can only change condition codes and FPU enabling in %psr. */
96 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
97 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
99 /* Prevent syscall restart. */
100 pt_regs_clear_syscall(regs
);
102 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
104 err
|= restore_fpu_state(regs
, fpu_save
);
105 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
107 err
|= restore_rwin_state(rwin_save
);
109 /* This is pretty much atomic, no amount locking would prevent
110 * the races which exist anyways.
112 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
113 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
114 (_NSIG_WORDS
-1) * sizeof(unsigned int));
119 set_current_blocked(&set
);
123 force_sig(SIGSEGV
, current
);
126 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
128 struct rt_signal_frame __user
*sf
;
129 unsigned int psr
, pc
, npc
;
130 __siginfo_fpu_t __user
*fpu_save
;
131 __siginfo_rwin_t __user
*rwin_save
;
135 synchronize_user_stack();
136 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
137 if (!access_ok(VERIFY_READ
, sf
, sizeof(*sf
)) ||
138 (((unsigned long) sf
) & 0x03))
141 err
= __get_user(pc
, &sf
->regs
.pc
);
142 err
|= __get_user(npc
, &sf
->regs
.npc
);
143 err
|= ((pc
| npc
) & 0x03);
145 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
146 err
|= __get_user(psr
, &sf
->regs
.psr
);
148 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
149 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
151 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
153 /* Prevent syscall restart. */
154 pt_regs_clear_syscall(regs
);
156 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
157 if (!err
&& fpu_save
)
158 err
|= restore_fpu_state(regs
, fpu_save
);
159 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
160 err
|= restore_altstack(&sf
->stack
);
168 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
169 if (!err
&& rwin_save
) {
170 if (restore_rwin_state(rwin_save
))
174 set_current_blocked(&set
);
177 force_sig(SIGSEGV
, current
);
180 /* Checks if the fp is valid */
181 static inline int invalid_frame_pointer(void __user
*fp
, int fplen
)
183 if ((((unsigned long) fp
) & 7) || !__access_ok((unsigned long)fp
, fplen
))
189 static inline void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
, unsigned long framesize
)
191 unsigned long sp
= regs
->u_regs
[UREG_FP
];
194 * If we are on the alternate signal stack and would overflow it, don't.
195 * Return an always-bogus address instead so we will die with SIGSEGV.
197 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
198 return (void __user
*) -1L;
200 /* This is the X/Open sanctioned signal stack switching. */
201 sp
= sigsp(sp
, ksig
) - framesize
;
203 /* Always align the stack frame. This handles two cases. First,
204 * sigaltstack need not be mindful of platform specific stack
205 * alignment. Second, if we took this signal because the stack
206 * is not aligned properly, we'd like to take the signal cleanly
211 return (void __user
*) sp
;
214 static int setup_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
217 struct signal_frame __user
*sf
;
218 int sigframe_size
, err
, wsaved
;
221 /* 1. Make sure everything is clean */
222 synchronize_user_stack();
224 wsaved
= current_thread_info()->w_saved
;
226 sigframe_size
= sizeof(*sf
);
228 sigframe_size
+= sizeof(__siginfo_fpu_t
);
230 sigframe_size
+= sizeof(__siginfo_rwin_t
);
232 sf
= (struct signal_frame __user
*)
233 get_sigframe(ksig
, regs
, sigframe_size
);
235 if (invalid_frame_pointer(sf
, sigframe_size
)) {
242 /* 2. Save the current process state */
243 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
245 err
|= __put_user(0, &sf
->extra_size
);
248 __siginfo_fpu_t __user
*fp
= tail
;
250 err
|= save_fpu_state(regs
, fp
);
251 err
|= __put_user(fp
, &sf
->fpu_save
);
253 err
|= __put_user(0, &sf
->fpu_save
);
256 __siginfo_rwin_t __user
*rwp
= tail
;
257 tail
+= sizeof(*rwp
);
258 err
|= save_rwin_state(wsaved
, rwp
);
259 err
|= __put_user(rwp
, &sf
->rwin_save
);
261 err
|= __put_user(0, &sf
->rwin_save
);
264 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
265 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
266 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
268 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
269 sizeof(struct reg_window32
));
271 struct reg_window32
*rp
;
273 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
274 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
279 /* 3. signal handler back-trampoline and parameters */
280 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
281 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
282 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
283 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
285 /* 4. signal handler */
286 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
287 regs
->npc
= (regs
->pc
+ 4);
289 /* 5. return to kernel instructions */
290 if (ksig
->ka
.ka_restorer
)
291 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
293 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
295 /* mov __NR_sigreturn, %g1 */
296 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
299 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
303 /* Flush instruction space. */
304 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
309 static int setup_rt_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
312 struct rt_signal_frame __user
*sf
;
313 int sigframe_size
, wsaved
;
318 synchronize_user_stack();
319 wsaved
= current_thread_info()->w_saved
;
320 sigframe_size
= sizeof(*sf
);
322 sigframe_size
+= sizeof(__siginfo_fpu_t
);
324 sigframe_size
+= sizeof(__siginfo_rwin_t
);
325 sf
= (struct rt_signal_frame __user
*)
326 get_sigframe(ksig
, regs
, sigframe_size
);
327 if (invalid_frame_pointer(sf
, sigframe_size
)) {
333 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
334 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
335 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
339 err
|= __put_user(psr
, &sf
->regs
.psr
);
340 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
341 err
|= __put_user(0, &sf
->extra_size
);
344 __siginfo_fpu_t
*fp
= tail
;
346 err
|= save_fpu_state(regs
, fp
);
347 err
|= __put_user(fp
, &sf
->fpu_save
);
349 err
|= __put_user(0, &sf
->fpu_save
);
352 __siginfo_rwin_t
*rwp
= tail
;
353 tail
+= sizeof(*rwp
);
354 err
|= save_rwin_state(wsaved
, rwp
);
355 err
|= __put_user(rwp
, &sf
->rwin_save
);
357 err
|= __put_user(0, &sf
->rwin_save
);
359 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
361 /* Setup sigaltstack */
362 err
|= __save_altstack(&sf
->stack
, regs
->u_regs
[UREG_FP
]);
365 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
366 sizeof(struct reg_window32
));
368 struct reg_window32
*rp
;
370 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
371 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
374 err
|= copy_siginfo_to_user(&sf
->info
, &ksig
->info
);
379 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
380 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
381 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
382 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
384 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
385 regs
->npc
= (regs
->pc
+ 4);
387 if (ksig
->ka
.ka_restorer
)
388 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
390 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
392 /* mov __NR_sigreturn, %g1 */
393 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
396 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
400 /* Flush instruction space. */
401 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
407 handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
409 sigset_t
*oldset
= sigmask_to_save();
412 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
413 err
= setup_rt_frame(ksig
, regs
, oldset
);
415 err
= setup_frame(ksig
, regs
, oldset
);
416 signal_setup_done(err
, ksig
, 0);
419 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
420 struct sigaction
*sa
)
422 switch(regs
->u_regs
[UREG_I0
]) {
423 case ERESTART_RESTARTBLOCK
:
425 no_system_call_restart
:
426 regs
->u_regs
[UREG_I0
] = EINTR
;
430 if (!(sa
->sa_flags
& SA_RESTART
))
431 goto no_system_call_restart
;
434 regs
->u_regs
[UREG_I0
] = orig_i0
;
440 /* Note that 'init' is a special process: it doesn't get signals it doesn't
441 * want to handle. Thus you cannot kill init even with a SIGKILL even by
444 static void do_signal(struct pt_regs
*regs
, unsigned long orig_i0
)
450 /* It's a lot of work and synchronization to add a new ptrace
451 * register for GDB to save and restore in order to get
452 * orig_i0 correct for syscall restarts when debugging.
454 * Although it should be the case that most of the global
455 * registers are volatile across a system call, glibc already
456 * depends upon that fact that we preserve them. So we can't
457 * just use any global register to save away the orig_i0 value.
459 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
460 * preserved across a system call trap by various pieces of
463 * %g7 is used as the "thread register". %g6 is not used in
464 * any fixed manner. %g6 is used as a scratch register and
465 * a compiler temporary, but it's value is never used across
466 * a system call. Therefore %g6 is usable for orig_i0 storage.
468 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
469 regs
->u_regs
[UREG_G6
] = orig_i0
;
471 has_handler
= get_signal(&ksig
);
473 /* If the debugger messes with the program counter, it clears
474 * the software "in syscall" bit, directing us to not perform
478 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
)) {
480 orig_i0
= regs
->u_regs
[UREG_G6
];
485 syscall_restart(orig_i0
, regs
, &ksig
.ka
.sa
);
486 handle_signal(&ksig
, regs
);
488 if (restart_syscall
) {
489 switch (regs
->u_regs
[UREG_I0
]) {
493 /* replay the system call when we are done */
494 regs
->u_regs
[UREG_I0
] = orig_i0
;
497 pt_regs_clear_syscall(regs
);
498 case ERESTART_RESTARTBLOCK
:
499 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
502 pt_regs_clear_syscall(regs
);
505 restore_saved_sigmask();
509 void do_notify_resume(struct pt_regs
*regs
, unsigned long orig_i0
,
510 unsigned long thread_info_flags
)
512 if (thread_info_flags
& _TIF_SIGPENDING
)
513 do_signal(regs
, orig_i0
);
514 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
515 clear_thread_flag(TIF_NOTIFY_RESUME
);
516 tracehook_notify_resume(regs
);
521 do_sys_sigstack(struct sigstack __user
*ssptr
, struct sigstack __user
*ossptr
,
526 /* First see if old state is wanted. */
528 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
529 &ossptr
->the_stack
) ||
530 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
534 /* Now see if we want to update the new state. */
538 if (get_user(ss_sp
, &ssptr
->the_stack
))
540 /* If the current stack was set with sigaltstack, don't
541 swap stacks while we are on it. */
543 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
546 /* Since we don't know the extent of the stack, and we don't
547 track onstack-ness, but rather calculate it, we must
548 presume a size. Ho hum this interface is lossy. */
549 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
550 current
->sas_ss_size
= SIGSTKSZ
;