Linux Audio

Check our new training course

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