1 // SPDX-License-Identifier: GPL-2.0
2 /* linux/arch/sparc/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
7 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/signal.h>
13 #include <linux/errno.h>
14 #include <linux/wait.h>
15 #include <linux/ptrace.h>
16 #include <linux/unistd.h>
18 #include <linux/tty.h>
19 #include <linux/smp.h>
20 #include <linux/binfmts.h> /* do_coredum */
21 #include <linux/bitops.h>
22 #include <linux/tracehook.h>
24 #include <linux/uaccess.h>
25 #include <asm/ptrace.h>
26 #include <asm/pgalloc.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 /* Checks if the fp is valid. We always build signal frames which are
64 * 16-byte aligned, therefore we can always enforce that the restore
65 * frame has that property as well.
67 static inline bool invalid_frame_pointer(void __user
*fp
, int fplen
)
69 if ((((unsigned long) fp
) & 15) || !__access_ok((unsigned long)fp
, fplen
))
75 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
77 unsigned long up_psr
, pc
, npc
, ufp
;
78 struct signal_frame __user
*sf
;
80 __siginfo_fpu_t __user
*fpu_save
;
81 __siginfo_rwin_t __user
*rwin_save
;
84 /* Always make any pending restarted system calls return -EINTR */
85 current
->restart_block
.fn
= do_no_restart_syscall
;
87 synchronize_user_stack();
89 sf
= (struct signal_frame __user
*) regs
->u_regs
[UREG_FP
];
91 /* 1. Make sure we are not getting garbage from the user */
92 if (invalid_frame_pointer(sf
, sizeof(*sf
)))
95 if (get_user(ufp
, &sf
->info
.si_regs
.u_regs
[UREG_FP
]))
101 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
102 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
107 /* 2. Restore the state */
109 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
111 /* User can only change condition codes and FPU enabling in %psr. */
112 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
113 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
115 /* Prevent syscall restart. */
116 pt_regs_clear_syscall(regs
);
118 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
120 err
|= restore_fpu_state(regs
, fpu_save
);
121 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
123 err
|= restore_rwin_state(rwin_save
);
125 /* This is pretty much atomic, no amount locking would prevent
126 * the races which exist anyways.
128 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
129 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
130 (_NSIG_WORDS
-1) * sizeof(unsigned int));
135 set_current_blocked(&set
);
142 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
144 struct rt_signal_frame __user
*sf
;
145 unsigned int psr
, pc
, npc
, ufp
;
146 __siginfo_fpu_t __user
*fpu_save
;
147 __siginfo_rwin_t __user
*rwin_save
;
151 synchronize_user_stack();
152 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
153 if (invalid_frame_pointer(sf
, sizeof(*sf
)))
156 if (get_user(ufp
, &sf
->regs
.u_regs
[UREG_FP
]))
162 err
= __get_user(pc
, &sf
->regs
.pc
);
163 err
|= __get_user(npc
, &sf
->regs
.npc
);
164 err
|= ((pc
| npc
) & 0x03);
166 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
167 err
|= __get_user(psr
, &sf
->regs
.psr
);
169 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
170 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
172 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
174 /* Prevent syscall restart. */
175 pt_regs_clear_syscall(regs
);
177 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
178 if (!err
&& fpu_save
)
179 err
|= restore_fpu_state(regs
, fpu_save
);
180 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
181 err
|= restore_altstack(&sf
->stack
);
189 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
190 if (!err
&& rwin_save
) {
191 if (restore_rwin_state(rwin_save
))
195 set_current_blocked(&set
);
201 static inline void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
, unsigned long framesize
)
203 unsigned long sp
= regs
->u_regs
[UREG_FP
];
206 * If we are on the alternate signal stack and would overflow it, don't.
207 * Return an always-bogus address instead so we will die with SIGSEGV.
209 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
210 return (void __user
*) -1L;
212 /* This is the X/Open sanctioned signal stack switching. */
213 sp
= sigsp(sp
, ksig
) - framesize
;
215 /* Always align the stack frame. This handles two cases. First,
216 * sigaltstack need not be mindful of platform specific stack
217 * alignment. Second, if we took this signal because the stack
218 * is not aligned properly, we'd like to take the signal cleanly
223 return (void __user
*) sp
;
226 static int setup_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
229 struct signal_frame __user
*sf
;
230 int sigframe_size
, err
, wsaved
;
233 /* 1. Make sure everything is clean */
234 synchronize_user_stack();
236 wsaved
= current_thread_info()->w_saved
;
238 sigframe_size
= sizeof(*sf
);
240 sigframe_size
+= sizeof(__siginfo_fpu_t
);
242 sigframe_size
+= sizeof(__siginfo_rwin_t
);
244 sf
= (struct signal_frame __user
*)
245 get_sigframe(ksig
, regs
, sigframe_size
);
247 if (invalid_frame_pointer(sf
, sigframe_size
)) {
254 /* 2. Save the current process state */
255 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
257 err
|= __put_user(0, &sf
->extra_size
);
260 __siginfo_fpu_t __user
*fp
= tail
;
262 err
|= save_fpu_state(regs
, fp
);
263 err
|= __put_user(fp
, &sf
->fpu_save
);
265 err
|= __put_user(0, &sf
->fpu_save
);
268 __siginfo_rwin_t __user
*rwp
= tail
;
269 tail
+= sizeof(*rwp
);
270 err
|= save_rwin_state(wsaved
, rwp
);
271 err
|= __put_user(rwp
, &sf
->rwin_save
);
273 err
|= __put_user(0, &sf
->rwin_save
);
276 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
277 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
278 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
280 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
281 sizeof(struct reg_window32
));
283 struct reg_window32
*rp
;
285 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
286 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
291 /* 3. signal handler back-trampoline and parameters */
292 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
293 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
294 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
295 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
297 /* 4. signal handler */
298 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
299 regs
->npc
= (regs
->pc
+ 4);
301 /* 5. return to kernel instructions */
302 if (ksig
->ka
.ka_restorer
)
303 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
305 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
307 /* mov __NR_sigreturn, %g1 */
308 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
311 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
315 /* Flush instruction space. */
316 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
321 static int setup_rt_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
324 struct rt_signal_frame __user
*sf
;
325 int sigframe_size
, wsaved
;
330 synchronize_user_stack();
331 wsaved
= current_thread_info()->w_saved
;
332 sigframe_size
= sizeof(*sf
);
334 sigframe_size
+= sizeof(__siginfo_fpu_t
);
336 sigframe_size
+= sizeof(__siginfo_rwin_t
);
337 sf
= (struct rt_signal_frame __user
*)
338 get_sigframe(ksig
, regs
, sigframe_size
);
339 if (invalid_frame_pointer(sf
, sigframe_size
)) {
345 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
346 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
347 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
351 err
|= __put_user(psr
, &sf
->regs
.psr
);
352 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
353 err
|= __put_user(0, &sf
->extra_size
);
356 __siginfo_fpu_t __user
*fp
= tail
;
358 err
|= save_fpu_state(regs
, fp
);
359 err
|= __put_user(fp
, &sf
->fpu_save
);
361 err
|= __put_user(0, &sf
->fpu_save
);
364 __siginfo_rwin_t __user
*rwp
= tail
;
365 tail
+= sizeof(*rwp
);
366 err
|= save_rwin_state(wsaved
, rwp
);
367 err
|= __put_user(rwp
, &sf
->rwin_save
);
369 err
|= __put_user(0, &sf
->rwin_save
);
371 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
373 /* Setup sigaltstack */
374 err
|= __save_altstack(&sf
->stack
, regs
->u_regs
[UREG_FP
]);
377 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
378 sizeof(struct reg_window32
));
380 struct reg_window32
*rp
;
382 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
383 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
386 err
|= copy_siginfo_to_user(&sf
->info
, &ksig
->info
);
391 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
392 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
393 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
394 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
396 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
397 regs
->npc
= (regs
->pc
+ 4);
399 if (ksig
->ka
.ka_restorer
)
400 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
402 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
404 /* mov __NR_sigreturn, %g1 */
405 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
408 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
412 /* Flush instruction space. */
413 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
419 handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
421 sigset_t
*oldset
= sigmask_to_save();
424 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
425 err
= setup_rt_frame(ksig
, regs
, oldset
);
427 err
= setup_frame(ksig
, regs
, oldset
);
428 signal_setup_done(err
, ksig
, 0);
431 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
432 struct sigaction
*sa
)
434 switch(regs
->u_regs
[UREG_I0
]) {
435 case ERESTART_RESTARTBLOCK
:
437 no_system_call_restart
:
438 regs
->u_regs
[UREG_I0
] = EINTR
;
442 if (!(sa
->sa_flags
& SA_RESTART
))
443 goto no_system_call_restart
;
446 regs
->u_regs
[UREG_I0
] = orig_i0
;
452 /* Note that 'init' is a special process: it doesn't get signals it doesn't
453 * want to handle. Thus you cannot kill init even with a SIGKILL even by
456 static void do_signal(struct pt_regs
*regs
, unsigned long orig_i0
)
462 /* It's a lot of work and synchronization to add a new ptrace
463 * register for GDB to save and restore in order to get
464 * orig_i0 correct for syscall restarts when debugging.
466 * Although it should be the case that most of the global
467 * registers are volatile across a system call, glibc already
468 * depends upon that fact that we preserve them. So we can't
469 * just use any global register to save away the orig_i0 value.
471 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
472 * preserved across a system call trap by various pieces of
475 * %g7 is used as the "thread register". %g6 is not used in
476 * any fixed manner. %g6 is used as a scratch register and
477 * a compiler temporary, but it's value is never used across
478 * a system call. Therefore %g6 is usable for orig_i0 storage.
480 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
481 regs
->u_regs
[UREG_G6
] = orig_i0
;
483 has_handler
= get_signal(&ksig
);
485 /* If the debugger messes with the program counter, it clears
486 * the software "in syscall" bit, directing us to not perform
490 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
)) {
492 orig_i0
= regs
->u_regs
[UREG_G6
];
497 syscall_restart(orig_i0
, regs
, &ksig
.ka
.sa
);
498 handle_signal(&ksig
, regs
);
500 if (restart_syscall
) {
501 switch (regs
->u_regs
[UREG_I0
]) {
505 /* replay the system call when we are done */
506 regs
->u_regs
[UREG_I0
] = orig_i0
;
509 pt_regs_clear_syscall(regs
);
511 case ERESTART_RESTARTBLOCK
:
512 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
515 pt_regs_clear_syscall(regs
);
518 restore_saved_sigmask();
522 void do_notify_resume(struct pt_regs
*regs
, unsigned long orig_i0
,
523 unsigned long thread_info_flags
)
525 if (thread_info_flags
& _TIF_SIGPENDING
)
526 do_signal(regs
, orig_i0
);
527 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
528 clear_thread_flag(TIF_NOTIFY_RESUME
);
529 tracehook_notify_resume(regs
);
533 asmlinkage
int do_sys_sigstack(struct sigstack __user
*ssptr
,
534 struct sigstack __user
*ossptr
,
539 /* First see if old state is wanted. */
541 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
542 &ossptr
->the_stack
) ||
543 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
547 /* Now see if we want to update the new state. */
551 if (get_user(ss_sp
, &ssptr
->the_stack
))
553 /* If the current stack was set with sigaltstack, don't
554 swap stacks while we are on it. */
556 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
559 /* Since we don't know the extent of the stack, and we don't
560 track onstack-ness, but rather calculate it, we must
561 presume a size. Ho hum this interface is lossy. */
562 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
563 current
->sas_ss_size
= SIGSTKSZ
;