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/resume_user_mode.h>
24 #include <linux/uaccess.h>
25 #include <asm/ptrace.h>
26 #include <asm/cacheflush.h> /* flush_sig_insns */
27 #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 /* Checks if the fp is valid. We always build signal frames which are
63 * 16-byte aligned, therefore we can always enforce that the restore
64 * frame has that property as well.
66 static inline bool invalid_frame_pointer(void __user
*fp
, int fplen
)
68 if ((((unsigned long) fp
) & 15) || !access_ok(fp
, fplen
))
74 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
76 unsigned long up_psr
, pc
, npc
, ufp
;
77 struct signal_frame __user
*sf
;
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
->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 (invalid_frame_pointer(sf
, sizeof(*sf
)))
94 if (get_user(ufp
, &sf
->info
.si_regs
.u_regs
[UREG_FP
]))
100 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
101 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
106 /* 2. Restore the state */
108 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
110 /* User can only change condition codes and FPU enabling in %psr. */
111 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
112 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
114 /* Prevent syscall restart. */
115 pt_regs_clear_syscall(regs
);
117 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
119 err
|= restore_fpu_state(regs
, fpu_save
);
120 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
122 err
|= restore_rwin_state(rwin_save
);
124 /* This is pretty much atomic, no amount locking would prevent
125 * the races which exist anyways.
127 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
128 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
129 (_NSIG_WORDS
-1) * sizeof(unsigned int));
134 set_current_blocked(&set
);
141 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
143 struct rt_signal_frame __user
*sf
;
144 unsigned int psr
, pc
, npc
, ufp
;
145 __siginfo_fpu_t __user
*fpu_save
;
146 __siginfo_rwin_t __user
*rwin_save
;
150 synchronize_user_stack();
151 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
152 if (invalid_frame_pointer(sf
, sizeof(*sf
)))
155 if (get_user(ufp
, &sf
->regs
.u_regs
[UREG_FP
]))
161 err
= __get_user(pc
, &sf
->regs
.pc
);
162 err
|= __get_user(npc
, &sf
->regs
.npc
);
163 err
|= ((pc
| npc
) & 0x03);
165 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
166 err
|= __get_user(psr
, &sf
->regs
.psr
);
168 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
169 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
171 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
173 /* Prevent syscall restart. */
174 pt_regs_clear_syscall(regs
);
176 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
177 if (!err
&& fpu_save
)
178 err
|= restore_fpu_state(regs
, fpu_save
);
179 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
180 err
|= restore_altstack(&sf
->stack
);
188 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
189 if (!err
&& rwin_save
) {
190 if (restore_rwin_state(rwin_save
))
194 set_current_blocked(&set
);
200 static inline void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
, unsigned long framesize
)
202 unsigned long sp
= regs
->u_regs
[UREG_FP
];
205 * If we are on the alternate signal stack and would overflow it, don't.
206 * Return an always-bogus address instead so we will die with SIGSEGV.
208 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
209 return (void __user
*) -1L;
211 /* This is the X/Open sanctioned signal stack switching. */
212 sp
= sigsp(sp
, ksig
) - framesize
;
214 /* Always align the stack frame. This handles two cases. First,
215 * sigaltstack need not be mindful of platform specific stack
216 * alignment. Second, if we took this signal because the stack
217 * is not aligned properly, we'd like to take the signal cleanly
222 return (void __user
*) sp
;
225 static int setup_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
228 struct signal_frame __user
*sf
;
229 int sigframe_size
, err
, wsaved
;
232 /* 1. Make sure everything is clean */
233 synchronize_user_stack();
235 wsaved
= current_thread_info()->w_saved
;
237 sigframe_size
= sizeof(*sf
);
239 sigframe_size
+= sizeof(__siginfo_fpu_t
);
241 sigframe_size
+= sizeof(__siginfo_rwin_t
);
243 sf
= (struct signal_frame __user
*)
244 get_sigframe(ksig
, regs
, sigframe_size
);
246 if (invalid_frame_pointer(sf
, sigframe_size
)) {
247 force_exit_sig(SIGILL
);
253 /* 2. Save the current process state */
254 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
256 err
|= __put_user(0, &sf
->extra_size
);
259 __siginfo_fpu_t __user
*fp
= tail
;
261 err
|= save_fpu_state(regs
, fp
);
262 err
|= __put_user(fp
, &sf
->fpu_save
);
264 err
|= __put_user(0, &sf
->fpu_save
);
267 __siginfo_rwin_t __user
*rwp
= tail
;
268 tail
+= sizeof(*rwp
);
269 err
|= save_rwin_state(wsaved
, rwp
);
270 err
|= __put_user(rwp
, &sf
->rwin_save
);
272 err
|= __put_user(0, &sf
->rwin_save
);
275 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
276 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
277 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
279 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
280 sizeof(struct reg_window32
));
282 struct reg_window32
*rp
;
284 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
285 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
290 /* 3. signal handler back-trampoline and parameters */
291 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
292 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
293 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
294 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
296 /* 4. signal handler */
297 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
298 regs
->npc
= (regs
->pc
+ 4);
300 /* 5. return to kernel instructions */
301 if (ksig
->ka
.ka_restorer
)
302 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
304 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
306 /* mov __NR_sigreturn, %g1 */
307 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
310 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
314 /* Flush instruction space. */
315 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
320 static int setup_rt_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
323 struct rt_signal_frame __user
*sf
;
324 int sigframe_size
, wsaved
;
329 synchronize_user_stack();
330 wsaved
= current_thread_info()->w_saved
;
331 sigframe_size
= sizeof(*sf
);
333 sigframe_size
+= sizeof(__siginfo_fpu_t
);
335 sigframe_size
+= sizeof(__siginfo_rwin_t
);
336 sf
= (struct rt_signal_frame __user
*)
337 get_sigframe(ksig
, regs
, sigframe_size
);
338 if (invalid_frame_pointer(sf
, sigframe_size
)) {
339 force_exit_sig(SIGILL
);
344 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
345 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
346 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
350 err
|= __put_user(psr
, &sf
->regs
.psr
);
351 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
352 err
|= __put_user(0, &sf
->extra_size
);
355 __siginfo_fpu_t __user
*fp
= tail
;
357 err
|= save_fpu_state(regs
, fp
);
358 err
|= __put_user(fp
, &sf
->fpu_save
);
360 err
|= __put_user(0, &sf
->fpu_save
);
363 __siginfo_rwin_t __user
*rwp
= tail
;
364 tail
+= sizeof(*rwp
);
365 err
|= save_rwin_state(wsaved
, rwp
);
366 err
|= __put_user(rwp
, &sf
->rwin_save
);
368 err
|= __put_user(0, &sf
->rwin_save
);
370 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
372 /* Setup sigaltstack */
373 err
|= __save_altstack(&sf
->stack
, regs
->u_regs
[UREG_FP
]);
376 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
377 sizeof(struct reg_window32
));
379 struct reg_window32
*rp
;
381 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
382 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
385 err
|= copy_siginfo_to_user(&sf
->info
, &ksig
->info
);
390 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
391 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
392 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
393 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
395 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
396 regs
->npc
= (regs
->pc
+ 4);
398 if (ksig
->ka
.ka_restorer
)
399 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
401 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
403 /* mov __NR_rt_sigreturn, %g1 */
404 err
|= __put_user(0x82102065, &sf
->insns
[0]);
407 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
411 /* Flush instruction space. */
412 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
418 handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
420 sigset_t
*oldset
= sigmask_to_save();
423 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
424 err
= setup_rt_frame(ksig
, regs
, oldset
);
426 err
= setup_frame(ksig
, regs
, oldset
);
427 signal_setup_done(err
, ksig
, 0);
430 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
431 struct sigaction
*sa
)
433 switch(regs
->u_regs
[UREG_I0
]) {
434 case ERESTART_RESTARTBLOCK
:
436 no_system_call_restart
:
437 regs
->u_regs
[UREG_I0
] = EINTR
;
441 if (!(sa
->sa_flags
& SA_RESTART
))
442 goto no_system_call_restart
;
445 regs
->u_regs
[UREG_I0
] = orig_i0
;
451 /* Note that 'init' is a special process: it doesn't get signals it doesn't
452 * want to handle. Thus you cannot kill init even with a SIGKILL even by
455 static void do_signal(struct pt_regs
*regs
, unsigned long orig_i0
)
461 /* It's a lot of work and synchronization to add a new ptrace
462 * register for GDB to save and restore in order to get
463 * orig_i0 correct for syscall restarts when debugging.
465 * Although it should be the case that most of the global
466 * registers are volatile across a system call, glibc already
467 * depends upon that fact that we preserve them. So we can't
468 * just use any global register to save away the orig_i0 value.
470 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
471 * preserved across a system call trap by various pieces of
474 * %g7 is used as the "thread register". %g6 is not used in
475 * any fixed manner. %g6 is used as a scratch register and
476 * a compiler temporary, but its value is never used across
477 * a system call. Therefore %g6 is usable for orig_i0 storage.
479 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
480 regs
->u_regs
[UREG_G6
] = orig_i0
;
482 has_handler
= get_signal(&ksig
);
484 /* If the debugger messes with the program counter, it clears
485 * the software "in syscall" bit, directing us to not perform
489 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
)) {
491 orig_i0
= regs
->u_regs
[UREG_G6
];
496 syscall_restart(orig_i0
, regs
, &ksig
.ka
.sa
);
497 handle_signal(&ksig
, regs
);
499 if (restart_syscall
) {
500 switch (regs
->u_regs
[UREG_I0
]) {
504 /* replay the system call when we are done */
505 regs
->u_regs
[UREG_I0
] = orig_i0
;
508 pt_regs_clear_syscall(regs
);
510 case ERESTART_RESTARTBLOCK
:
511 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
514 pt_regs_clear_syscall(regs
);
517 restore_saved_sigmask();
521 void do_notify_resume(struct pt_regs
*regs
, unsigned long orig_i0
,
522 unsigned long thread_info_flags
)
524 if (thread_info_flags
& (_TIF_SIGPENDING
| _TIF_NOTIFY_SIGNAL
))
525 do_signal(regs
, orig_i0
);
526 if (thread_info_flags
& _TIF_NOTIFY_RESUME
)
527 resume_user_mode_work(regs
);
530 asmlinkage
int do_sys_sigstack(struct sigstack __user
*ssptr
,
531 struct sigstack __user
*ossptr
,
536 /* First see if old state is wanted. */
538 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
539 &ossptr
->the_stack
) ||
540 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
544 /* Now see if we want to update the new state. */
548 if (get_user(ss_sp
, &ssptr
->the_stack
))
550 /* If the current stack was set with sigaltstack, don't
551 swap stacks while we are on it. */
553 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
556 /* Since we don't know the extent of the stack, and we don't
557 track onstack-ness, but rather calculate it, we must
558 presume a size. Ho hum this interface is lossy. */
559 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
560 current
->sas_ss_size
= SIGSTKSZ
;