xref: /openbmc/linux/arch/sparc/kernel/signal32.c (revision 2d6bed9c)
1 /*  arch/sparc64/kernel/signal32.c
2  *
3  *  Copyright (C) 1991, 1992  Linus Torvalds
4  *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  *  Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6  *  Copyright (C) 1997 Eddie C. Dost   (ecd@skynet.be)
7  *  Copyright (C) 1997,1998 Jakub Jelinek   (jj@sunsite.mff.cuni.cz)
8  */
9 
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>
17 #include <linux/mm.h>
18 #include <linux/tty.h>
19 #include <linux/binfmts.h>
20 #include <linux/compat.h>
21 #include <linux/bitops.h>
22 #include <linux/tracehook.h>
23 
24 #include <asm/uaccess.h>
25 #include <asm/ptrace.h>
26 #include <asm/pgtable.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 
35 /* This magic should be in g_upper[0] for all upper parts
36  * to be valid.
37  */
38 #define SIGINFO_EXTRA_V8PLUS_MAGIC	0x130e269
39 typedef struct {
40 	unsigned int g_upper[8];
41 	unsigned int o_upper[8];
42 	unsigned int asi;
43 } siginfo_extra_v8plus_t;
44 
45 struct signal_frame32 {
46 	struct sparc_stackf32	ss;
47 	__siginfo32_t		info;
48 	/* __siginfo_fpu_t * */ u32 fpu_save;
49 	unsigned int		insns[2];
50 	unsigned int		extramask[_COMPAT_NSIG_WORDS - 1];
51 	unsigned int		extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
52 	/* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
53 	siginfo_extra_v8plus_t	v8plus;
54 	/* __siginfo_rwin_t * */u32 rwin_save;
55 } __attribute__((aligned(8)));
56 
57 struct rt_signal_frame32 {
58 	struct sparc_stackf32	ss;
59 	compat_siginfo_t	info;
60 	struct pt_regs32	regs;
61 	compat_sigset_t		mask;
62 	/* __siginfo_fpu_t * */ u32 fpu_save;
63 	unsigned int		insns[2];
64 	stack_t32		stack;
65 	unsigned int		extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
66 	/* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
67 	siginfo_extra_v8plus_t	v8plus;
68 	/* __siginfo_rwin_t * */u32 rwin_save;
69 } __attribute__((aligned(8)));
70 
71 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
72 {
73 	int err;
74 
75 	if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
76 		return -EFAULT;
77 
78 	/* If you change siginfo_t structure, please be sure
79 	   this code is fixed accordingly.
80 	   It should never copy any pad contained in the structure
81 	   to avoid security leaks, but must copy the generic
82 	   3 ints plus the relevant union member.
83 	   This routine must convert siginfo from 64bit to 32bit as well
84 	   at the same time.  */
85 	err = __put_user(from->si_signo, &to->si_signo);
86 	err |= __put_user(from->si_errno, &to->si_errno);
87 	err |= __put_user((short)from->si_code, &to->si_code);
88 	if (from->si_code < 0)
89 		err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
90 	else {
91 		switch (from->si_code >> 16) {
92 		case __SI_TIMER >> 16:
93 			err |= __put_user(from->si_tid, &to->si_tid);
94 			err |= __put_user(from->si_overrun, &to->si_overrun);
95 			err |= __put_user(from->si_int, &to->si_int);
96 			break;
97 		case __SI_CHLD >> 16:
98 			err |= __put_user(from->si_utime, &to->si_utime);
99 			err |= __put_user(from->si_stime, &to->si_stime);
100 			err |= __put_user(from->si_status, &to->si_status);
101 		default:
102 			err |= __put_user(from->si_pid, &to->si_pid);
103 			err |= __put_user(from->si_uid, &to->si_uid);
104 			break;
105 		case __SI_FAULT >> 16:
106 			err |= __put_user(from->si_trapno, &to->si_trapno);
107 			err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
108 			break;
109 		case __SI_POLL >> 16:
110 			err |= __put_user(from->si_band, &to->si_band);
111 			err |= __put_user(from->si_fd, &to->si_fd);
112 			break;
113 		case __SI_RT >> 16: /* This is not generated by the kernel as of now.  */
114 		case __SI_MESGQ >> 16:
115 			err |= __put_user(from->si_pid, &to->si_pid);
116 			err |= __put_user(from->si_uid, &to->si_uid);
117 			err |= __put_user(from->si_int, &to->si_int);
118 			break;
119 		}
120 	}
121 	return err;
122 }
123 
124 /* CAUTION: This is just a very minimalist implementation for the
125  *          sake of compat_sys_rt_sigqueueinfo()
126  */
127 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
128 {
129 	if (!access_ok(VERIFY_WRITE, from, sizeof(compat_siginfo_t)))
130 		return -EFAULT;
131 
132 	if (copy_from_user(to, from, 3*sizeof(int)) ||
133 	    copy_from_user(to->_sifields._pad, from->_sifields._pad,
134 			   SI_PAD_SIZE))
135 		return -EFAULT;
136 
137 	return 0;
138 }
139 
140 void do_sigreturn32(struct pt_regs *regs)
141 {
142 	struct signal_frame32 __user *sf;
143 	compat_uptr_t fpu_save;
144 	compat_uptr_t rwin_save;
145 	unsigned int psr;
146 	unsigned pc, npc;
147 	sigset_t set;
148 	unsigned seta[_COMPAT_NSIG_WORDS];
149 	int err, i;
150 
151 	/* Always make any pending restarted system calls return -EINTR */
152 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
153 
154 	synchronize_user_stack();
155 
156 	regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
157 	sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
158 
159 	/* 1. Make sure we are not getting garbage from the user */
160 	if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
161 	    (((unsigned long) sf) & 3))
162 		goto segv;
163 
164 	if (get_user(pc, &sf->info.si_regs.pc) ||
165 	    __get_user(npc, &sf->info.si_regs.npc))
166 		goto segv;
167 
168 	if ((pc | npc) & 3)
169 		goto segv;
170 
171 	if (test_thread_flag(TIF_32BIT)) {
172 		pc &= 0xffffffff;
173 		npc &= 0xffffffff;
174 	}
175 	regs->tpc = pc;
176 	regs->tnpc = npc;
177 
178 	/* 2. Restore the state */
179 	err = __get_user(regs->y, &sf->info.si_regs.y);
180 	err |= __get_user(psr, &sf->info.si_regs.psr);
181 
182 	for (i = UREG_G1; i <= UREG_I7; i++)
183 		err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
184 	if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
185 		err |= __get_user(i, &sf->v8plus.g_upper[0]);
186 		if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
187 			unsigned long asi;
188 
189 			for (i = UREG_G1; i <= UREG_I7; i++)
190 				err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
191 			err |= __get_user(asi, &sf->v8plus.asi);
192 			regs->tstate &= ~TSTATE_ASI;
193 			regs->tstate |= ((asi & 0xffUL) << 24UL);
194 		}
195 	}
196 
197 	/* User can only change condition codes in %tstate. */
198 	regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
199 	regs->tstate |= psr_to_tstate_icc(psr);
200 
201 	/* Prevent syscall restart.  */
202 	pt_regs_clear_syscall(regs);
203 
204 	err |= __get_user(fpu_save, &sf->fpu_save);
205 	if (!err && fpu_save)
206 		err |= restore_fpu_state(regs, compat_ptr(fpu_save));
207 	err |= __get_user(rwin_save, &sf->rwin_save);
208 	if (!err && rwin_save) {
209 		if (restore_rwin_state(compat_ptr(rwin_save)))
210 			goto segv;
211 	}
212 	err |= __get_user(seta[0], &sf->info.si_mask);
213 	err |= copy_from_user(seta+1, &sf->extramask,
214 			      (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
215 	if (err)
216 	    	goto segv;
217 	switch (_NSIG_WORDS) {
218 		case 4: set.sig[3] = seta[6] + (((long)seta[7]) << 32);
219 		case 3: set.sig[2] = seta[4] + (((long)seta[5]) << 32);
220 		case 2: set.sig[1] = seta[2] + (((long)seta[3]) << 32);
221 		case 1: set.sig[0] = seta[0] + (((long)seta[1]) << 32);
222 	}
223 	set_current_blocked(&set);
224 	return;
225 
226 segv:
227 	force_sig(SIGSEGV, current);
228 }
229 
230 asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
231 {
232 	struct rt_signal_frame32 __user *sf;
233 	unsigned int psr, pc, npc, u_ss_sp;
234 	compat_uptr_t fpu_save;
235 	compat_uptr_t rwin_save;
236 	mm_segment_t old_fs;
237 	sigset_t set;
238 	compat_sigset_t seta;
239 	stack_t st;
240 	int err, i;
241 
242 	/* Always make any pending restarted system calls return -EINTR */
243 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
244 
245 	synchronize_user_stack();
246 	regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
247 	sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
248 
249 	/* 1. Make sure we are not getting garbage from the user */
250 	if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
251 	    (((unsigned long) sf) & 3))
252 		goto segv;
253 
254 	if (get_user(pc, &sf->regs.pc) ||
255 	    __get_user(npc, &sf->regs.npc))
256 		goto segv;
257 
258 	if ((pc | npc) & 3)
259 		goto segv;
260 
261 	if (test_thread_flag(TIF_32BIT)) {
262 		pc &= 0xffffffff;
263 		npc &= 0xffffffff;
264 	}
265 	regs->tpc = pc;
266 	regs->tnpc = npc;
267 
268 	/* 2. Restore the state */
269 	err = __get_user(regs->y, &sf->regs.y);
270 	err |= __get_user(psr, &sf->regs.psr);
271 
272 	for (i = UREG_G1; i <= UREG_I7; i++)
273 		err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
274 	if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
275 		err |= __get_user(i, &sf->v8plus.g_upper[0]);
276 		if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
277 			unsigned long asi;
278 
279 			for (i = UREG_G1; i <= UREG_I7; i++)
280 				err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
281 			err |= __get_user(asi, &sf->v8plus.asi);
282 			regs->tstate &= ~TSTATE_ASI;
283 			regs->tstate |= ((asi & 0xffUL) << 24UL);
284 		}
285 	}
286 
287 	/* User can only change condition codes in %tstate. */
288 	regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
289 	regs->tstate |= psr_to_tstate_icc(psr);
290 
291 	/* Prevent syscall restart.  */
292 	pt_regs_clear_syscall(regs);
293 
294 	err |= __get_user(fpu_save, &sf->fpu_save);
295 	if (!err && fpu_save)
296 		err |= restore_fpu_state(regs, compat_ptr(fpu_save));
297 	err |= copy_from_user(&seta, &sf->mask, sizeof(compat_sigset_t));
298 	err |= __get_user(u_ss_sp, &sf->stack.ss_sp);
299 	st.ss_sp = compat_ptr(u_ss_sp);
300 	err |= __get_user(st.ss_flags, &sf->stack.ss_flags);
301 	err |= __get_user(st.ss_size, &sf->stack.ss_size);
302 	if (err)
303 		goto segv;
304 
305 	/* It is more difficult to avoid calling this function than to
306 	   call it and ignore errors.  */
307 	old_fs = get_fs();
308 	set_fs(KERNEL_DS);
309 	do_sigaltstack((stack_t __user *) &st, NULL, (unsigned long)sf);
310 	set_fs(old_fs);
311 
312 	err |= __get_user(rwin_save, &sf->rwin_save);
313 	if (!err && rwin_save) {
314 		if (restore_rwin_state(compat_ptr(rwin_save)))
315 			goto segv;
316 	}
317 
318 	switch (_NSIG_WORDS) {
319 		case 4: set.sig[3] = seta.sig[6] + (((long)seta.sig[7]) << 32);
320 		case 3: set.sig[2] = seta.sig[4] + (((long)seta.sig[5]) << 32);
321 		case 2: set.sig[1] = seta.sig[2] + (((long)seta.sig[3]) << 32);
322 		case 1: set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
323 	}
324 	set_current_blocked(&set);
325 	return;
326 segv:
327 	force_sig(SIGSEGV, current);
328 }
329 
330 /* Checks if the fp is valid */
331 static int invalid_frame_pointer(void __user *fp, int fplen)
332 {
333 	if ((((unsigned long) fp) & 7) || ((unsigned long)fp) > 0x100000000ULL - fplen)
334 		return 1;
335 	return 0;
336 }
337 
338 static void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
339 {
340 	unsigned long sp;
341 
342 	regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
343 	sp = regs->u_regs[UREG_FP];
344 
345 	/*
346 	 * If we are on the alternate signal stack and would overflow it, don't.
347 	 * Return an always-bogus address instead so we will die with SIGSEGV.
348 	 */
349 	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
350 		return (void __user *) -1L;
351 
352 	/* This is the X/Open sanctioned signal stack switching.  */
353 	if (sa->sa_flags & SA_ONSTACK) {
354 		if (sas_ss_flags(sp) == 0)
355 			sp = current->sas_ss_sp + current->sas_ss_size;
356 	}
357 
358 	sp -= framesize;
359 
360 	/* Always align the stack frame.  This handles two cases.  First,
361 	 * sigaltstack need not be mindful of platform specific stack
362 	 * alignment.  Second, if we took this signal because the stack
363 	 * is not aligned properly, we'd like to take the signal cleanly
364 	 * and report that.
365 	 */
366 	sp &= ~15UL;
367 
368 	return (void __user *) sp;
369 }
370 
371 /* The I-cache flush instruction only works in the primary ASI, which
372  * right now is the nucleus, aka. kernel space.
373  *
374  * Therefore we have to kick the instructions out using the kernel
375  * side linear mapping of the physical address backing the user
376  * instructions.
377  */
378 static void flush_signal_insns(unsigned long address)
379 {
380 	unsigned long pstate, paddr;
381 	pte_t *ptep, pte;
382 	pgd_t *pgdp;
383 	pud_t *pudp;
384 	pmd_t *pmdp;
385 
386 	/* Commit all stores of the instructions we are about to flush.  */
387 	wmb();
388 
389 	/* Disable cross-call reception.  In this way even a very wide
390 	 * munmap() on another cpu can't tear down the page table
391 	 * hierarchy from underneath us, since that can't complete
392 	 * until the IPI tlb flush returns.
393 	 */
394 
395 	__asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
396 	__asm__ __volatile__("wrpr %0, %1, %%pstate"
397 				: : "r" (pstate), "i" (PSTATE_IE));
398 
399 	pgdp = pgd_offset(current->mm, address);
400 	if (pgd_none(*pgdp))
401 		goto out_irqs_on;
402 	pudp = pud_offset(pgdp, address);
403 	if (pud_none(*pudp))
404 		goto out_irqs_on;
405 	pmdp = pmd_offset(pudp, address);
406 	if (pmd_none(*pmdp))
407 		goto out_irqs_on;
408 
409 	ptep = pte_offset_map(pmdp, address);
410 	pte = *ptep;
411 	if (!pte_present(pte))
412 		goto out_unmap;
413 
414 	paddr = (unsigned long) page_address(pte_page(pte));
415 
416 	__asm__ __volatile__("flush	%0 + %1"
417 			     : /* no outputs */
418 			     : "r" (paddr),
419 			       "r" (address & (PAGE_SIZE - 1))
420 			     : "memory");
421 
422 out_unmap:
423 	pte_unmap(ptep);
424 out_irqs_on:
425 	__asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
426 
427 }
428 
429 static int setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
430 			 int signo, sigset_t *oldset)
431 {
432 	struct signal_frame32 __user *sf;
433 	int i, err, wsaved;
434 	void __user *tail;
435 	int sigframe_size;
436 	u32 psr;
437 	unsigned int seta[_COMPAT_NSIG_WORDS];
438 
439 	/* 1. Make sure everything is clean */
440 	synchronize_user_stack();
441 	save_and_clear_fpu();
442 
443 	wsaved = get_thread_wsaved();
444 
445 	sigframe_size = sizeof(*sf);
446 	if (current_thread_info()->fpsaved[0] & FPRS_FEF)
447 		sigframe_size += sizeof(__siginfo_fpu_t);
448 	if (wsaved)
449 		sigframe_size += sizeof(__siginfo_rwin_t);
450 
451 	sf = (struct signal_frame32 __user *)
452 		get_sigframe(&ka->sa, regs, sigframe_size);
453 
454 	if (invalid_frame_pointer(sf, sigframe_size))
455 		goto sigill;
456 
457 	tail = (sf + 1);
458 
459 	/* 2. Save the current process state */
460 	if (test_thread_flag(TIF_32BIT)) {
461 		regs->tpc &= 0xffffffff;
462 		regs->tnpc &= 0xffffffff;
463 	}
464 	err  = put_user(regs->tpc, &sf->info.si_regs.pc);
465 	err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
466 	err |= __put_user(regs->y, &sf->info.si_regs.y);
467 	psr = tstate_to_psr(regs->tstate);
468 	if (current_thread_info()->fpsaved[0] & FPRS_FEF)
469 		psr |= PSR_EF;
470 	err |= __put_user(psr, &sf->info.si_regs.psr);
471 	for (i = 0; i < 16; i++)
472 		err |= __put_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
473 	err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
474 	err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
475 	for (i = 1; i < 16; i++)
476 		err |= __put_user(((u32 *)regs->u_regs)[2*i],
477 				  &sf->v8plus.g_upper[i]);
478 	err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
479 			  &sf->v8plus.asi);
480 
481 	if (psr & PSR_EF) {
482 		__siginfo_fpu_t __user *fp = tail;
483 		tail += sizeof(*fp);
484 		err |= save_fpu_state(regs, fp);
485 		err |= __put_user((u64)fp, &sf->fpu_save);
486 	} else {
487 		err |= __put_user(0, &sf->fpu_save);
488 	}
489 	if (wsaved) {
490 		__siginfo_rwin_t __user *rwp = tail;
491 		tail += sizeof(*rwp);
492 		err |= save_rwin_state(wsaved, rwp);
493 		err |= __put_user((u64)rwp, &sf->rwin_save);
494 		set_thread_wsaved(0);
495 	} else {
496 		err |= __put_user(0, &sf->rwin_save);
497 	}
498 
499 	switch (_NSIG_WORDS) {
500 	case 4: seta[7] = (oldset->sig[3] >> 32);
501 	        seta[6] = oldset->sig[3];
502 	case 3: seta[5] = (oldset->sig[2] >> 32);
503 	        seta[4] = oldset->sig[2];
504 	case 2: seta[3] = (oldset->sig[1] >> 32);
505 	        seta[2] = oldset->sig[1];
506 	case 1: seta[1] = (oldset->sig[0] >> 32);
507 	        seta[0] = oldset->sig[0];
508 	}
509 	err |= __put_user(seta[0], &sf->info.si_mask);
510 	err |= __copy_to_user(sf->extramask, seta + 1,
511 			      (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
512 
513 	if (!wsaved) {
514 		err |= copy_in_user((u32 __user *)sf,
515 				    (u32 __user *)(regs->u_regs[UREG_FP]),
516 				    sizeof(struct reg_window32));
517 	} else {
518 		struct reg_window *rp;
519 
520 		rp = &current_thread_info()->reg_window[wsaved - 1];
521 		for (i = 0; i < 8; i++)
522 			err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
523 		for (i = 0; i < 6; i++)
524 			err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
525 		err |= __put_user(rp->ins[6], &sf->ss.fp);
526 		err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
527 	}
528 	if (err)
529 		goto sigsegv;
530 
531 	/* 3. signal handler back-trampoline and parameters */
532 	regs->u_regs[UREG_FP] = (unsigned long) sf;
533 	regs->u_regs[UREG_I0] = signo;
534 	regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
535 	regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
536 
537 	/* 4. signal handler */
538 	regs->tpc = (unsigned long) ka->sa.sa_handler;
539 	regs->tnpc = (regs->tpc + 4);
540 	if (test_thread_flag(TIF_32BIT)) {
541 		regs->tpc &= 0xffffffff;
542 		regs->tnpc &= 0xffffffff;
543 	}
544 
545 	/* 5. return to kernel instructions */
546 	if (ka->ka_restorer) {
547 		regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
548 	} else {
549 		unsigned long address = ((unsigned long)&(sf->insns[0]));
550 
551 		regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
552 
553 		err  = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
554 		err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
555 		if (err)
556 			goto sigsegv;
557 		flush_signal_insns(address);
558 	}
559 	return 0;
560 
561 sigill:
562 	do_exit(SIGILL);
563 	return -EINVAL;
564 
565 sigsegv:
566 	force_sigsegv(signo, current);
567 	return -EFAULT;
568 }
569 
570 static int setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
571 			    unsigned long signr, sigset_t *oldset,
572 			    siginfo_t *info)
573 {
574 	struct rt_signal_frame32 __user *sf;
575 	int i, err, wsaved;
576 	void __user *tail;
577 	int sigframe_size;
578 	u32 psr;
579 	compat_sigset_t seta;
580 
581 	/* 1. Make sure everything is clean */
582 	synchronize_user_stack();
583 	save_and_clear_fpu();
584 
585 	wsaved = get_thread_wsaved();
586 
587 	sigframe_size = sizeof(*sf);
588 	if (current_thread_info()->fpsaved[0] & FPRS_FEF)
589 		sigframe_size += sizeof(__siginfo_fpu_t);
590 	if (wsaved)
591 		sigframe_size += sizeof(__siginfo_rwin_t);
592 
593 	sf = (struct rt_signal_frame32 __user *)
594 		get_sigframe(&ka->sa, regs, sigframe_size);
595 
596 	if (invalid_frame_pointer(sf, sigframe_size))
597 		goto sigill;
598 
599 	tail = (sf + 1);
600 
601 	/* 2. Save the current process state */
602 	if (test_thread_flag(TIF_32BIT)) {
603 		regs->tpc &= 0xffffffff;
604 		regs->tnpc &= 0xffffffff;
605 	}
606 	err  = put_user(regs->tpc, &sf->regs.pc);
607 	err |= __put_user(regs->tnpc, &sf->regs.npc);
608 	err |= __put_user(regs->y, &sf->regs.y);
609 	psr = tstate_to_psr(regs->tstate);
610 	if (current_thread_info()->fpsaved[0] & FPRS_FEF)
611 		psr |= PSR_EF;
612 	err |= __put_user(psr, &sf->regs.psr);
613 	for (i = 0; i < 16; i++)
614 		err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
615 	err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
616 	err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
617 	for (i = 1; i < 16; i++)
618 		err |= __put_user(((u32 *)regs->u_regs)[2*i],
619 				  &sf->v8plus.g_upper[i]);
620 	err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
621 			  &sf->v8plus.asi);
622 
623 	if (psr & PSR_EF) {
624 		__siginfo_fpu_t __user *fp = tail;
625 		tail += sizeof(*fp);
626 		err |= save_fpu_state(regs, fp);
627 		err |= __put_user((u64)fp, &sf->fpu_save);
628 	} else {
629 		err |= __put_user(0, &sf->fpu_save);
630 	}
631 	if (wsaved) {
632 		__siginfo_rwin_t __user *rwp = tail;
633 		tail += sizeof(*rwp);
634 		err |= save_rwin_state(wsaved, rwp);
635 		err |= __put_user((u64)rwp, &sf->rwin_save);
636 		set_thread_wsaved(0);
637 	} else {
638 		err |= __put_user(0, &sf->rwin_save);
639 	}
640 
641 	/* Update the siginfo structure.  */
642 	err |= copy_siginfo_to_user32(&sf->info, info);
643 
644 	/* Setup sigaltstack */
645 	err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
646 	err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
647 	err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
648 
649 	switch (_NSIG_WORDS) {
650 	case 4: seta.sig[7] = (oldset->sig[3] >> 32);
651 		seta.sig[6] = oldset->sig[3];
652 	case 3: seta.sig[5] = (oldset->sig[2] >> 32);
653 		seta.sig[4] = oldset->sig[2];
654 	case 2: seta.sig[3] = (oldset->sig[1] >> 32);
655 		seta.sig[2] = oldset->sig[1];
656 	case 1: seta.sig[1] = (oldset->sig[0] >> 32);
657 		seta.sig[0] = oldset->sig[0];
658 	}
659 	err |= __copy_to_user(&sf->mask, &seta, sizeof(compat_sigset_t));
660 
661 	if (!wsaved) {
662 		err |= copy_in_user((u32 __user *)sf,
663 				    (u32 __user *)(regs->u_regs[UREG_FP]),
664 				    sizeof(struct reg_window32));
665 	} else {
666 		struct reg_window *rp;
667 
668 		rp = &current_thread_info()->reg_window[wsaved - 1];
669 		for (i = 0; i < 8; i++)
670 			err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
671 		for (i = 0; i < 6; i++)
672 			err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
673 		err |= __put_user(rp->ins[6], &sf->ss.fp);
674 		err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
675 	}
676 	if (err)
677 		goto sigsegv;
678 
679 	/* 3. signal handler back-trampoline and parameters */
680 	regs->u_regs[UREG_FP] = (unsigned long) sf;
681 	regs->u_regs[UREG_I0] = signr;
682 	regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
683 	regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
684 
685 	/* 4. signal handler */
686 	regs->tpc = (unsigned long) ka->sa.sa_handler;
687 	regs->tnpc = (regs->tpc + 4);
688 	if (test_thread_flag(TIF_32BIT)) {
689 		regs->tpc &= 0xffffffff;
690 		regs->tnpc &= 0xffffffff;
691 	}
692 
693 	/* 5. return to kernel instructions */
694 	if (ka->ka_restorer)
695 		regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
696 	else {
697 		unsigned long address = ((unsigned long)&(sf->insns[0]));
698 
699 		regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
700 
701 		/* mov __NR_rt_sigreturn, %g1 */
702 		err |= __put_user(0x82102065, &sf->insns[0]);
703 
704 		/* t 0x10 */
705 		err |= __put_user(0x91d02010, &sf->insns[1]);
706 		if (err)
707 			goto sigsegv;
708 
709 		flush_signal_insns(address);
710 	}
711 	return 0;
712 
713 sigill:
714 	do_exit(SIGILL);
715 	return -EINVAL;
716 
717 sigsegv:
718 	force_sigsegv(signr, current);
719 	return -EFAULT;
720 }
721 
722 static inline void handle_signal32(unsigned long signr, struct k_sigaction *ka,
723 				  siginfo_t *info,
724 				  sigset_t *oldset, struct pt_regs *regs)
725 {
726 	int err;
727 
728 	if (ka->sa.sa_flags & SA_SIGINFO)
729 		err = setup_rt_frame32(ka, regs, signr, oldset, info);
730 	else
731 		err = setup_frame32(ka, regs, signr, oldset);
732 
733 	if (err)
734 		return;
735 
736 	signal_delivered(signr, info, ka, regs, 0);
737 }
738 
739 static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
740 				     struct sigaction *sa)
741 {
742 	switch (regs->u_regs[UREG_I0]) {
743 	case ERESTART_RESTARTBLOCK:
744 	case ERESTARTNOHAND:
745 	no_system_call_restart:
746 		regs->u_regs[UREG_I0] = EINTR;
747 		regs->tstate |= TSTATE_ICARRY;
748 		break;
749 	case ERESTARTSYS:
750 		if (!(sa->sa_flags & SA_RESTART))
751 			goto no_system_call_restart;
752 		/* fallthrough */
753 	case ERESTARTNOINTR:
754 		regs->u_regs[UREG_I0] = orig_i0;
755 		regs->tpc -= 4;
756 		regs->tnpc -= 4;
757 	}
758 }
759 
760 /* Note that 'init' is a special process: it doesn't get signals it doesn't
761  * want to handle. Thus you cannot kill init even with a SIGKILL even by
762  * mistake.
763  */
764 void do_signal32(sigset_t *oldset, struct pt_regs * regs)
765 {
766 	struct k_sigaction ka;
767 	unsigned long orig_i0;
768 	int restart_syscall;
769 	siginfo_t info;
770 	int signr;
771 
772 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
773 
774 	restart_syscall = 0;
775 	orig_i0 = 0;
776 	if (pt_regs_is_syscall(regs) &&
777 	    (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
778 		restart_syscall = 1;
779 		orig_i0 = regs->u_regs[UREG_G6];
780 	}
781 
782 	if (signr > 0) {
783 		if (restart_syscall)
784 			syscall_restart32(orig_i0, regs, &ka.sa);
785 		handle_signal32(signr, &ka, &info, oldset, regs);
786 		return;
787 	}
788 	if (restart_syscall &&
789 	    (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
790 	     regs->u_regs[UREG_I0] == ERESTARTSYS ||
791 	     regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
792 		/* replay the system call when we are done */
793 		regs->u_regs[UREG_I0] = orig_i0;
794 		regs->tpc -= 4;
795 		regs->tnpc -= 4;
796 		pt_regs_clear_syscall(regs);
797 	}
798 	if (restart_syscall &&
799 	    regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
800 		regs->u_regs[UREG_G1] = __NR_restart_syscall;
801 		regs->tpc -= 4;
802 		regs->tnpc -= 4;
803 		pt_regs_clear_syscall(regs);
804 	}
805 
806 	/* If there's no signal to deliver, we just put the saved sigmask
807 	 * back
808 	 */
809 	restore_saved_sigmask();
810 }
811 
812 struct sigstack32 {
813 	u32 the_stack;
814 	int cur_status;
815 };
816 
817 asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
818 {
819 	struct sigstack32 __user *ssptr =
820 		(struct sigstack32 __user *)((unsigned long)(u_ssptr));
821 	struct sigstack32 __user *ossptr =
822 		(struct sigstack32 __user *)((unsigned long)(u_ossptr));
823 	int ret = -EFAULT;
824 
825 	/* First see if old state is wanted. */
826 	if (ossptr) {
827 		if (put_user(current->sas_ss_sp + current->sas_ss_size,
828 			     &ossptr->the_stack) ||
829 		    __put_user(on_sig_stack(sp), &ossptr->cur_status))
830 			goto out;
831 	}
832 
833 	/* Now see if we want to update the new state. */
834 	if (ssptr) {
835 		u32 ss_sp;
836 
837 		if (get_user(ss_sp, &ssptr->the_stack))
838 			goto out;
839 
840 		/* If the current stack was set with sigaltstack, don't
841 		 * swap stacks while we are on it.
842 		 */
843 		ret = -EPERM;
844 		if (current->sas_ss_sp && on_sig_stack(sp))
845 			goto out;
846 
847 		/* Since we don't know the extent of the stack, and we don't
848 		 * track onstack-ness, but rather calculate it, we must
849 		 * presume a size.  Ho hum this interface is lossy.
850 		 */
851 		current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
852 		current->sas_ss_size = SIGSTKSZ;
853 	}
854 
855 	ret = 0;
856 out:
857 	return ret;
858 }
859 
860 asmlinkage long do_sys32_sigaltstack(u32 ussa, u32 uossa, unsigned long sp)
861 {
862 	stack_t uss, uoss;
863 	u32 u_ss_sp = 0;
864 	int ret;
865 	mm_segment_t old_fs;
866 	stack_t32 __user *uss32 = compat_ptr(ussa);
867 	stack_t32 __user *uoss32 = compat_ptr(uossa);
868 
869 	if (ussa && (get_user(u_ss_sp, &uss32->ss_sp) ||
870 		    __get_user(uss.ss_flags, &uss32->ss_flags) ||
871 		    __get_user(uss.ss_size, &uss32->ss_size)))
872 		return -EFAULT;
873 	uss.ss_sp = compat_ptr(u_ss_sp);
874 	old_fs = get_fs();
875 	set_fs(KERNEL_DS);
876 	ret = do_sigaltstack(ussa ? (stack_t __user *) &uss : NULL,
877 			     uossa ? (stack_t __user *) &uoss : NULL, sp);
878 	set_fs(old_fs);
879 	if (!ret && uossa && (put_user(ptr_to_compat(uoss.ss_sp), &uoss32->ss_sp) ||
880 		    __put_user(uoss.ss_flags, &uoss32->ss_flags) ||
881 		    __put_user(uoss.ss_size, &uoss32->ss_size)))
882 		return -EFAULT;
883 	return ret;
884 }
885