xref: /openbmc/linux/arch/sparc/kernel/signal32.c (revision ed84ef1c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*  arch/sparc64/kernel/signal32.c
3  *
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)
8  *  Copyright (C) 1997,1998 Jakub Jelinek   (jj@sunsite.mff.cuni.cz)
9  */
10 
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/wait.h>
16 #include <linux/ptrace.h>
17 #include <linux/unistd.h>
18 #include <linux/mm.h>
19 #include <linux/tty.h>
20 #include <linux/binfmts.h>
21 #include <linux/compat.h>
22 #include <linux/bitops.h>
23 #include <linux/tracehook.h>
24 
25 #include <linux/uaccess.h>
26 #include <asm/ptrace.h>
27 #include <asm/psrcompat.h>
28 #include <asm/fpumacro.h>
29 #include <asm/visasm.h>
30 #include <asm/compat_signal.h>
31 #include <asm/switch_to.h>
32 
33 #include "sigutil.h"
34 #include "kernel.h"
35 
36 /* This magic should be in g_upper[0] for all upper parts
37  * to be valid.
38  */
39 #define SIGINFO_EXTRA_V8PLUS_MAGIC	0x130e269
40 typedef struct {
41 	unsigned int g_upper[8];
42 	unsigned int o_upper[8];
43 	unsigned int asi;
44 } siginfo_extra_v8plus_t;
45 
46 struct signal_frame32 {
47 	struct sparc_stackf32	ss;
48 	__siginfo32_t		info;
49 	/* __siginfo_fpu_t * */ u32 fpu_save;
50 	unsigned int		insns[2];
51 	unsigned int		extramask[_COMPAT_NSIG_WORDS - 1];
52 	unsigned int		extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
53 	/* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
54 	siginfo_extra_v8plus_t	v8plus;
55 	/* __siginfo_rwin_t * */u32 rwin_save;
56 } __attribute__((aligned(8)));
57 
58 struct rt_signal_frame32 {
59 	struct sparc_stackf32	ss;
60 	compat_siginfo_t	info;
61 	struct pt_regs32	regs;
62 	compat_sigset_t		mask;
63 	/* __siginfo_fpu_t * */ u32 fpu_save;
64 	unsigned int		insns[2];
65 	compat_stack_t		stack;
66 	unsigned int		extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
67 	/* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
68 	siginfo_extra_v8plus_t	v8plus;
69 	/* __siginfo_rwin_t * */u32 rwin_save;
70 } __attribute__((aligned(8)));
71 
72 /* Checks if the fp is valid.  We always build signal frames which are
73  * 16-byte aligned, therefore we can always enforce that the restore
74  * frame has that property as well.
75  */
76 static bool invalid_frame_pointer(void __user *fp, int fplen)
77 {
78 	if ((((unsigned long) fp) & 15) ||
79 	    ((unsigned long)fp) > 0x100000000ULL - fplen)
80 		return true;
81 	return false;
82 }
83 
84 void do_sigreturn32(struct pt_regs *regs)
85 {
86 	struct signal_frame32 __user *sf;
87 	compat_uptr_t fpu_save;
88 	compat_uptr_t rwin_save;
89 	unsigned int psr, ufp;
90 	unsigned int pc, npc;
91 	sigset_t set;
92 	compat_sigset_t seta;
93 	int err, i;
94 
95 	/* Always make any pending restarted system calls return -EINTR */
96 	current->restart_block.fn = do_no_restart_syscall;
97 
98 	synchronize_user_stack();
99 
100 	regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
101 	sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
102 
103 	/* 1. Make sure we are not getting garbage from the user */
104 	if (invalid_frame_pointer(sf, sizeof(*sf)))
105 		goto segv;
106 
107 	if (get_user(ufp, &sf->info.si_regs.u_regs[UREG_FP]))
108 		goto segv;
109 
110 	if (ufp & 0x7)
111 		goto segv;
112 
113 	if (__get_user(pc, &sf->info.si_regs.pc) ||
114 	    __get_user(npc, &sf->info.si_regs.npc))
115 		goto segv;
116 
117 	if ((pc | npc) & 3)
118 		goto segv;
119 
120 	if (test_thread_flag(TIF_32BIT)) {
121 		pc &= 0xffffffff;
122 		npc &= 0xffffffff;
123 	}
124 	regs->tpc = pc;
125 	regs->tnpc = npc;
126 
127 	/* 2. Restore the state */
128 	err = __get_user(regs->y, &sf->info.si_regs.y);
129 	err |= __get_user(psr, &sf->info.si_regs.psr);
130 
131 	for (i = UREG_G1; i <= UREG_I7; i++)
132 		err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
133 	if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
134 		err |= __get_user(i, &sf->v8plus.g_upper[0]);
135 		if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
136 			unsigned long asi;
137 
138 			for (i = UREG_G1; i <= UREG_I7; i++)
139 				err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
140 			err |= __get_user(asi, &sf->v8plus.asi);
141 			regs->tstate &= ~TSTATE_ASI;
142 			regs->tstate |= ((asi & 0xffUL) << 24UL);
143 		}
144 	}
145 
146 	/* User can only change condition codes in %tstate. */
147 	regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
148 	regs->tstate |= psr_to_tstate_icc(psr);
149 
150 	/* Prevent syscall restart.  */
151 	pt_regs_clear_syscall(regs);
152 
153 	err |= __get_user(fpu_save, &sf->fpu_save);
154 	if (!err && fpu_save)
155 		err |= restore_fpu_state(regs, compat_ptr(fpu_save));
156 	err |= __get_user(rwin_save, &sf->rwin_save);
157 	if (!err && rwin_save) {
158 		if (restore_rwin_state(compat_ptr(rwin_save)))
159 			goto segv;
160 	}
161 	err |= __get_user(seta.sig[0], &sf->info.si_mask);
162 	err |= copy_from_user(&seta.sig[1], &sf->extramask,
163 			      (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
164 	if (err)
165 	    	goto segv;
166 
167 	set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
168 	set_current_blocked(&set);
169 	return;
170 
171 segv:
172 	force_sig(SIGSEGV);
173 }
174 
175 asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
176 {
177 	struct rt_signal_frame32 __user *sf;
178 	unsigned int psr, pc, npc, ufp;
179 	compat_uptr_t fpu_save;
180 	compat_uptr_t rwin_save;
181 	sigset_t set;
182 	int err, i;
183 
184 	/* Always make any pending restarted system calls return -EINTR */
185 	current->restart_block.fn = do_no_restart_syscall;
186 
187 	synchronize_user_stack();
188 	regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
189 	sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
190 
191 	/* 1. Make sure we are not getting garbage from the user */
192 	if (invalid_frame_pointer(sf, sizeof(*sf)))
193 		goto segv;
194 
195 	if (get_user(ufp, &sf->regs.u_regs[UREG_FP]))
196 		goto segv;
197 
198 	if (ufp & 0x7)
199 		goto segv;
200 
201 	if (__get_user(pc, &sf->regs.pc) ||
202 	    __get_user(npc, &sf->regs.npc))
203 		goto segv;
204 
205 	if ((pc | npc) & 3)
206 		goto segv;
207 
208 	if (test_thread_flag(TIF_32BIT)) {
209 		pc &= 0xffffffff;
210 		npc &= 0xffffffff;
211 	}
212 	regs->tpc = pc;
213 	regs->tnpc = npc;
214 
215 	/* 2. Restore the state */
216 	err = __get_user(regs->y, &sf->regs.y);
217 	err |= __get_user(psr, &sf->regs.psr);
218 
219 	for (i = UREG_G1; i <= UREG_I7; i++)
220 		err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
221 	if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
222 		err |= __get_user(i, &sf->v8plus.g_upper[0]);
223 		if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
224 			unsigned long asi;
225 
226 			for (i = UREG_G1; i <= UREG_I7; i++)
227 				err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
228 			err |= __get_user(asi, &sf->v8plus.asi);
229 			regs->tstate &= ~TSTATE_ASI;
230 			regs->tstate |= ((asi & 0xffUL) << 24UL);
231 		}
232 	}
233 
234 	/* User can only change condition codes in %tstate. */
235 	regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
236 	regs->tstate |= psr_to_tstate_icc(psr);
237 
238 	/* Prevent syscall restart.  */
239 	pt_regs_clear_syscall(regs);
240 
241 	err |= __get_user(fpu_save, &sf->fpu_save);
242 	if (!err && fpu_save)
243 		err |= restore_fpu_state(regs, compat_ptr(fpu_save));
244 	err |= get_compat_sigset(&set, &sf->mask);
245 	err |= compat_restore_altstack(&sf->stack);
246 	if (err)
247 		goto segv;
248 
249 	err |= __get_user(rwin_save, &sf->rwin_save);
250 	if (!err && rwin_save) {
251 		if (restore_rwin_state(compat_ptr(rwin_save)))
252 			goto segv;
253 	}
254 
255 	set_current_blocked(&set);
256 	return;
257 segv:
258 	force_sig(SIGSEGV);
259 }
260 
261 static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
262 {
263 	unsigned long sp;
264 
265 	regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
266 	sp = regs->u_regs[UREG_FP];
267 
268 	/*
269 	 * If we are on the alternate signal stack and would overflow it, don't.
270 	 * Return an always-bogus address instead so we will die with SIGSEGV.
271 	 */
272 	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
273 		return (void __user *) -1L;
274 
275 	/* This is the X/Open sanctioned signal stack switching.  */
276 	sp = sigsp(sp, ksig) - framesize;
277 
278 	/* Always align the stack frame.  This handles two cases.  First,
279 	 * sigaltstack need not be mindful of platform specific stack
280 	 * alignment.  Second, if we took this signal because the stack
281 	 * is not aligned properly, we'd like to take the signal cleanly
282 	 * and report that.
283 	 */
284 	sp &= ~15UL;
285 
286 	return (void __user *) sp;
287 }
288 
289 /* The I-cache flush instruction only works in the primary ASI, which
290  * right now is the nucleus, aka. kernel space.
291  *
292  * Therefore we have to kick the instructions out using the kernel
293  * side linear mapping of the physical address backing the user
294  * instructions.
295  */
296 static void flush_signal_insns(unsigned long address)
297 {
298 	unsigned long pstate, paddr;
299 	pte_t *ptep, pte;
300 	pgd_t *pgdp;
301 	p4d_t *p4dp;
302 	pud_t *pudp;
303 	pmd_t *pmdp;
304 
305 	/* Commit all stores of the instructions we are about to flush.  */
306 	wmb();
307 
308 	/* Disable cross-call reception.  In this way even a very wide
309 	 * munmap() on another cpu can't tear down the page table
310 	 * hierarchy from underneath us, since that can't complete
311 	 * until the IPI tlb flush returns.
312 	 */
313 
314 	__asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
315 	__asm__ __volatile__("wrpr %0, %1, %%pstate"
316 				: : "r" (pstate), "i" (PSTATE_IE));
317 
318 	pgdp = pgd_offset(current->mm, address);
319 	if (pgd_none(*pgdp))
320 		goto out_irqs_on;
321 	p4dp = p4d_offset(pgdp, address);
322 	if (p4d_none(*p4dp))
323 		goto out_irqs_on;
324 	pudp = pud_offset(p4dp, address);
325 	if (pud_none(*pudp))
326 		goto out_irqs_on;
327 	pmdp = pmd_offset(pudp, address);
328 	if (pmd_none(*pmdp))
329 		goto out_irqs_on;
330 
331 	ptep = pte_offset_map(pmdp, address);
332 	pte = *ptep;
333 	if (!pte_present(pte))
334 		goto out_unmap;
335 
336 	paddr = (unsigned long) page_address(pte_page(pte));
337 
338 	__asm__ __volatile__("flush	%0 + %1"
339 			     : /* no outputs */
340 			     : "r" (paddr),
341 			       "r" (address & (PAGE_SIZE - 1))
342 			     : "memory");
343 
344 out_unmap:
345 	pte_unmap(ptep);
346 out_irqs_on:
347 	__asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
348 
349 }
350 
351 static int setup_frame32(struct ksignal *ksig, struct pt_regs *regs,
352 			 sigset_t *oldset)
353 {
354 	struct signal_frame32 __user *sf;
355 	int i, err, wsaved;
356 	void __user *tail;
357 	int sigframe_size;
358 	u32 psr;
359 	compat_sigset_t seta;
360 
361 	/* 1. Make sure everything is clean */
362 	synchronize_user_stack();
363 	save_and_clear_fpu();
364 
365 	wsaved = get_thread_wsaved();
366 
367 	sigframe_size = sizeof(*sf);
368 	if (current_thread_info()->fpsaved[0] & FPRS_FEF)
369 		sigframe_size += sizeof(__siginfo_fpu_t);
370 	if (wsaved)
371 		sigframe_size += sizeof(__siginfo_rwin_t);
372 
373 	sf = (struct signal_frame32 __user *)
374 		get_sigframe(ksig, regs, sigframe_size);
375 
376 	if (invalid_frame_pointer(sf, sigframe_size)) {
377 		if (show_unhandled_signals)
378 			pr_info("%s[%d] bad frame in setup_frame32: %08lx TPC %08lx O7 %08lx\n",
379 				current->comm, current->pid, (unsigned long)sf,
380 				regs->tpc, regs->u_regs[UREG_I7]);
381 		force_sigsegv(ksig->sig);
382 		return -EINVAL;
383 	}
384 
385 	tail = (sf + 1);
386 
387 	/* 2. Save the current process state */
388 	if (test_thread_flag(TIF_32BIT)) {
389 		regs->tpc &= 0xffffffff;
390 		regs->tnpc &= 0xffffffff;
391 	}
392 	err  = put_user(regs->tpc, &sf->info.si_regs.pc);
393 	err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
394 	err |= __put_user(regs->y, &sf->info.si_regs.y);
395 	psr = tstate_to_psr(regs->tstate);
396 	if (current_thread_info()->fpsaved[0] & FPRS_FEF)
397 		psr |= PSR_EF;
398 	err |= __put_user(psr, &sf->info.si_regs.psr);
399 	for (i = 0; i < 16; i++)
400 		err |= __put_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
401 	err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
402 	err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
403 	for (i = 1; i < 16; i++)
404 		err |= __put_user(((u32 *)regs->u_regs)[2*i],
405 				  &sf->v8plus.g_upper[i]);
406 	err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
407 			  &sf->v8plus.asi);
408 
409 	if (psr & PSR_EF) {
410 		__siginfo_fpu_t __user *fp = tail;
411 		tail += sizeof(*fp);
412 		err |= save_fpu_state(regs, fp);
413 		err |= __put_user((u64)fp, &sf->fpu_save);
414 	} else {
415 		err |= __put_user(0, &sf->fpu_save);
416 	}
417 	if (wsaved) {
418 		__siginfo_rwin_t __user *rwp = tail;
419 		tail += sizeof(*rwp);
420 		err |= save_rwin_state(wsaved, rwp);
421 		err |= __put_user((u64)rwp, &sf->rwin_save);
422 		set_thread_wsaved(0);
423 	} else {
424 		err |= __put_user(0, &sf->rwin_save);
425 	}
426 
427 	/* If these change we need to know - assignments to seta relies on these sizes */
428 	BUILD_BUG_ON(_NSIG_WORDS != 1);
429 	BUILD_BUG_ON(_COMPAT_NSIG_WORDS != 2);
430 	seta.sig[1] = (oldset->sig[0] >> 32);
431 	seta.sig[0] = oldset->sig[0];
432 
433 	err |= __put_user(seta.sig[0], &sf->info.si_mask);
434 	err |= __copy_to_user(sf->extramask, &seta.sig[1],
435 			      (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
436 
437 	if (!wsaved) {
438 		err |= raw_copy_in_user((u32 __user *)sf,
439 					(u32 __user *)(regs->u_regs[UREG_FP]),
440 					sizeof(struct reg_window32));
441 	} else {
442 		struct reg_window *rp;
443 
444 		rp = &current_thread_info()->reg_window[wsaved - 1];
445 		for (i = 0; i < 8; i++)
446 			err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
447 		for (i = 0; i < 6; i++)
448 			err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
449 		err |= __put_user(rp->ins[6], &sf->ss.fp);
450 		err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
451 	}
452 	if (err)
453 		return err;
454 
455 	/* 3. signal handler back-trampoline and parameters */
456 	regs->u_regs[UREG_FP] = (unsigned long) sf;
457 	regs->u_regs[UREG_I0] = ksig->sig;
458 	regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
459 	regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
460 
461 	/* 4. signal handler */
462 	regs->tpc = (unsigned long) ksig->ka.sa.sa_handler;
463 	regs->tnpc = (regs->tpc + 4);
464 	if (test_thread_flag(TIF_32BIT)) {
465 		regs->tpc &= 0xffffffff;
466 		regs->tnpc &= 0xffffffff;
467 	}
468 
469 	/* 5. return to kernel instructions */
470 	if (ksig->ka.ka_restorer) {
471 		regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
472 	} else {
473 		unsigned long address = ((unsigned long)&(sf->insns[0]));
474 
475 		regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
476 
477 		err  = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
478 		err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
479 		if (err)
480 			return err;
481 		flush_signal_insns(address);
482 	}
483 	return 0;
484 }
485 
486 static int setup_rt_frame32(struct ksignal *ksig, struct pt_regs *regs,
487 			    sigset_t *oldset)
488 {
489 	struct rt_signal_frame32 __user *sf;
490 	int i, err, wsaved;
491 	void __user *tail;
492 	int sigframe_size;
493 	u32 psr;
494 
495 	/* 1. Make sure everything is clean */
496 	synchronize_user_stack();
497 	save_and_clear_fpu();
498 
499 	wsaved = get_thread_wsaved();
500 
501 	sigframe_size = sizeof(*sf);
502 	if (current_thread_info()->fpsaved[0] & FPRS_FEF)
503 		sigframe_size += sizeof(__siginfo_fpu_t);
504 	if (wsaved)
505 		sigframe_size += sizeof(__siginfo_rwin_t);
506 
507 	sf = (struct rt_signal_frame32 __user *)
508 		get_sigframe(ksig, regs, sigframe_size);
509 
510 	if (invalid_frame_pointer(sf, sigframe_size)) {
511 		if (show_unhandled_signals)
512 			pr_info("%s[%d] bad frame in setup_rt_frame32: %08lx TPC %08lx O7 %08lx\n",
513 				current->comm, current->pid, (unsigned long)sf,
514 				regs->tpc, regs->u_regs[UREG_I7]);
515 		force_sigsegv(ksig->sig);
516 		return -EINVAL;
517 	}
518 
519 	tail = (sf + 1);
520 
521 	/* 2. Save the current process state */
522 	if (test_thread_flag(TIF_32BIT)) {
523 		regs->tpc &= 0xffffffff;
524 		regs->tnpc &= 0xffffffff;
525 	}
526 	err  = put_user(regs->tpc, &sf->regs.pc);
527 	err |= __put_user(regs->tnpc, &sf->regs.npc);
528 	err |= __put_user(regs->y, &sf->regs.y);
529 	psr = tstate_to_psr(regs->tstate);
530 	if (current_thread_info()->fpsaved[0] & FPRS_FEF)
531 		psr |= PSR_EF;
532 	err |= __put_user(psr, &sf->regs.psr);
533 	for (i = 0; i < 16; i++)
534 		err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
535 	err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
536 	err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
537 	for (i = 1; i < 16; i++)
538 		err |= __put_user(((u32 *)regs->u_regs)[2*i],
539 				  &sf->v8plus.g_upper[i]);
540 	err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
541 			  &sf->v8plus.asi);
542 
543 	if (psr & PSR_EF) {
544 		__siginfo_fpu_t __user *fp = tail;
545 		tail += sizeof(*fp);
546 		err |= save_fpu_state(regs, fp);
547 		err |= __put_user((u64)fp, &sf->fpu_save);
548 	} else {
549 		err |= __put_user(0, &sf->fpu_save);
550 	}
551 	if (wsaved) {
552 		__siginfo_rwin_t __user *rwp = tail;
553 		tail += sizeof(*rwp);
554 		err |= save_rwin_state(wsaved, rwp);
555 		err |= __put_user((u64)rwp, &sf->rwin_save);
556 		set_thread_wsaved(0);
557 	} else {
558 		err |= __put_user(0, &sf->rwin_save);
559 	}
560 
561 	/* Update the siginfo structure.  */
562 	err |= copy_siginfo_to_user32(&sf->info, &ksig->info);
563 
564 	/* Setup sigaltstack */
565 	err |= __compat_save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
566 
567 	err |= put_compat_sigset(&sf->mask, oldset, sizeof(compat_sigset_t));
568 
569 	if (!wsaved) {
570 		err |= raw_copy_in_user((u32 __user *)sf,
571 					(u32 __user *)(regs->u_regs[UREG_FP]),
572 					sizeof(struct reg_window32));
573 	} else {
574 		struct reg_window *rp;
575 
576 		rp = &current_thread_info()->reg_window[wsaved - 1];
577 		for (i = 0; i < 8; i++)
578 			err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
579 		for (i = 0; i < 6; i++)
580 			err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
581 		err |= __put_user(rp->ins[6], &sf->ss.fp);
582 		err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
583 	}
584 	if (err)
585 		return err;
586 
587 	/* 3. signal handler back-trampoline and parameters */
588 	regs->u_regs[UREG_FP] = (unsigned long) sf;
589 	regs->u_regs[UREG_I0] = ksig->sig;
590 	regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
591 	regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
592 
593 	/* 4. signal handler */
594 	regs->tpc = (unsigned long) ksig->ka.sa.sa_handler;
595 	regs->tnpc = (regs->tpc + 4);
596 	if (test_thread_flag(TIF_32BIT)) {
597 		regs->tpc &= 0xffffffff;
598 		regs->tnpc &= 0xffffffff;
599 	}
600 
601 	/* 5. return to kernel instructions */
602 	if (ksig->ka.ka_restorer)
603 		regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
604 	else {
605 		unsigned long address = ((unsigned long)&(sf->insns[0]));
606 
607 		regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
608 
609 		/* mov __NR_rt_sigreturn, %g1 */
610 		err |= __put_user(0x82102065, &sf->insns[0]);
611 
612 		/* t 0x10 */
613 		err |= __put_user(0x91d02010, &sf->insns[1]);
614 		if (err)
615 			return err;
616 
617 		flush_signal_insns(address);
618 	}
619 	return 0;
620 }
621 
622 static inline void handle_signal32(struct ksignal *ksig,
623 				  struct pt_regs *regs)
624 {
625 	sigset_t *oldset = sigmask_to_save();
626 	int err;
627 
628 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
629 		err = setup_rt_frame32(ksig, regs, oldset);
630 	else
631 		err = setup_frame32(ksig, regs, oldset);
632 
633 	signal_setup_done(err, ksig, 0);
634 }
635 
636 static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
637 				     struct sigaction *sa)
638 {
639 	switch (regs->u_regs[UREG_I0]) {
640 	case ERESTART_RESTARTBLOCK:
641 	case ERESTARTNOHAND:
642 	no_system_call_restart:
643 		regs->u_regs[UREG_I0] = EINTR;
644 		regs->tstate |= TSTATE_ICARRY;
645 		break;
646 	case ERESTARTSYS:
647 		if (!(sa->sa_flags & SA_RESTART))
648 			goto no_system_call_restart;
649 		fallthrough;
650 	case ERESTARTNOINTR:
651 		regs->u_regs[UREG_I0] = orig_i0;
652 		regs->tpc -= 4;
653 		regs->tnpc -= 4;
654 	}
655 }
656 
657 /* Note that 'init' is a special process: it doesn't get signals it doesn't
658  * want to handle. Thus you cannot kill init even with a SIGKILL even by
659  * mistake.
660  */
661 void do_signal32(struct pt_regs * regs)
662 {
663 	struct ksignal ksig;
664 	unsigned long orig_i0 = 0;
665 	int restart_syscall = 0;
666 	bool has_handler = get_signal(&ksig);
667 
668 	if (pt_regs_is_syscall(regs) &&
669 	    (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
670 		restart_syscall = 1;
671 		orig_i0 = regs->u_regs[UREG_G6];
672 	}
673 
674 	if (has_handler) {
675 		if (restart_syscall)
676 			syscall_restart32(orig_i0, regs, &ksig.ka.sa);
677 		handle_signal32(&ksig, regs);
678 	} else {
679 		if (restart_syscall) {
680 			switch (regs->u_regs[UREG_I0]) {
681 			case ERESTARTNOHAND:
682 	     		case ERESTARTSYS:
683 			case ERESTARTNOINTR:
684 				/* replay the system call when we are done */
685 				regs->u_regs[UREG_I0] = orig_i0;
686 				regs->tpc -= 4;
687 				regs->tnpc -= 4;
688 				pt_regs_clear_syscall(regs);
689 				fallthrough;
690 			case ERESTART_RESTARTBLOCK:
691 				regs->u_regs[UREG_G1] = __NR_restart_syscall;
692 				regs->tpc -= 4;
693 				regs->tnpc -= 4;
694 				pt_regs_clear_syscall(regs);
695 			}
696 		}
697 		restore_saved_sigmask();
698 	}
699 }
700 
701 struct sigstack32 {
702 	u32 the_stack;
703 	int cur_status;
704 };
705 
706 asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
707 {
708 	struct sigstack32 __user *ssptr =
709 		(struct sigstack32 __user *)((unsigned long)(u_ssptr));
710 	struct sigstack32 __user *ossptr =
711 		(struct sigstack32 __user *)((unsigned long)(u_ossptr));
712 	int ret = -EFAULT;
713 
714 	/* First see if old state is wanted. */
715 	if (ossptr) {
716 		if (put_user(current->sas_ss_sp + current->sas_ss_size,
717 			     &ossptr->the_stack) ||
718 		    __put_user(on_sig_stack(sp), &ossptr->cur_status))
719 			goto out;
720 	}
721 
722 	/* Now see if we want to update the new state. */
723 	if (ssptr) {
724 		u32 ss_sp;
725 
726 		if (get_user(ss_sp, &ssptr->the_stack))
727 			goto out;
728 
729 		/* If the current stack was set with sigaltstack, don't
730 		 * swap stacks while we are on it.
731 		 */
732 		ret = -EPERM;
733 		if (current->sas_ss_sp && on_sig_stack(sp))
734 			goto out;
735 
736 		/* Since we don't know the extent of the stack, and we don't
737 		 * track onstack-ness, but rather calculate it, we must
738 		 * presume a size.  Ho hum this interface is lossy.
739 		 */
740 		current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
741 		current->sas_ss_size = SIGSTKSZ;
742 	}
743 
744 	ret = 0;
745 out:
746 	return ret;
747 }
748 
749 /*
750  * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
751  * changes likely come with new fields that should be added below.
752  */
753 static_assert(NSIGILL	== 11);
754 static_assert(NSIGFPE	== 15);
755 static_assert(NSIGSEGV	== 9);
756 static_assert(NSIGBUS	== 5);
757 static_assert(NSIGTRAP	== 6);
758 static_assert(NSIGCHLD	== 6);
759 static_assert(NSIGSYS	== 2);
760 static_assert(sizeof(compat_siginfo_t) == 128);
761 static_assert(__alignof__(compat_siginfo_t) == 4);
762 static_assert(offsetof(compat_siginfo_t, si_signo)	== 0x00);
763 static_assert(offsetof(compat_siginfo_t, si_errno)	== 0x04);
764 static_assert(offsetof(compat_siginfo_t, si_code)	== 0x08);
765 static_assert(offsetof(compat_siginfo_t, si_pid)	== 0x0c);
766 static_assert(offsetof(compat_siginfo_t, si_uid)	== 0x10);
767 static_assert(offsetof(compat_siginfo_t, si_tid)	== 0x0c);
768 static_assert(offsetof(compat_siginfo_t, si_overrun)	== 0x10);
769 static_assert(offsetof(compat_siginfo_t, si_status)	== 0x14);
770 static_assert(offsetof(compat_siginfo_t, si_utime)	== 0x18);
771 static_assert(offsetof(compat_siginfo_t, si_stime)	== 0x1c);
772 static_assert(offsetof(compat_siginfo_t, si_value)	== 0x14);
773 static_assert(offsetof(compat_siginfo_t, si_int)	== 0x14);
774 static_assert(offsetof(compat_siginfo_t, si_ptr)	== 0x14);
775 static_assert(offsetof(compat_siginfo_t, si_addr)	== 0x0c);
776 static_assert(offsetof(compat_siginfo_t, si_trapno)	== 0x10);
777 static_assert(offsetof(compat_siginfo_t, si_addr_lsb)	== 0x10);
778 static_assert(offsetof(compat_siginfo_t, si_lower)	== 0x14);
779 static_assert(offsetof(compat_siginfo_t, si_upper)	== 0x18);
780 static_assert(offsetof(compat_siginfo_t, si_pkey)	== 0x14);
781 static_assert(offsetof(compat_siginfo_t, si_perf_data)	== 0x10);
782 static_assert(offsetof(compat_siginfo_t, si_perf_type)	== 0x14);
783 static_assert(offsetof(compat_siginfo_t, si_band)	== 0x0c);
784 static_assert(offsetof(compat_siginfo_t, si_fd)		== 0x10);
785