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