1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * arch/sh64/kernel/signal.c
7  *
8  * Copyright (C) 2000, 2001  Paolo Alberelli
9  *
10  * Started from sh version.
11  *
12  */
13 
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/kernel.h>
19 #include <linux/signal.h>
20 #include <linux/errno.h>
21 #include <linux/wait.h>
22 #include <linux/ptrace.h>
23 #include <linux/unistd.h>
24 #include <linux/stddef.h>
25 #include <linux/personality.h>
26 #include <asm/ucontext.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 
30 
31 #define REG_RET 9
32 #define REG_ARG1 2
33 #define REG_ARG2 3
34 #define REG_SP 15
35 #define REG_PR 18
36 #define REF_REG_RET regs->regs[REG_RET]
37 #define REF_REG_SP regs->regs[REG_SP]
38 #define DEREF_REG_PR regs->regs[REG_PR]
39 
40 #define DEBUG_SIG 0
41 
42 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
43 
44 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
45 
copy_siginfo_to_user(siginfo_t * to,siginfo_t * from)46 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
47 {
48 	if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
49 		return -EFAULT;
50 	if (from->si_code < 0)
51 		return __copy_to_user(to, from, sizeof(siginfo_t));
52 	else {
53 		int err;
54 
55 		/* If you change siginfo_t structure, please be sure
56 		   this code is fixed accordingly.
57 		   It should never copy any pad contained in the structure
58 		   to avoid security leaks, but must copy the generic
59 		   3 ints plus the relevant union member.  */
60 		err = __put_user(from->si_signo, &to->si_signo);
61 		err |= __put_user(from->si_errno, &to->si_errno);
62 		err |= __put_user((short)from->si_code, &to->si_code);
63 		/* First 32bits of unions are always present.  */
64 		err |= __put_user(from->si_pid, &to->si_pid);
65 		switch (from->si_code >> 16) {
66 		case __SI_FAULT >> 16:
67 			break;
68 		case __SI_CHLD >> 16:
69 			err |= __put_user(from->si_utime, &to->si_utime);
70 			err |= __put_user(from->si_stime, &to->si_stime);
71 			err |= __put_user(from->si_status, &to->si_status);
72 		default:
73 			err |= __put_user(from->si_uid, &to->si_uid);
74 			break;
75 		/* case __SI_RT: This is not generated by the kernel as of now.  */
76 		}
77 		return err;
78 	}
79 }
80 
81 /*
82  * Atomically swap in the new signal mask, and wait for a signal.
83  */
84 
85 asmlinkage int
sys_sigsuspend(old_sigset_t mask,unsigned long r3,unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs * regs)86 sys_sigsuspend(old_sigset_t mask,
87 	       unsigned long r3, unsigned long r4, unsigned long r5,
88 	       unsigned long r6, unsigned long r7,
89 	       struct pt_regs * regs)
90 {
91 	sigset_t saveset;
92 
93 	mask &= _BLOCKABLE;
94 	spin_lock_irq(&current->sigmask_lock);
95 	saveset = current->blocked;
96 	siginitset(&current->blocked, mask);
97 	recalc_sigpending(current);
98 	spin_unlock_irq(&current->sigmask_lock);
99 
100 	REF_REG_RET = -EINTR;
101 	while (1) {
102 		current->state = TASK_INTERRUPTIBLE;
103 		schedule();
104 		regs->pc += 4;    /* because sys_sigreturn decrements the pc */
105 		if (do_signal (regs, &saveset))
106 		{	/* pc now points at signal handler. Need to decrement
107 			   it because entry.S will increment it. */
108 			regs->pc -= 4;
109 			return -EINTR;
110 		}
111 	}
112 }
113 
114 asmlinkage int
sys_rt_sigsuspend(sigset_t * unewset,size_t sigsetsize,unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs * regs)115 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize,
116 	          unsigned long r4, unsigned long r5, unsigned long r6,
117 	          unsigned long r7,
118 	          struct pt_regs * regs)
119 {
120 	sigset_t saveset, newset;
121 
122 	/* XXX: Don't preclude handling different sized sigset_t's.  */
123 	if (sigsetsize != sizeof(sigset_t))
124 		return -EINVAL;
125 
126 	if (copy_from_user(&newset, unewset, sizeof(newset)))
127 		return -EFAULT;
128 	sigdelsetmask(&newset, ~_BLOCKABLE);
129 	spin_lock_irq(&current->sigmask_lock);
130 	saveset = current->blocked;
131 	current->blocked = newset;
132 	recalc_sigpending(current);
133 	spin_unlock_irq(&current->sigmask_lock);
134 
135 	REF_REG_RET = -EINTR;
136 	while (1) {
137 		current->state = TASK_INTERRUPTIBLE;
138 		schedule();
139 		regs->pc += 4;    /* because sys_sigreturn decrements the pc */
140 		if (do_signal (regs, &saveset))
141 		{	/* pc now points at signal handler. Need to decrement
142 			   it because entry.S will increment it. */
143 			regs->pc -= 4;
144 			return -EINTR;
145 		}
146 	}
147 }
148 
149 asmlinkage int
sys_sigaction(int sig,const struct old_sigaction * act,struct old_sigaction * oact)150 sys_sigaction(int sig, const struct old_sigaction *act,
151 	      struct old_sigaction *oact)
152 {
153 	struct k_sigaction new_ka, old_ka;
154 	int ret;
155 
156 	if (act) {
157 		old_sigset_t mask;
158 		if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
159 		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
160 		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
161 			return -EFAULT;
162 		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
163 		__get_user(mask, &act->sa_mask);
164 		siginitset(&new_ka.sa.sa_mask, mask);
165 	}
166 
167 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
168 
169 	if (!ret && oact) {
170 		if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
171 		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
172 		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
173 			return -EFAULT;
174 		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
175 		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
176 	}
177 
178 	return ret;
179 }
180 
181 asmlinkage int
sys_sigaltstack(const stack_t * uss,stack_t * uoss,unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs * regs)182 sys_sigaltstack(const stack_t *uss, stack_t *uoss,
183 	        unsigned long r4, unsigned long r5, unsigned long r6,
184 	        unsigned long r7,
185 	        struct pt_regs * regs)
186 {
187 	return do_sigaltstack(uss, uoss, REF_REG_SP);
188 }
189 
190 
191 /*
192  * Do a signal return; undo the signal stack.
193  */
194 
195 struct sigframe
196 {
197 	struct sigcontext sc;
198 	unsigned long extramask[_NSIG_WORDS-1];
199 	long long retcode[2];
200 };
201 
202 struct rt_sigframe
203 {
204 	struct siginfo *pinfo;
205 	void *puc;
206 	struct siginfo info;
207 	struct ucontext uc;
208 	long long retcode[2];
209 };
210 
211 #ifndef CONFIG_NOFPU_SUPPORT
212 static inline int
restore_sigcontext_fpu(struct pt_regs * regs,struct sigcontext * sc)213 restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext *sc)
214 {
215 	int err = 0;
216 	int fpvalid;
217 
218 	err |= __get_user (fpvalid, &sc->sc_fpvalid);
219 	current->used_math = fpvalid;
220 	if (! fpvalid)
221 		return err;
222 
223 	if (current == last_task_used_math) {
224 		last_task_used_math = NULL;
225 		regs->sr |= SR_FD;
226 	}
227 
228 	err |= __copy_from_user(&current->thread.fpu.hard, &sc->sc_fpregs[0],
229 				(sizeof(long long) * 32) + (sizeof(int) * 1));
230 
231 	return err;
232 }
233 
234 static inline int
setup_sigcontext_fpu(struct pt_regs * regs,struct sigcontext * sc)235 setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext *sc)
236 {
237 	int err = 0;
238 	int fpvalid;
239 
240 	fpvalid = current->used_math;
241 	err |= __put_user(fpvalid, &sc->sc_fpvalid);
242 	if (! fpvalid)
243 		return err;
244 
245 	if (current == last_task_used_math) {
246 		grab_fpu();
247 		fpsave(&current->thread.fpu.hard);
248 		release_fpu();
249 		last_task_used_math = NULL;
250 		regs->sr |= SR_FD;
251 	}
252 
253 	err |= __copy_to_user(&sc->sc_fpregs[0], &current->thread.fpu.hard,
254 			      (sizeof(long long) * 32) + (sizeof(int) * 1));
255 	current->used_math = 0;
256 
257 	return err;
258 }
259 #else
260 static inline int
restore_sigcontext_fpu(struct pt_regs * regs,struct sigcontext * sc)261 restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext *sc)
262 {}
263 static inline int
setup_sigcontext_fpu(struct pt_regs * regs,struct sigcontext * sc)264 setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext *sc)
265 {}
266 #endif
267 
268 static int
restore_sigcontext(struct pt_regs * regs,struct sigcontext * sc,long long * r2_p)269 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, long long *r2_p)
270 {
271 	unsigned int err = 0;
272 
273 #define COPY(x)		err |= __get_user(regs->x, &sc->sc_##x)
274 
275 	COPY(regs[0]);	COPY(regs[1]);	COPY(regs[2]);	COPY(regs[3]);
276 	COPY(regs[4]);	COPY(regs[5]);	COPY(regs[6]);	COPY(regs[7]);
277 	COPY(regs[8]);	COPY(regs[9]);  COPY(regs[10]);	COPY(regs[11]);
278 	COPY(regs[12]);	COPY(regs[13]);	COPY(regs[14]);	COPY(regs[15]);
279 	COPY(regs[16]);	COPY(regs[17]);	COPY(regs[18]);	COPY(regs[19]);
280 	COPY(regs[20]);	COPY(regs[21]);	COPY(regs[22]);	COPY(regs[23]);
281 	COPY(regs[24]);	COPY(regs[25]);	COPY(regs[26]);	COPY(regs[27]);
282 	COPY(regs[28]);	COPY(regs[29]);	COPY(regs[30]);	COPY(regs[31]);
283 	COPY(regs[32]);	COPY(regs[33]);	COPY(regs[34]);	COPY(regs[35]);
284 	COPY(regs[36]);	COPY(regs[37]);	COPY(regs[38]);	COPY(regs[39]);
285 	COPY(regs[40]);	COPY(regs[41]);	COPY(regs[42]);	COPY(regs[43]);
286 	COPY(regs[44]);	COPY(regs[45]);	COPY(regs[46]);	COPY(regs[47]);
287 	COPY(regs[48]);	COPY(regs[49]);	COPY(regs[50]);	COPY(regs[51]);
288 	COPY(regs[52]);	COPY(regs[53]);	COPY(regs[54]);	COPY(regs[55]);
289 	COPY(regs[56]);	COPY(regs[57]);	COPY(regs[58]);	COPY(regs[59]);
290 	COPY(regs[60]);	COPY(regs[61]);	COPY(regs[62]);
291 	COPY(tregs[0]);	COPY(tregs[1]);	COPY(tregs[2]);	COPY(tregs[3]);
292 	COPY(tregs[4]);	COPY(tregs[5]);	COPY(tregs[6]);	COPY(tregs[7]);
293 	COPY(sr);	COPY(pc);
294 
295 #undef COPY
296 
297 	/* Must do this last in case it sets regs->sr.fd (i.e. after rest of sr
298 	 * has been restored above.) */
299 	err |= restore_sigcontext_fpu(regs, sc);
300 
301 	regs->syscall_nr = -1;		/* disable syscall checks */
302 	err |= __get_user(*r2_p, &sc->sc_regs[REG_RET]);
303 	return err;
304 }
305 
sys_sigreturn(unsigned long r2,unsigned long r3,unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs * regs)306 asmlinkage int sys_sigreturn(unsigned long r2, unsigned long r3,
307 				   unsigned long r4, unsigned long r5,
308 				   unsigned long r6, unsigned long r7,
309 				   struct pt_regs * regs)
310 {
311 	struct sigframe *frame = (struct sigframe *) (long) REF_REG_SP;
312 	sigset_t set;
313 	long long ret;
314 
315 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
316 		goto badframe;
317 
318 	if (__get_user(set.sig[0], &frame->sc.oldmask)
319 	    || (_NSIG_WORDS > 1
320 		&& __copy_from_user(&set.sig[1], &frame->extramask,
321 				    sizeof(frame->extramask))))
322 		goto badframe;
323 
324 	sigdelsetmask(&set, ~_BLOCKABLE);
325 
326 	spin_lock_irq(&current->sigmask_lock);
327 	current->blocked = set;
328 	recalc_sigpending(current);
329 	spin_unlock_irq(&current->sigmask_lock);
330 
331 	if (restore_sigcontext(regs, &frame->sc, &ret))
332 		goto badframe;
333 	regs->pc -= 4;
334 
335 	return (int) ret;
336 
337 badframe:
338 	force_sig(SIGSEGV, current);
339 	return 0;
340 }
341 
sys_rt_sigreturn(unsigned long r2,unsigned long r3,unsigned long r4,unsigned long r5,unsigned long r6,unsigned long r7,struct pt_regs * regs)342 asmlinkage int sys_rt_sigreturn(unsigned long r2, unsigned long r3,
343 				unsigned long r4, unsigned long r5,
344 				unsigned long r6, unsigned long r7,
345 				struct pt_regs * regs)
346 {
347 	struct rt_sigframe *frame = (struct rt_sigframe *) (long) REF_REG_SP;
348 	sigset_t set;
349 	stack_t st;
350 	long long ret;
351 
352 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
353 		goto badframe;
354 
355 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
356 		goto badframe;
357 
358 	sigdelsetmask(&set, ~_BLOCKABLE);
359 	spin_lock_irq(&current->sigmask_lock);
360 	current->blocked = set;
361 	recalc_sigpending(current);
362 	spin_unlock_irq(&current->sigmask_lock);
363 
364 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ret))
365 		goto badframe;
366 	regs->pc -= 4;
367 
368 	if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
369 		goto badframe;
370 	/* It is more difficult to avoid calling this function than to
371 	   call it and ignore errors.  */
372 	do_sigaltstack(&st, NULL, REF_REG_SP);
373 
374 	return (int) ret;
375 
376 badframe:
377 	force_sig(SIGSEGV, current);
378 	return 0;
379 }
380 
381 /*
382  * Set up a signal frame.
383  */
384 
385 static int
setup_sigcontext(struct sigcontext * sc,struct pt_regs * regs,unsigned long mask)386 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
387 		 unsigned long mask)
388 {
389 	int err = 0;
390 
391 	/* Do this first, otherwise is this sets sr->fd, that value isn't preserved. */
392 	err |= setup_sigcontext_fpu(regs, sc);
393 
394 #define COPY(x)		err |= __put_user(regs->x, &sc->sc_##x)
395 
396 	COPY(regs[0]);	COPY(regs[1]);	COPY(regs[2]);	COPY(regs[3]);
397 	COPY(regs[4]);	COPY(regs[5]);	COPY(regs[6]);	COPY(regs[7]);
398 	COPY(regs[8]);	COPY(regs[9]);	COPY(regs[10]);	COPY(regs[11]);
399 	COPY(regs[12]);	COPY(regs[13]);	COPY(regs[14]);	COPY(regs[15]);
400 	COPY(regs[16]);	COPY(regs[17]);	COPY(regs[18]);	COPY(regs[19]);
401 	COPY(regs[20]);	COPY(regs[21]);	COPY(regs[22]);	COPY(regs[23]);
402 	COPY(regs[24]);	COPY(regs[25]);	COPY(regs[26]);	COPY(regs[27]);
403 	COPY(regs[28]);	COPY(regs[29]);	COPY(regs[30]);	COPY(regs[31]);
404 	COPY(regs[32]);	COPY(regs[33]);	COPY(regs[34]);	COPY(regs[35]);
405 	COPY(regs[36]);	COPY(regs[37]);	COPY(regs[38]);	COPY(regs[39]);
406 	COPY(regs[40]);	COPY(regs[41]);	COPY(regs[42]);	COPY(regs[43]);
407 	COPY(regs[44]);	COPY(regs[45]);	COPY(regs[46]);	COPY(regs[47]);
408 	COPY(regs[48]);	COPY(regs[49]);	COPY(regs[50]);	COPY(regs[51]);
409 	COPY(regs[52]);	COPY(regs[53]);	COPY(regs[54]);	COPY(regs[55]);
410 	COPY(regs[56]);	COPY(regs[57]);	COPY(regs[58]);	COPY(regs[59]);
411 	COPY(regs[60]);	COPY(regs[61]);	COPY(regs[62]);
412 	COPY(tregs[0]);	COPY(tregs[1]);	COPY(tregs[2]);	COPY(tregs[3]);
413 	COPY(tregs[4]);	COPY(tregs[5]);	COPY(tregs[6]);	COPY(tregs[7]);
414 	COPY(sr);	COPY(pc);
415 
416 #undef COPY
417 
418 	err |= __put_user(mask, &sc->oldmask);
419 
420 	return err;
421 }
422 
423 /*
424  * Determine which stack to use..
425  */
426 static inline void *
get_sigframe(struct k_sigaction * ka,unsigned long sp,size_t frame_size)427 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
428 {
429 	if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! on_sig_stack(sp))
430 		sp = current->sas_ss_sp + current->sas_ss_size;
431 
432 	return (void *)((sp - frame_size) & -8ul);
433 }
434 
435 void sa_default_restorer(void);		/* See comments below */
436 void sa_default_rt_restorer(void);	/* See comments below */
437 
setup_frame(int sig,struct k_sigaction * ka,sigset_t * set,struct pt_regs * regs)438 static void setup_frame(int sig, struct k_sigaction *ka,
439 			sigset_t *set, struct pt_regs *regs)
440 {
441 	struct sigframe *frame;
442 	int err = 0;
443 	int signal;
444 
445 	frame = get_sigframe(ka, regs->regs[REG_SP], sizeof(*frame));
446 
447 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
448 		goto give_sigsegv;
449 
450 	signal = current->exec_domain
451 		&& current->exec_domain->signal_invmap
452 		&& sig < 32
453 		? current->exec_domain->signal_invmap[sig]
454 		: sig;
455 
456 	err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
457 
458 	/* Give up earlier as i386, in case */
459 	if (err)
460 		goto give_sigsegv;
461 
462 	if (_NSIG_WORDS > 1) {
463 		err |= __copy_to_user(frame->extramask, &set->sig[1],
464 				      sizeof(frame->extramask));
465 	}
466 
467 	/* Give up earlier as i386, in case */
468 	if (err)
469 		goto give_sigsegv;
470 
471 	/* Set up to return from userspace.  If provided, use a stub
472 	   already in userspace.  */
473 	if (ka->sa.sa_flags & SA_RESTORER) {
474 		DEREF_REG_PR = (unsigned long) ka->sa.sa_restorer | 0x1;
475 
476 		/*
477 		 * On SH5 all edited pointers are subject to NEFF
478 		 */
479 		DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
480         		 	(DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
481 	} else {
482 		/*
483 		 * Different approach on SH5.
484 	         * . Endianness independent asm code gets placed in entry.S .
485 		 *   This is limited to four ASM instructions corresponding
486 		 *   to two long longs in size.
487 		 * . err checking is done on the else branch only
488 		 * . flush_icache_range() is called upon __put_user() only
489 		 * . all edited pointers are subject to NEFF
490 		 * . being code, linker turns ShMedia bit on, always
491 		 *   dereference index -1.
492 		 */
493 		DEREF_REG_PR = (unsigned long) frame->retcode | 0x01;
494 		DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
495         		 	(DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
496 
497 		if (__copy_to_user(frame->retcode,
498 			(unsigned long long)sa_default_restorer & (~1), 16) != 0)
499 			goto give_sigsegv;
500 
501 		/* Cohere the trampoline with the I-cache. */
502 		flush_cache_sigtramp(DEREF_REG_PR-1, DEREF_REG_PR-1+16);
503 	}
504 
505 	/*
506 	 * Set up registers for signal handler.
507 	 * All edited pointers are subject to NEFF.
508 	 */
509 	regs->regs[REG_SP] = (unsigned long) frame;
510 	regs->regs[REG_SP] = (regs->regs[REG_SP] & NEFF_SIGN) ?
511         		 (regs->regs[REG_SP] | NEFF_MASK) : regs->regs[REG_SP];
512 	regs->regs[REG_ARG1] = signal; /* Arg for signal handler */
513 	regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
514 	regs->pc = (unsigned long) ka->sa.sa_handler;
515 	regs->pc = (regs->pc & NEFF_SIGN) ? (regs->pc | NEFF_MASK) : regs->pc;
516 
517 	set_fs(USER_DS);
518 
519 #if DEBUG_SIG
520 	/* Broken %016Lx */
521 	printk("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
522 		signal,
523 		current->comm, current->pid, frame,
524 		regs->pc >> 32, regs->pc & 0xffffffff,
525 		DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
526 #endif
527 
528 	return;
529 
530 give_sigsegv:
531 	if (sig == SIGSEGV)
532 		ka->sa.sa_handler = SIG_DFL;
533 	force_sig(SIGSEGV, current);
534 }
535 
setup_rt_frame(int sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * set,struct pt_regs * regs)536 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
537 			   sigset_t *set, struct pt_regs *regs)
538 {
539 	struct rt_sigframe *frame;
540 	int err = 0;
541 	int signal;
542 
543 	frame = get_sigframe(ka, regs->regs[REG_SP], sizeof(*frame));
544 
545 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
546 		goto give_sigsegv;
547 
548 	signal = current->exec_domain
549 		&& current->exec_domain->signal_invmap
550 		&& sig < 32
551 		? current->exec_domain->signal_invmap[sig]
552 		: sig;
553 
554 	err |= __put_user(&frame->info, &frame->pinfo);
555 	err |= __put_user(&frame->uc, &frame->puc);
556 	err |= copy_siginfo_to_user(&frame->info, info);
557 
558 	/* Give up earlier as i386, in case */
559 	if (err)
560 		goto give_sigsegv;
561 
562 	/* Create the ucontext.  */
563 	err |= __put_user(0, &frame->uc.uc_flags);
564 	err |= __put_user(0, &frame->uc.uc_link);
565 	err |= __put_user((void *)current->sas_ss_sp,
566 			  &frame->uc.uc_stack.ss_sp);
567 	err |= __put_user(sas_ss_flags(regs->regs[REG_SP]),
568 			  &frame->uc.uc_stack.ss_flags);
569 	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
570 	err |= setup_sigcontext(&frame->uc.uc_mcontext,
571 			        regs, set->sig[0]);
572 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
573 
574 	/* Give up earlier as i386, in case */
575 	if (err)
576 		goto give_sigsegv;
577 
578 	/* Set up to return from userspace.  If provided, use a stub
579 	   already in userspace.  */
580 	if (ka->sa.sa_flags & SA_RESTORER) {
581 		DEREF_REG_PR = (unsigned long) ka->sa.sa_restorer | 0x1;
582 
583 		/*
584 		 * On SH5 all edited pointers are subject to NEFF
585 		 */
586 		DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
587         		 	(DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
588 	} else {
589 		/*
590 		 * Different approach on SH5.
591 	         * . Endianness independent asm code gets placed in entry.S .
592 		 *   This is limited to four ASM instructions corresponding
593 		 *   to two long longs in size.
594 		 * . err checking is done on the else branch only
595 		 * . flush_icache_range() is called upon __put_user() only
596 		 * . all edited pointers are subject to NEFF
597 		 * . being code, linker turns ShMedia bit on, always
598 		 *   dereference index -1.
599 		 */
600 
601 		DEREF_REG_PR = (unsigned long) frame->retcode | 0x01;
602 		DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
603         		 	(DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
604 
605 		if (__copy_to_user(frame->retcode,
606 			(unsigned long long)sa_default_rt_restorer & (~1), 16) != 0)
607 			goto give_sigsegv;
608 
609 		flush_icache_range(DEREF_REG_PR-1, DEREF_REG_PR-1+15);
610 	}
611 
612 	/*
613 	 * Set up registers for signal handler.
614 	 * All edited pointers are subject to NEFF.
615 	 */
616 	regs->regs[REG_SP] = (unsigned long) frame;
617 	regs->regs[REG_SP] = (regs->regs[REG_SP] & NEFF_SIGN) ?
618         		 (regs->regs[REG_SP] | NEFF_MASK) : regs->regs[REG_SP];
619 	regs->regs[REG_ARG1] = signal; /* Arg for signal handler */
620 	regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->uc.uc_mcontext;
621 	regs->pc = (unsigned long) ka->sa.sa_handler;
622 	regs->pc = (regs->pc & NEFF_SIGN) ? (regs->pc | NEFF_MASK) : regs->pc;
623 
624 	set_fs(USER_DS);
625 
626 #if DEBUG_SIG
627 	/* Broken %016Lx */
628 	printk("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
629 		signal,
630 		current->comm, current->pid, frame,
631 		regs->pc >> 32, regs->pc & 0xffffffff,
632 		DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
633 #endif
634 
635 	return;
636 
637 give_sigsegv:
638 	if (sig == SIGSEGV)
639 		ka->sa.sa_handler = SIG_DFL;
640 	force_sig(SIGSEGV, current);
641 }
642 
643 /*
644  * OK, we're invoking a handler
645  */
646 
647 static void
handle_signal(unsigned long sig,struct k_sigaction * ka,siginfo_t * info,sigset_t * oldset,struct pt_regs * regs)648 handle_signal(unsigned long sig, struct k_sigaction *ka,
649 	      siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
650 {
651 	/* Are we from a system call? */
652 	if (regs->syscall_nr >= 0) {
653 		/* If so, check system call restarting.. */
654 		switch (regs->regs[REG_RET]) {
655 			case -ERESTARTNOHAND:
656 				regs->regs[REG_RET] = -EINTR;
657 				break;
658 
659 			case -ERESTARTSYS:
660 				if (!(ka->sa.sa_flags & SA_RESTART)) {
661 					regs->regs[REG_RET] = -EINTR;
662 					break;
663 				}
664 			/* fallthrough */
665 			case -ERESTARTNOINTR:
666 				/* Decode syscall # */
667 				regs->regs[REG_RET] = regs->syscall_nr;
668 				regs->pc -= 4;
669 		}
670 	}
671 
672 	/* Set up the stack frame */
673 	if (ka->sa.sa_flags & SA_SIGINFO)
674 		setup_rt_frame(sig, ka, info, oldset, regs);
675 	else
676 		setup_frame(sig, ka, oldset, regs);
677 
678 	if (ka->sa.sa_flags & SA_ONESHOT)
679 		ka->sa.sa_handler = SIG_DFL;
680 
681 	if (!(ka->sa.sa_flags & SA_NODEFER)) {
682 		spin_lock_irq(&current->sigmask_lock);
683 		sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
684 		sigaddset(&current->blocked,sig);
685 		recalc_sigpending(current);
686 		spin_unlock_irq(&current->sigmask_lock);
687 	}
688 }
689 
690 /*
691  * Note that 'init' is a special process: it doesn't get signals it doesn't
692  * want to handle. Thus you cannot kill init even with a SIGKILL even by
693  * mistake.
694  *
695  * Note that we go through the signals twice: once to check the signals that
696  * the kernel can handle, and then we build all the user-level signal handling
697  * stack-frames in one go after that.
698  */
do_signal(struct pt_regs * regs,sigset_t * oldset)699 int do_signal(struct pt_regs *regs, sigset_t *oldset)
700 {
701 	siginfo_t info;
702 	struct k_sigaction *ka;
703 
704 	/*
705 	 * We want the common case to go fast, which
706 	 * is why we may in certain cases get here from
707 	 * kernel mode. Just return without doing anything
708 	 * if so.
709 	 */
710 	if (!user_mode(regs))
711 		return 1;
712 
713 	if (!oldset)
714 		oldset = &current->blocked;
715 
716 	for (;;) {
717 		unsigned long signr;
718 
719 		spin_lock_irq(&current->sigmask_lock);
720 		signr = dequeue_signal(&current->blocked, &info);
721 		spin_unlock_irq(&current->sigmask_lock);
722 
723 		if (!signr)
724 			break;
725 		if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
726 			/* Let the debugger run.  */
727 			current->exit_code = signr;
728 			current->state = TASK_STOPPED;
729 			notify_parent(current, SIGCHLD);
730 			schedule();
731 
732 			/* We're back.  Did the debugger cancel the sig?  */
733 			if (!(signr = current->exit_code))
734 				continue;
735 			current->exit_code = 0;
736 
737 			/* The debugger continued.  Ignore SIGSTOP.  */
738 			if (signr == SIGSTOP)
739 				continue;
740 
741 			/* Update the siginfo structure.  Is this good?  */
742 			if (signr != info.si_signo) {
743 				info.si_signo = signr;
744 				info.si_errno = 0;
745 				info.si_code = SI_USER;
746 				info.si_pid = current->p_pptr->pid;
747 				info.si_uid = current->p_pptr->uid;
748 			}
749 
750 			/* If the (new) signal is now blocked, requeue it.  */
751 			if (sigismember(&current->blocked, signr)) {
752 				send_sig_info(signr, &info, current);
753 				continue;
754 			}
755 		}
756 
757 		ka = &current->sig->action[signr-1];
758 		if (ka->sa.sa_handler == SIG_IGN) {
759 			if (signr != SIGCHLD)
760 				continue;
761 			/* Check for SIGCHLD: it's special.  */
762 			while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
763 				/* nothing */;
764 			continue;
765 		}
766 
767 		if (ka->sa.sa_handler == SIG_DFL) {
768 			int exit_code = signr;
769 
770 			/* Init gets no signals it doesn't want.  */
771 			if (current->pid == 1)
772 				continue;
773 
774 			switch (signr) {
775 			case SIGCONT: case SIGCHLD: case SIGWINCH:
776 				continue;
777 
778 			case SIGTSTP: case SIGTTIN: case SIGTTOU:
779 				if (is_orphaned_pgrp(current->pgrp))
780 					continue;
781 				/* FALLTHRU */
782 
783 			case SIGSTOP:
784 				current->state = TASK_STOPPED;
785 				current->exit_code = signr;
786 				if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
787 					notify_parent(current, SIGCHLD);
788 				schedule();
789 				continue;
790 
791 			case SIGQUIT: case SIGILL: case SIGTRAP:
792 			case SIGABRT: case SIGFPE: case SIGSEGV:
793 			case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
794 				if (do_coredump(signr, regs))
795 					exit_code |= 0x80;
796 				/* FALLTHRU */
797 
798 			default:
799 			        sig_exit(signr, exit_code, &info);
800 				/* NOTREACHED */
801 			}
802 		}
803 
804 		/* Whee!  Actually deliver the signal.  */
805 		handle_signal(signr, ka, &info, oldset, regs);
806 		return 1;
807 	}
808 
809 	/* Did we come from a system call? */
810 	if (regs->syscall_nr >= 0) {
811 		/* Restart the system call - no handlers present */
812 		if (regs->regs[REG_RET] == -ERESTARTNOHAND ||
813 		    regs->regs[REG_RET] == -ERESTARTSYS ||
814 		    regs->regs[REG_RET] == -ERESTARTNOINTR) {
815 			/* Decode Syscall # */
816 			regs->regs[REG_RET] = regs->syscall_nr;
817 			regs->pc -= 4;
818 		}
819 	}
820 	return 0;
821 }
822