Linux Audio

Check our new training course

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