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/pgtable.h>
28 #include <asm/cacheflush.h> /* flush_sig_insns */
29 #include <asm/switch_to.h>
34 extern void fpsave(unsigned long *fpregs
, unsigned long *fsr
,
35 void *fpqueue
, unsigned long *fpqdepth
);
36 extern void fpload(unsigned long *fpregs
, unsigned long *fsr
);
39 struct sparc_stackf ss
;
41 __siginfo_fpu_t __user
*fpu_save
;
42 unsigned long insns
[2] __attribute__ ((aligned (8)));
43 unsigned int extramask
[_NSIG_WORDS
- 1];
44 unsigned int extra_size
; /* Should be 0 */
45 __siginfo_rwin_t __user
*rwin_save
;
46 } __attribute__((aligned(8)));
48 struct rt_signal_frame
{
49 struct sparc_stackf ss
;
53 __siginfo_fpu_t __user
*fpu_save
;
54 unsigned int insns
[2];
56 unsigned int extra_size
; /* Should be 0 */
57 __siginfo_rwin_t __user
*rwin_save
;
58 } __attribute__((aligned(8)));
61 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
62 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
64 /* Checks if the fp is valid. We always build signal frames which are
65 * 16-byte aligned, therefore we can always enforce that the restore
66 * frame has that property as well.
68 static inline bool invalid_frame_pointer(void __user
*fp
, int fplen
)
70 if ((((unsigned long) fp
) & 15) || !__access_ok((unsigned long)fp
, fplen
))
76 asmlinkage
void do_sigreturn(struct pt_regs
*regs
)
78 unsigned long up_psr
, pc
, npc
, ufp
;
79 struct signal_frame __user
*sf
;
81 __siginfo_fpu_t __user
*fpu_save
;
82 __siginfo_rwin_t __user
*rwin_save
;
85 /* Always make any pending restarted system calls return -EINTR */
86 current
->restart_block
.fn
= do_no_restart_syscall
;
88 synchronize_user_stack();
90 sf
= (struct signal_frame __user
*) regs
->u_regs
[UREG_FP
];
92 /* 1. Make sure we are not getting garbage from the user */
93 if (invalid_frame_pointer(sf
, sizeof(*sf
)))
96 if (get_user(ufp
, &sf
->info
.si_regs
.u_regs
[UREG_FP
]))
102 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
103 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
108 /* 2. Restore the state */
110 err
|= __copy_from_user(regs
, &sf
->info
.si_regs
, sizeof(struct pt_regs
));
112 /* User can only change condition codes and FPU enabling in %psr. */
113 regs
->psr
= (up_psr
& ~(PSR_ICC
| PSR_EF
))
114 | (regs
->psr
& (PSR_ICC
| PSR_EF
));
116 /* Prevent syscall restart. */
117 pt_regs_clear_syscall(regs
);
119 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
121 err
|= restore_fpu_state(regs
, fpu_save
);
122 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
124 err
|= restore_rwin_state(rwin_save
);
126 /* This is pretty much atomic, no amount locking would prevent
127 * the races which exist anyways.
129 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
130 err
|= __copy_from_user(&set
.sig
[1], &sf
->extramask
,
131 (_NSIG_WORDS
-1) * sizeof(unsigned int));
136 set_current_blocked(&set
);
140 force_sig(SIGSEGV
, current
);
143 asmlinkage
void do_rt_sigreturn(struct pt_regs
*regs
)
145 struct rt_signal_frame __user
*sf
;
146 unsigned int psr
, pc
, npc
, ufp
;
147 __siginfo_fpu_t __user
*fpu_save
;
148 __siginfo_rwin_t __user
*rwin_save
;
152 synchronize_user_stack();
153 sf
= (struct rt_signal_frame __user
*) regs
->u_regs
[UREG_FP
];
154 if (invalid_frame_pointer(sf
, sizeof(*sf
)))
157 if (get_user(ufp
, &sf
->regs
.u_regs
[UREG_FP
]))
163 err
= __get_user(pc
, &sf
->regs
.pc
);
164 err
|= __get_user(npc
, &sf
->regs
.npc
);
165 err
|= ((pc
| npc
) & 0x03);
167 err
|= __get_user(regs
->y
, &sf
->regs
.y
);
168 err
|= __get_user(psr
, &sf
->regs
.psr
);
170 err
|= __copy_from_user(®s
->u_regs
[UREG_G1
],
171 &sf
->regs
.u_regs
[UREG_G1
], 15 * sizeof(u32
));
173 regs
->psr
= (regs
->psr
& ~PSR_ICC
) | (psr
& PSR_ICC
);
175 /* Prevent syscall restart. */
176 pt_regs_clear_syscall(regs
);
178 err
|= __get_user(fpu_save
, &sf
->fpu_save
);
179 if (!err
&& fpu_save
)
180 err
|= restore_fpu_state(regs
, fpu_save
);
181 err
|= __copy_from_user(&set
, &sf
->mask
, sizeof(sigset_t
));
182 err
|= restore_altstack(&sf
->stack
);
190 err
|= __get_user(rwin_save
, &sf
->rwin_save
);
191 if (!err
&& rwin_save
) {
192 if (restore_rwin_state(rwin_save
))
196 set_current_blocked(&set
);
199 force_sig(SIGSEGV
, current
);
202 static inline void __user
*get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
, unsigned long framesize
)
204 unsigned long sp
= regs
->u_regs
[UREG_FP
];
207 * If we are on the alternate signal stack and would overflow it, don't.
208 * Return an always-bogus address instead so we will die with SIGSEGV.
210 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- framesize
)))
211 return (void __user
*) -1L;
213 /* This is the X/Open sanctioned signal stack switching. */
214 sp
= sigsp(sp
, ksig
) - framesize
;
216 /* Always align the stack frame. This handles two cases. First,
217 * sigaltstack need not be mindful of platform specific stack
218 * alignment. Second, if we took this signal because the stack
219 * is not aligned properly, we'd like to take the signal cleanly
224 return (void __user
*) sp
;
227 static int setup_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
230 struct signal_frame __user
*sf
;
231 int sigframe_size
, err
, wsaved
;
234 /* 1. Make sure everything is clean */
235 synchronize_user_stack();
237 wsaved
= current_thread_info()->w_saved
;
239 sigframe_size
= sizeof(*sf
);
241 sigframe_size
+= sizeof(__siginfo_fpu_t
);
243 sigframe_size
+= sizeof(__siginfo_rwin_t
);
245 sf
= (struct signal_frame __user
*)
246 get_sigframe(ksig
, regs
, sigframe_size
);
248 if (invalid_frame_pointer(sf
, sigframe_size
)) {
255 /* 2. Save the current process state */
256 err
= __copy_to_user(&sf
->info
.si_regs
, regs
, sizeof(struct pt_regs
));
258 err
|= __put_user(0, &sf
->extra_size
);
261 __siginfo_fpu_t __user
*fp
= tail
;
263 err
|= save_fpu_state(regs
, fp
);
264 err
|= __put_user(fp
, &sf
->fpu_save
);
266 err
|= __put_user(0, &sf
->fpu_save
);
269 __siginfo_rwin_t __user
*rwp
= tail
;
270 tail
+= sizeof(*rwp
);
271 err
|= save_rwin_state(wsaved
, rwp
);
272 err
|= __put_user(rwp
, &sf
->rwin_save
);
274 err
|= __put_user(0, &sf
->rwin_save
);
277 err
|= __put_user(oldset
->sig
[0], &sf
->info
.si_mask
);
278 err
|= __copy_to_user(sf
->extramask
, &oldset
->sig
[1],
279 (_NSIG_WORDS
- 1) * sizeof(unsigned int));
281 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
282 sizeof(struct reg_window32
));
284 struct reg_window32
*rp
;
286 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
287 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
292 /* 3. signal handler back-trampoline and parameters */
293 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
294 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
295 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
296 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->info
;
298 /* 4. signal handler */
299 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
300 regs
->npc
= (regs
->pc
+ 4);
302 /* 5. return to kernel instructions */
303 if (ksig
->ka
.ka_restorer
)
304 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
306 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
308 /* mov __NR_sigreturn, %g1 */
309 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
312 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
316 /* Flush instruction space. */
317 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
322 static int setup_rt_frame(struct ksignal
*ksig
, struct pt_regs
*regs
,
325 struct rt_signal_frame __user
*sf
;
326 int sigframe_size
, wsaved
;
331 synchronize_user_stack();
332 wsaved
= current_thread_info()->w_saved
;
333 sigframe_size
= sizeof(*sf
);
335 sigframe_size
+= sizeof(__siginfo_fpu_t
);
337 sigframe_size
+= sizeof(__siginfo_rwin_t
);
338 sf
= (struct rt_signal_frame __user
*)
339 get_sigframe(ksig
, regs
, sigframe_size
);
340 if (invalid_frame_pointer(sf
, sigframe_size
)) {
346 err
= __put_user(regs
->pc
, &sf
->regs
.pc
);
347 err
|= __put_user(regs
->npc
, &sf
->regs
.npc
);
348 err
|= __put_user(regs
->y
, &sf
->regs
.y
);
352 err
|= __put_user(psr
, &sf
->regs
.psr
);
353 err
|= __copy_to_user(&sf
->regs
.u_regs
, regs
->u_regs
, sizeof(regs
->u_regs
));
354 err
|= __put_user(0, &sf
->extra_size
);
357 __siginfo_fpu_t __user
*fp
= tail
;
359 err
|= save_fpu_state(regs
, fp
);
360 err
|= __put_user(fp
, &sf
->fpu_save
);
362 err
|= __put_user(0, &sf
->fpu_save
);
365 __siginfo_rwin_t __user
*rwp
= tail
;
366 tail
+= sizeof(*rwp
);
367 err
|= save_rwin_state(wsaved
, rwp
);
368 err
|= __put_user(rwp
, &sf
->rwin_save
);
370 err
|= __put_user(0, &sf
->rwin_save
);
372 err
|= __copy_to_user(&sf
->mask
, &oldset
->sig
[0], sizeof(sigset_t
));
374 /* Setup sigaltstack */
375 err
|= __save_altstack(&sf
->stack
, regs
->u_regs
[UREG_FP
]);
378 err
|= __copy_to_user(sf
, (char *) regs
->u_regs
[UREG_FP
],
379 sizeof(struct reg_window32
));
381 struct reg_window32
*rp
;
383 rp
= ¤t_thread_info()->reg_window
[wsaved
- 1];
384 err
|= __copy_to_user(sf
, rp
, sizeof(struct reg_window32
));
387 err
|= copy_siginfo_to_user(&sf
->info
, &ksig
->info
);
392 regs
->u_regs
[UREG_FP
] = (unsigned long) sf
;
393 regs
->u_regs
[UREG_I0
] = ksig
->sig
;
394 regs
->u_regs
[UREG_I1
] = (unsigned long) &sf
->info
;
395 regs
->u_regs
[UREG_I2
] = (unsigned long) &sf
->regs
;
397 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
398 regs
->npc
= (regs
->pc
+ 4);
400 if (ksig
->ka
.ka_restorer
)
401 regs
->u_regs
[UREG_I7
] = (unsigned long)ksig
->ka
.ka_restorer
;
403 regs
->u_regs
[UREG_I7
] = (unsigned long)(&(sf
->insns
[0]) - 2);
405 /* mov __NR_sigreturn, %g1 */
406 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
409 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
413 /* Flush instruction space. */
414 flush_sig_insns(current
->mm
, (unsigned long) &(sf
->insns
[0]));
420 handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
422 sigset_t
*oldset
= sigmask_to_save();
425 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
426 err
= setup_rt_frame(ksig
, regs
, oldset
);
428 err
= setup_frame(ksig
, regs
, oldset
);
429 signal_setup_done(err
, ksig
, 0);
432 static inline void syscall_restart(unsigned long orig_i0
, struct pt_regs
*regs
,
433 struct sigaction
*sa
)
435 switch(regs
->u_regs
[UREG_I0
]) {
436 case ERESTART_RESTARTBLOCK
:
438 no_system_call_restart
:
439 regs
->u_regs
[UREG_I0
] = EINTR
;
443 if (!(sa
->sa_flags
& SA_RESTART
))
444 goto no_system_call_restart
;
447 regs
->u_regs
[UREG_I0
] = orig_i0
;
453 /* Note that 'init' is a special process: it doesn't get signals it doesn't
454 * want to handle. Thus you cannot kill init even with a SIGKILL even by
457 static void do_signal(struct pt_regs
*regs
, unsigned long orig_i0
)
463 /* It's a lot of work and synchronization to add a new ptrace
464 * register for GDB to save and restore in order to get
465 * orig_i0 correct for syscall restarts when debugging.
467 * Although it should be the case that most of the global
468 * registers are volatile across a system call, glibc already
469 * depends upon that fact that we preserve them. So we can't
470 * just use any global register to save away the orig_i0 value.
472 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
473 * preserved across a system call trap by various pieces of
476 * %g7 is used as the "thread register". %g6 is not used in
477 * any fixed manner. %g6 is used as a scratch register and
478 * a compiler temporary, but it's value is never used across
479 * a system call. Therefore %g6 is usable for orig_i0 storage.
481 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
))
482 regs
->u_regs
[UREG_G6
] = orig_i0
;
484 has_handler
= get_signal(&ksig
);
486 /* If the debugger messes with the program counter, it clears
487 * the software "in syscall" bit, directing us to not perform
491 if (pt_regs_is_syscall(regs
) && (regs
->psr
& PSR_C
)) {
493 orig_i0
= regs
->u_regs
[UREG_G6
];
498 syscall_restart(orig_i0
, regs
, &ksig
.ka
.sa
);
499 handle_signal(&ksig
, regs
);
501 if (restart_syscall
) {
502 switch (regs
->u_regs
[UREG_I0
]) {
506 /* replay the system call when we are done */
507 regs
->u_regs
[UREG_I0
] = orig_i0
;
510 pt_regs_clear_syscall(regs
);
512 case ERESTART_RESTARTBLOCK
:
513 regs
->u_regs
[UREG_G1
] = __NR_restart_syscall
;
516 pt_regs_clear_syscall(regs
);
519 restore_saved_sigmask();
523 void do_notify_resume(struct pt_regs
*regs
, unsigned long orig_i0
,
524 unsigned long thread_info_flags
)
526 if (thread_info_flags
& _TIF_SIGPENDING
)
527 do_signal(regs
, orig_i0
);
528 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
529 clear_thread_flag(TIF_NOTIFY_RESUME
);
530 tracehook_notify_resume(regs
);
534 asmlinkage
int do_sys_sigstack(struct sigstack __user
*ssptr
,
535 struct sigstack __user
*ossptr
,
540 /* First see if old state is wanted. */
542 if (put_user(current
->sas_ss_sp
+ current
->sas_ss_size
,
543 &ossptr
->the_stack
) ||
544 __put_user(on_sig_stack(sp
), &ossptr
->cur_status
))
548 /* Now see if we want to update the new state. */
552 if (get_user(ss_sp
, &ssptr
->the_stack
))
554 /* If the current stack was set with sigaltstack, don't
555 swap stacks while we are on it. */
557 if (current
->sas_ss_sp
&& on_sig_stack(sp
))
560 /* Since we don't know the extent of the stack, and we don't
561 track onstack-ness, but rather calculate it, we must
562 presume a size. Ho hum this interface is lossy. */
563 current
->sas_ss_sp
= (unsigned long)ss_sp
- SIGSTKSZ
;
564 current
->sas_ss_size
= SIGSTKSZ
;