Linux Audio

Check our new training course

Loading...
v3.5.6
  1/*
  2 *  linux/arch/sh/kernel/signal.c
  3 *
  4 *  Copyright (C) 1991, 1992  Linus Torvalds
  5 *
  6 *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
  7 *
  8 *  SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
  9 *
 10 */
 11#include <linux/sched.h>
 12#include <linux/mm.h>
 13#include <linux/smp.h>
 14#include <linux/kernel.h>
 15#include <linux/signal.h>
 16#include <linux/errno.h>
 17#include <linux/wait.h>
 18#include <linux/ptrace.h>
 19#include <linux/unistd.h>
 20#include <linux/stddef.h>
 21#include <linux/tty.h>
 22#include <linux/elf.h>
 23#include <linux/personality.h>
 24#include <linux/binfmts.h>
 25#include <linux/freezer.h>
 26#include <linux/io.h>
 27#include <linux/tracehook.h>
 
 28#include <asm/ucontext.h>
 29#include <asm/uaccess.h>
 30#include <asm/pgtable.h>
 31#include <asm/cacheflush.h>
 32#include <asm/syscalls.h>
 33#include <asm/fpu.h>
 34
 
 
 35struct fdpic_func_descriptor {
 36	unsigned long	text;
 37	unsigned long	GOT;
 38};
 39
 40/*
 41 * The following define adds a 64 byte gap between the signal
 42 * stack frame and previous contents of the stack.  This allows
 43 * frame unwinding in a function epilogue but only if a frame
 44 * pointer is used in the function.  This is necessary because
 45 * current gcc compilers (<4.3) do not generate unwind info on
 46 * SH for function epilogues.
 47 */
 48#define UNWINDGUARD 64
 49
 50/*
 51 * Atomically swap in the new signal mask, and wait for a signal.
 52 */
 53asmlinkage int
 54sys_sigsuspend(old_sigset_t mask)
 55{
 56	sigset_t blocked;
 57	siginitset(&blocked, mask);
 58	return sigsuspend(&blocked);
 
 
 
 
 
 
 
 
 
 
 
 59}
 60
 61asmlinkage int
 62sys_sigaction(int sig, const struct old_sigaction __user *act,
 63	      struct old_sigaction __user *oact)
 64{
 65	struct k_sigaction new_ka, old_ka;
 66	int ret;
 67
 68	if (act) {
 69		old_sigset_t mask;
 70		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
 71		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
 72		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
 73		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
 74		    __get_user(mask, &act->sa_mask))
 75			return -EFAULT;
 
 
 76		siginitset(&new_ka.sa.sa_mask, mask);
 77	}
 78
 79	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
 80
 81	if (!ret && oact) {
 82		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
 83		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
 84		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
 85		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
 86		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
 87			return -EFAULT;
 
 
 88	}
 89
 90	return ret;
 91}
 92
 93asmlinkage int
 94sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
 95		unsigned long r6, unsigned long r7,
 96		struct pt_regs __regs)
 97{
 98	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
 99
100	return do_sigaltstack(uss, uoss, regs->regs[15]);
101}
102
103
104/*
105 * Do a signal return; undo the signal stack.
106 */
107
108#define MOVW(n)	 (0x9300|((n)-2))	/* Move mem word at PC+n to R3 */
109#if defined(CONFIG_CPU_SH2)
110#define TRAP_NOARG 0xc320		/* Syscall w/no args (NR in R3) */
111#else
112#define TRAP_NOARG 0xc310		/* Syscall w/no args (NR in R3) */
113#endif
114#define OR_R0_R0 0x200b			/* or r0,r0 (insert to avoid hardware bug) */
115
116struct sigframe
117{
118	struct sigcontext sc;
119	unsigned long extramask[_NSIG_WORDS-1];
120	u16 retcode[8];
121};
122
123struct rt_sigframe
124{
125	struct siginfo info;
126	struct ucontext uc;
127	u16 retcode[8];
128};
129
130#ifdef CONFIG_SH_FPU
131static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
132{
133	struct task_struct *tsk = current;
134
135	if (!(boot_cpu_data.flags & CPU_HAS_FPU))
136		return 0;
137
138	set_used_math();
139	return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
140				sizeof(long)*(16*2+2));
141}
142
143static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
144				      struct pt_regs *regs)
145{
146	struct task_struct *tsk = current;
147
148	if (!(boot_cpu_data.flags & CPU_HAS_FPU))
149		return 0;
150
151	if (!used_math())
152		return __put_user(0, &sc->sc_ownedfp);
 
 
153
154	if (__put_user(1, &sc->sc_ownedfp))
155		return -EFAULT;
156
157	/* This will cause a "finit" to be triggered by the next
158	   attempted FPU operation by the 'current' process.
159	   */
160	clear_used_math();
161
162	unlazy_fpu(tsk, regs);
163	return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
164			      sizeof(long)*(16*2+2));
165}
166#endif /* CONFIG_SH_FPU */
167
168static int
169restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
170{
171	unsigned int err = 0;
172
173#define COPY(x)		err |= __get_user(regs->x, &sc->sc_##x)
174			COPY(regs[1]);
175	COPY(regs[2]);	COPY(regs[3]);
176	COPY(regs[4]);	COPY(regs[5]);
177	COPY(regs[6]);	COPY(regs[7]);
178	COPY(regs[8]);	COPY(regs[9]);
179	COPY(regs[10]);	COPY(regs[11]);
180	COPY(regs[12]);	COPY(regs[13]);
181	COPY(regs[14]);	COPY(regs[15]);
182	COPY(gbr);	COPY(mach);
183	COPY(macl);	COPY(pr);
184	COPY(sr);	COPY(pc);
185#undef COPY
186
187#ifdef CONFIG_SH_FPU
188	if (boot_cpu_data.flags & CPU_HAS_FPU) {
189		int owned_fp;
190		struct task_struct *tsk = current;
191
192		regs->sr |= SR_FD; /* Release FPU */
193		clear_fpu(tsk, regs);
194		clear_used_math();
195		err |= __get_user (owned_fp, &sc->sc_ownedfp);
196		if (owned_fp)
197			err |= restore_sigcontext_fpu(sc);
198	}
199#endif
200
201	regs->tra = -1;		/* disable syscall checks */
202	err |= __get_user(*r0_p, &sc->sc_regs[0]);
203	return err;
204}
205
206asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
207			     unsigned long r6, unsigned long r7,
208			     struct pt_regs __regs)
209{
210	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
211	struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
212	sigset_t set;
213	int r0;
214
215        /* Always make any pending restarted system calls return -EINTR */
216	current_thread_info()->restart_block.fn = do_no_restart_syscall;
217
218	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
219		goto badframe;
220
221	if (__get_user(set.sig[0], &frame->sc.oldmask)
222	    || (_NSIG_WORDS > 1
223		&& __copy_from_user(&set.sig[1], &frame->extramask,
224				    sizeof(frame->extramask))))
225		goto badframe;
226
227	set_current_blocked(&set);
 
 
 
 
 
228
229	if (restore_sigcontext(regs, &frame->sc, &r0))
230		goto badframe;
231	return r0;
232
233badframe:
234	force_sig(SIGSEGV, current);
235	return 0;
236}
237
238asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
239				unsigned long r6, unsigned long r7,
240				struct pt_regs __regs)
241{
242	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
243	struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
244	sigset_t set;
245	int r0;
246
247	/* Always make any pending restarted system calls return -EINTR */
248	current_thread_info()->restart_block.fn = do_no_restart_syscall;
249
250	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
251		goto badframe;
252
253	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
254		goto badframe;
255
256	set_current_blocked(&set);
 
 
 
 
257
258	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
259		goto badframe;
260
261	if (do_sigaltstack(&frame->uc.uc_stack, NULL,
262			   regs->regs[15]) == -EFAULT)
263		goto badframe;
264
265	return r0;
266
267badframe:
268	force_sig(SIGSEGV, current);
269	return 0;
270}
271
272/*
273 * Set up a signal frame.
274 */
275
276static int
277setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
278		 unsigned long mask)
279{
280	int err = 0;
281
282#define COPY(x)		err |= __put_user(regs->x, &sc->sc_##x)
283	COPY(regs[0]);	COPY(regs[1]);
284	COPY(regs[2]);	COPY(regs[3]);
285	COPY(regs[4]);	COPY(regs[5]);
286	COPY(regs[6]);	COPY(regs[7]);
287	COPY(regs[8]);	COPY(regs[9]);
288	COPY(regs[10]);	COPY(regs[11]);
289	COPY(regs[12]);	COPY(regs[13]);
290	COPY(regs[14]);	COPY(regs[15]);
291	COPY(gbr);	COPY(mach);
292	COPY(macl);	COPY(pr);
293	COPY(sr);	COPY(pc);
294#undef COPY
295
296#ifdef CONFIG_SH_FPU
297	err |= save_sigcontext_fpu(sc, regs);
298#endif
299
300	/* non-iBCS2 extensions.. */
301	err |= __put_user(mask, &sc->oldmask);
302
303	return err;
304}
305
306/*
307 * Determine which stack to use..
308 */
309static inline void __user *
310get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
311{
312	if (ka->sa.sa_flags & SA_ONSTACK) {
313		if (sas_ss_flags(sp) == 0)
314			sp = current->sas_ss_sp + current->sas_ss_size;
315	}
316
317	return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
318}
319
320/* These symbols are defined with the addresses in the vsyscall page.
321   See vsyscall-trapa.S.  */
322extern void __kernel_sigreturn(void);
323extern void __kernel_rt_sigreturn(void);
324
325static int setup_frame(int sig, struct k_sigaction *ka,
326			sigset_t *set, struct pt_regs *regs)
327{
328	struct sigframe __user *frame;
329	int err = 0;
330	int signal;
331
332	frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
333
334	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
335		goto give_sigsegv;
336
337	signal = current_thread_info()->exec_domain
338		&& current_thread_info()->exec_domain->signal_invmap
339		&& sig < 32
340		? current_thread_info()->exec_domain->signal_invmap[sig]
341		: sig;
342
343	err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
344
345	if (_NSIG_WORDS > 1)
346		err |= __copy_to_user(frame->extramask, &set->sig[1],
347				      sizeof(frame->extramask));
348
349	/* Set up to return from userspace.  If provided, use a stub
350	   already in userspace.  */
351	if (ka->sa.sa_flags & SA_RESTORER) {
352		regs->pr = (unsigned long) ka->sa.sa_restorer;
353#ifdef CONFIG_VSYSCALL
354	} else if (likely(current->mm->context.vdso)) {
355		regs->pr = VDSO_SYM(&__kernel_sigreturn);
356#endif
357	} else {
358		/* Generate return code (system call to sigreturn) */
359		err |= __put_user(MOVW(7), &frame->retcode[0]);
360		err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
361		err |= __put_user(OR_R0_R0, &frame->retcode[2]);
362		err |= __put_user(OR_R0_R0, &frame->retcode[3]);
363		err |= __put_user(OR_R0_R0, &frame->retcode[4]);
364		err |= __put_user(OR_R0_R0, &frame->retcode[5]);
365		err |= __put_user(OR_R0_R0, &frame->retcode[6]);
366		err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
367		regs->pr = (unsigned long) frame->retcode;
368		flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
369	}
370
371	if (err)
372		goto give_sigsegv;
373
374	/* Set up registers for signal handler */
375	regs->regs[15] = (unsigned long) frame;
376	regs->regs[4] = signal; /* Arg for signal handler */
377	regs->regs[5] = 0;
378	regs->regs[6] = (unsigned long) &frame->sc;
379
380	if (current->personality & FDPIC_FUNCPTRS) {
381		struct fdpic_func_descriptor __user *funcptr =
382			(struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
383
384		err |= __get_user(regs->pc, &funcptr->text);
385		err |= __get_user(regs->regs[12], &funcptr->GOT);
386	} else
387		regs->pc = (unsigned long)ka->sa.sa_handler;
388
389	if (err)
390		goto give_sigsegv;
391
392	set_fs(USER_DS);
393
394	pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
395		 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
396
397	return 0;
398
399give_sigsegv:
400	force_sigsegv(sig, current);
401	return -EFAULT;
402}
403
404static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
405			   sigset_t *set, struct pt_regs *regs)
406{
407	struct rt_sigframe __user *frame;
408	int err = 0;
409	int signal;
410
411	frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
412
413	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
414		goto give_sigsegv;
415
416	signal = current_thread_info()->exec_domain
417		&& current_thread_info()->exec_domain->signal_invmap
418		&& sig < 32
419		? current_thread_info()->exec_domain->signal_invmap[sig]
420		: sig;
421
422	err |= copy_siginfo_to_user(&frame->info, info);
423
424	/* Create the ucontext.  */
425	err |= __put_user(0, &frame->uc.uc_flags);
426	err |= __put_user(NULL, &frame->uc.uc_link);
427	err |= __put_user((void *)current->sas_ss_sp,
428			  &frame->uc.uc_stack.ss_sp);
429	err |= __put_user(sas_ss_flags(regs->regs[15]),
430			  &frame->uc.uc_stack.ss_flags);
431	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
432	err |= setup_sigcontext(&frame->uc.uc_mcontext,
433			        regs, set->sig[0]);
434	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
435
436	/* Set up to return from userspace.  If provided, use a stub
437	   already in userspace.  */
438	if (ka->sa.sa_flags & SA_RESTORER) {
439		regs->pr = (unsigned long) ka->sa.sa_restorer;
440#ifdef CONFIG_VSYSCALL
441	} else if (likely(current->mm->context.vdso)) {
442		regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
443#endif
444	} else {
445		/* Generate return code (system call to rt_sigreturn) */
446		err |= __put_user(MOVW(7), &frame->retcode[0]);
447		err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
448		err |= __put_user(OR_R0_R0, &frame->retcode[2]);
449		err |= __put_user(OR_R0_R0, &frame->retcode[3]);
450		err |= __put_user(OR_R0_R0, &frame->retcode[4]);
451		err |= __put_user(OR_R0_R0, &frame->retcode[5]);
452		err |= __put_user(OR_R0_R0, &frame->retcode[6]);
453		err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
454		regs->pr = (unsigned long) frame->retcode;
455		flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
456	}
457
458	if (err)
459		goto give_sigsegv;
460
461	/* Set up registers for signal handler */
462	regs->regs[15] = (unsigned long) frame;
463	regs->regs[4] = signal; /* Arg for signal handler */
464	regs->regs[5] = (unsigned long) &frame->info;
465	regs->regs[6] = (unsigned long) &frame->uc;
466
467	if (current->personality & FDPIC_FUNCPTRS) {
468		struct fdpic_func_descriptor __user *funcptr =
469			(struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
470
471		err |= __get_user(regs->pc, &funcptr->text);
472		err |= __get_user(regs->regs[12], &funcptr->GOT);
473	} else
474		regs->pc = (unsigned long)ka->sa.sa_handler;
475
476	if (err)
477		goto give_sigsegv;
478
479	set_fs(USER_DS);
480
481	pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
482		 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
483
484	return 0;
485
486give_sigsegv:
487	force_sigsegv(sig, current);
488	return -EFAULT;
489}
490
491static inline void
492handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
493		       struct sigaction *sa)
494{
495	/* If we're not from a syscall, bail out */
496	if (regs->tra < 0)
497		return;
498
499	/* check for system call restart.. */
500	switch (regs->regs[0]) {
501		case -ERESTART_RESTARTBLOCK:
502		case -ERESTARTNOHAND:
503		no_system_call_restart:
504			regs->regs[0] = -EINTR;
505			break;
506
507		case -ERESTARTSYS:
508			if (!(sa->sa_flags & SA_RESTART))
509				goto no_system_call_restart;
510		/* fallthrough */
511		case -ERESTARTNOINTR:
512			regs->regs[0] = save_r0;
513			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
514			break;
515	}
516}
517
518/*
519 * OK, we're invoking a handler
520 */
521static void
522handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
523	      struct pt_regs *regs, unsigned int save_r0)
524{
525	sigset_t *oldset = sigmask_to_save();
526	int ret;
527
528	/* Set up the stack frame */
529	if (ka->sa.sa_flags & SA_SIGINFO)
530		ret = setup_rt_frame(sig, ka, info, oldset, regs);
531	else
532		ret = setup_frame(sig, ka, oldset, regs);
533
534	if (ret)
535		return;
536	signal_delivered(sig, info, ka, regs,
537			test_thread_flag(TIF_SINGLESTEP));
 
 
 
 
 
 
 
 
 
538}
539
540/*
541 * Note that 'init' is a special process: it doesn't get signals it doesn't
542 * want to handle. Thus you cannot kill init even with a SIGKILL even by
543 * mistake.
544 *
545 * Note that we go through the signals twice: once to check the signals that
546 * the kernel can handle, and then we build all the user-level signal handling
547 * stack-frames in one go after that.
548 */
549static void do_signal(struct pt_regs *regs, unsigned int save_r0)
550{
551	siginfo_t info;
552	int signr;
553	struct k_sigaction ka;
 
554
555	/*
556	 * We want the common case to go fast, which
557	 * is why we may in certain cases get here from
558	 * kernel mode. Just return without doing anything
559	 * if so.
560	 */
561	if (!user_mode(regs))
562		return;
563
 
 
 
 
 
 
 
 
564	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
565	if (signr > 0) {
566		handle_syscall_restart(save_r0, regs, &ka.sa);
567
568		/* Whee!  Actually deliver the signal.  */
569		handle_signal(signr, &ka, &info, regs, save_r0);
 
 
 
 
 
 
 
 
 
 
 
 
 
570		return;
571	}
572
 
573	/* Did we come from a system call? */
574	if (regs->tra >= 0) {
575		/* Restart the system call - no handlers present */
576		if (regs->regs[0] == -ERESTARTNOHAND ||
577		    regs->regs[0] == -ERESTARTSYS ||
578		    regs->regs[0] == -ERESTARTNOINTR) {
579			regs->regs[0] = save_r0;
580			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
581		} else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
582			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
583			regs->regs[3] = __NR_restart_syscall;
584		}
585	}
586
587	/*
588	 * If there's no signal to deliver, we just put the saved sigmask
589	 * back.
590	 */
591	restore_saved_sigmask();
 
 
 
592}
593
594asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
595				 unsigned long thread_info_flags)
596{
597	/* deal with pending signal delivery */
598	if (thread_info_flags & _TIF_SIGPENDING)
599		do_signal(regs, save_r0);
600
601	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
602		clear_thread_flag(TIF_NOTIFY_RESUME);
603		tracehook_notify_resume(regs);
 
 
604	}
605}
v3.1
  1/*
  2 *  linux/arch/sh/kernel/signal.c
  3 *
  4 *  Copyright (C) 1991, 1992  Linus Torvalds
  5 *
  6 *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
  7 *
  8 *  SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
  9 *
 10 */
 11#include <linux/sched.h>
 12#include <linux/mm.h>
 13#include <linux/smp.h>
 14#include <linux/kernel.h>
 15#include <linux/signal.h>
 16#include <linux/errno.h>
 17#include <linux/wait.h>
 18#include <linux/ptrace.h>
 19#include <linux/unistd.h>
 20#include <linux/stddef.h>
 21#include <linux/tty.h>
 22#include <linux/elf.h>
 23#include <linux/personality.h>
 24#include <linux/binfmts.h>
 25#include <linux/freezer.h>
 26#include <linux/io.h>
 27#include <linux/tracehook.h>
 28#include <asm/system.h>
 29#include <asm/ucontext.h>
 30#include <asm/uaccess.h>
 31#include <asm/pgtable.h>
 32#include <asm/cacheflush.h>
 33#include <asm/syscalls.h>
 34#include <asm/fpu.h>
 35
 36#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
 37
 38struct fdpic_func_descriptor {
 39	unsigned long	text;
 40	unsigned long	GOT;
 41};
 42
 43/*
 44 * The following define adds a 64 byte gap between the signal
 45 * stack frame and previous contents of the stack.  This allows
 46 * frame unwinding in a function epilogue but only if a frame
 47 * pointer is used in the function.  This is necessary because
 48 * current gcc compilers (<4.3) do not generate unwind info on
 49 * SH for function epilogues.
 50 */
 51#define UNWINDGUARD 64
 52
 53/*
 54 * Atomically swap in the new signal mask, and wait for a signal.
 55 */
 56asmlinkage int
 57sys_sigsuspend(old_sigset_t mask,
 58	       unsigned long r5, unsigned long r6, unsigned long r7,
 59	       struct pt_regs __regs)
 60{
 61	mask &= _BLOCKABLE;
 62	spin_lock_irq(&current->sighand->siglock);
 63	current->saved_sigmask = current->blocked;
 64	siginitset(&current->blocked, mask);
 65	recalc_sigpending();
 66	spin_unlock_irq(&current->sighand->siglock);
 67
 68	current->state = TASK_INTERRUPTIBLE;
 69	schedule();
 70	set_restore_sigmask();
 71
 72	return -ERESTARTNOHAND;
 73}
 74
 75asmlinkage int
 76sys_sigaction(int sig, const struct old_sigaction __user *act,
 77	      struct old_sigaction __user *oact)
 78{
 79	struct k_sigaction new_ka, old_ka;
 80	int ret;
 81
 82	if (act) {
 83		old_sigset_t mask;
 84		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
 85		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
 86		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
 
 
 87			return -EFAULT;
 88		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
 89		__get_user(mask, &act->sa_mask);
 90		siginitset(&new_ka.sa.sa_mask, mask);
 91	}
 92
 93	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
 94
 95	if (!ret && oact) {
 96		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
 97		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
 98		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
 
 
 99			return -EFAULT;
100		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
101		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
102	}
103
104	return ret;
105}
106
107asmlinkage int
108sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
109		unsigned long r6, unsigned long r7,
110		struct pt_regs __regs)
111{
112	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
113
114	return do_sigaltstack(uss, uoss, regs->regs[15]);
115}
116
117
118/*
119 * Do a signal return; undo the signal stack.
120 */
121
122#define MOVW(n)	 (0x9300|((n)-2))	/* Move mem word at PC+n to R3 */
123#if defined(CONFIG_CPU_SH2)
124#define TRAP_NOARG 0xc320		/* Syscall w/no args (NR in R3) */
125#else
126#define TRAP_NOARG 0xc310		/* Syscall w/no args (NR in R3) */
127#endif
128#define OR_R0_R0 0x200b			/* or r0,r0 (insert to avoid hardware bug) */
129
130struct sigframe
131{
132	struct sigcontext sc;
133	unsigned long extramask[_NSIG_WORDS-1];
134	u16 retcode[8];
135};
136
137struct rt_sigframe
138{
139	struct siginfo info;
140	struct ucontext uc;
141	u16 retcode[8];
142};
143
144#ifdef CONFIG_SH_FPU
145static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
146{
147	struct task_struct *tsk = current;
148
149	if (!(boot_cpu_data.flags & CPU_HAS_FPU))
150		return 0;
151
152	set_used_math();
153	return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
154				sizeof(long)*(16*2+2));
155}
156
157static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
158				      struct pt_regs *regs)
159{
160	struct task_struct *tsk = current;
161
162	if (!(boot_cpu_data.flags & CPU_HAS_FPU))
163		return 0;
164
165	if (!used_math()) {
166		__put_user(0, &sc->sc_ownedfp);
167		return 0;
168	}
169
170	__put_user(1, &sc->sc_ownedfp);
 
171
172	/* This will cause a "finit" to be triggered by the next
173	   attempted FPU operation by the 'current' process.
174	   */
175	clear_used_math();
176
177	unlazy_fpu(tsk, regs);
178	return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
179			      sizeof(long)*(16*2+2));
180}
181#endif /* CONFIG_SH_FPU */
182
183static int
184restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
185{
186	unsigned int err = 0;
187
188#define COPY(x)		err |= __get_user(regs->x, &sc->sc_##x)
189			COPY(regs[1]);
190	COPY(regs[2]);	COPY(regs[3]);
191	COPY(regs[4]);	COPY(regs[5]);
192	COPY(regs[6]);	COPY(regs[7]);
193	COPY(regs[8]);	COPY(regs[9]);
194	COPY(regs[10]);	COPY(regs[11]);
195	COPY(regs[12]);	COPY(regs[13]);
196	COPY(regs[14]);	COPY(regs[15]);
197	COPY(gbr);	COPY(mach);
198	COPY(macl);	COPY(pr);
199	COPY(sr);	COPY(pc);
200#undef COPY
201
202#ifdef CONFIG_SH_FPU
203	if (boot_cpu_data.flags & CPU_HAS_FPU) {
204		int owned_fp;
205		struct task_struct *tsk = current;
206
207		regs->sr |= SR_FD; /* Release FPU */
208		clear_fpu(tsk, regs);
209		clear_used_math();
210		__get_user (owned_fp, &sc->sc_ownedfp);
211		if (owned_fp)
212			err |= restore_sigcontext_fpu(sc);
213	}
214#endif
215
216	regs->tra = -1;		/* disable syscall checks */
217	err |= __get_user(*r0_p, &sc->sc_regs[0]);
218	return err;
219}
220
221asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
222			     unsigned long r6, unsigned long r7,
223			     struct pt_regs __regs)
224{
225	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
226	struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
227	sigset_t set;
228	int r0;
229
230        /* Always make any pending restarted system calls return -EINTR */
231	current_thread_info()->restart_block.fn = do_no_restart_syscall;
232
233	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
234		goto badframe;
235
236	if (__get_user(set.sig[0], &frame->sc.oldmask)
237	    || (_NSIG_WORDS > 1
238		&& __copy_from_user(&set.sig[1], &frame->extramask,
239				    sizeof(frame->extramask))))
240		goto badframe;
241
242	sigdelsetmask(&set, ~_BLOCKABLE);
243
244	spin_lock_irq(&current->sighand->siglock);
245	current->blocked = set;
246	recalc_sigpending();
247	spin_unlock_irq(&current->sighand->siglock);
248
249	if (restore_sigcontext(regs, &frame->sc, &r0))
250		goto badframe;
251	return r0;
252
253badframe:
254	force_sig(SIGSEGV, current);
255	return 0;
256}
257
258asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
259				unsigned long r6, unsigned long r7,
260				struct pt_regs __regs)
261{
262	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
263	struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
264	sigset_t set;
265	int r0;
266
267	/* Always make any pending restarted system calls return -EINTR */
268	current_thread_info()->restart_block.fn = do_no_restart_syscall;
269
270	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
271		goto badframe;
272
273	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
274		goto badframe;
275
276	sigdelsetmask(&set, ~_BLOCKABLE);
277	spin_lock_irq(&current->sighand->siglock);
278	current->blocked = set;
279	recalc_sigpending();
280	spin_unlock_irq(&current->sighand->siglock);
281
282	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
283		goto badframe;
284
285	if (do_sigaltstack(&frame->uc.uc_stack, NULL,
286			   regs->regs[15]) == -EFAULT)
287		goto badframe;
288
289	return r0;
290
291badframe:
292	force_sig(SIGSEGV, current);
293	return 0;
294}
295
296/*
297 * Set up a signal frame.
298 */
299
300static int
301setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
302		 unsigned long mask)
303{
304	int err = 0;
305
306#define COPY(x)		err |= __put_user(regs->x, &sc->sc_##x)
307	COPY(regs[0]);	COPY(regs[1]);
308	COPY(regs[2]);	COPY(regs[3]);
309	COPY(regs[4]);	COPY(regs[5]);
310	COPY(regs[6]);	COPY(regs[7]);
311	COPY(regs[8]);	COPY(regs[9]);
312	COPY(regs[10]);	COPY(regs[11]);
313	COPY(regs[12]);	COPY(regs[13]);
314	COPY(regs[14]);	COPY(regs[15]);
315	COPY(gbr);	COPY(mach);
316	COPY(macl);	COPY(pr);
317	COPY(sr);	COPY(pc);
318#undef COPY
319
320#ifdef CONFIG_SH_FPU
321	err |= save_sigcontext_fpu(sc, regs);
322#endif
323
324	/* non-iBCS2 extensions.. */
325	err |= __put_user(mask, &sc->oldmask);
326
327	return err;
328}
329
330/*
331 * Determine which stack to use..
332 */
333static inline void __user *
334get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
335{
336	if (ka->sa.sa_flags & SA_ONSTACK) {
337		if (sas_ss_flags(sp) == 0)
338			sp = current->sas_ss_sp + current->sas_ss_size;
339	}
340
341	return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
342}
343
344/* These symbols are defined with the addresses in the vsyscall page.
345   See vsyscall-trapa.S.  */
346extern void __kernel_sigreturn(void);
347extern void __kernel_rt_sigreturn(void);
348
349static int setup_frame(int sig, struct k_sigaction *ka,
350			sigset_t *set, struct pt_regs *regs)
351{
352	struct sigframe __user *frame;
353	int err = 0;
354	int signal;
355
356	frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
357
358	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
359		goto give_sigsegv;
360
361	signal = current_thread_info()->exec_domain
362		&& current_thread_info()->exec_domain->signal_invmap
363		&& sig < 32
364		? current_thread_info()->exec_domain->signal_invmap[sig]
365		: sig;
366
367	err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
368
369	if (_NSIG_WORDS > 1)
370		err |= __copy_to_user(frame->extramask, &set->sig[1],
371				      sizeof(frame->extramask));
372
373	/* Set up to return from userspace.  If provided, use a stub
374	   already in userspace.  */
375	if (ka->sa.sa_flags & SA_RESTORER) {
376		regs->pr = (unsigned long) ka->sa.sa_restorer;
377#ifdef CONFIG_VSYSCALL
378	} else if (likely(current->mm->context.vdso)) {
379		regs->pr = VDSO_SYM(&__kernel_sigreturn);
380#endif
381	} else {
382		/* Generate return code (system call to sigreturn) */
383		err |= __put_user(MOVW(7), &frame->retcode[0]);
384		err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
385		err |= __put_user(OR_R0_R0, &frame->retcode[2]);
386		err |= __put_user(OR_R0_R0, &frame->retcode[3]);
387		err |= __put_user(OR_R0_R0, &frame->retcode[4]);
388		err |= __put_user(OR_R0_R0, &frame->retcode[5]);
389		err |= __put_user(OR_R0_R0, &frame->retcode[6]);
390		err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
391		regs->pr = (unsigned long) frame->retcode;
392		flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
393	}
394
395	if (err)
396		goto give_sigsegv;
397
398	/* Set up registers for signal handler */
399	regs->regs[15] = (unsigned long) frame;
400	regs->regs[4] = signal; /* Arg for signal handler */
401	regs->regs[5] = 0;
402	regs->regs[6] = (unsigned long) &frame->sc;
403
404	if (current->personality & FDPIC_FUNCPTRS) {
405		struct fdpic_func_descriptor __user *funcptr =
406			(struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
407
408		__get_user(regs->pc, &funcptr->text);
409		__get_user(regs->regs[12], &funcptr->GOT);
410	} else
411		regs->pc = (unsigned long)ka->sa.sa_handler;
412
 
 
 
413	set_fs(USER_DS);
414
415	pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
416		 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
417
418	return 0;
419
420give_sigsegv:
421	force_sigsegv(sig, current);
422	return -EFAULT;
423}
424
425static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
426			   sigset_t *set, struct pt_regs *regs)
427{
428	struct rt_sigframe __user *frame;
429	int err = 0;
430	int signal;
431
432	frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
433
434	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
435		goto give_sigsegv;
436
437	signal = current_thread_info()->exec_domain
438		&& current_thread_info()->exec_domain->signal_invmap
439		&& sig < 32
440		? current_thread_info()->exec_domain->signal_invmap[sig]
441		: sig;
442
443	err |= copy_siginfo_to_user(&frame->info, info);
444
445	/* Create the ucontext.  */
446	err |= __put_user(0, &frame->uc.uc_flags);
447	err |= __put_user(NULL, &frame->uc.uc_link);
448	err |= __put_user((void *)current->sas_ss_sp,
449			  &frame->uc.uc_stack.ss_sp);
450	err |= __put_user(sas_ss_flags(regs->regs[15]),
451			  &frame->uc.uc_stack.ss_flags);
452	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
453	err |= setup_sigcontext(&frame->uc.uc_mcontext,
454			        regs, set->sig[0]);
455	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
456
457	/* Set up to return from userspace.  If provided, use a stub
458	   already in userspace.  */
459	if (ka->sa.sa_flags & SA_RESTORER) {
460		regs->pr = (unsigned long) ka->sa.sa_restorer;
461#ifdef CONFIG_VSYSCALL
462	} else if (likely(current->mm->context.vdso)) {
463		regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
464#endif
465	} else {
466		/* Generate return code (system call to rt_sigreturn) */
467		err |= __put_user(MOVW(7), &frame->retcode[0]);
468		err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
469		err |= __put_user(OR_R0_R0, &frame->retcode[2]);
470		err |= __put_user(OR_R0_R0, &frame->retcode[3]);
471		err |= __put_user(OR_R0_R0, &frame->retcode[4]);
472		err |= __put_user(OR_R0_R0, &frame->retcode[5]);
473		err |= __put_user(OR_R0_R0, &frame->retcode[6]);
474		err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
475		regs->pr = (unsigned long) frame->retcode;
476		flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
477	}
478
479	if (err)
480		goto give_sigsegv;
481
482	/* Set up registers for signal handler */
483	regs->regs[15] = (unsigned long) frame;
484	regs->regs[4] = signal; /* Arg for signal handler */
485	regs->regs[5] = (unsigned long) &frame->info;
486	regs->regs[6] = (unsigned long) &frame->uc;
487
488	if (current->personality & FDPIC_FUNCPTRS) {
489		struct fdpic_func_descriptor __user *funcptr =
490			(struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
491
492		__get_user(regs->pc, &funcptr->text);
493		__get_user(regs->regs[12], &funcptr->GOT);
494	} else
495		regs->pc = (unsigned long)ka->sa.sa_handler;
496
 
 
 
497	set_fs(USER_DS);
498
499	pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
500		 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
501
502	return 0;
503
504give_sigsegv:
505	force_sigsegv(sig, current);
506	return -EFAULT;
507}
508
509static inline void
510handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
511		       struct sigaction *sa)
512{
513	/* If we're not from a syscall, bail out */
514	if (regs->tra < 0)
515		return;
516
517	/* check for system call restart.. */
518	switch (regs->regs[0]) {
519		case -ERESTART_RESTARTBLOCK:
520		case -ERESTARTNOHAND:
521		no_system_call_restart:
522			regs->regs[0] = -EINTR;
523			break;
524
525		case -ERESTARTSYS:
526			if (!(sa->sa_flags & SA_RESTART))
527				goto no_system_call_restart;
528		/* fallthrough */
529		case -ERESTARTNOINTR:
530			regs->regs[0] = save_r0;
531			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
532			break;
533	}
534}
535
536/*
537 * OK, we're invoking a handler
538 */
539static int
540handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
541	      sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0)
542{
 
543	int ret;
544
545	/* Set up the stack frame */
546	if (ka->sa.sa_flags & SA_SIGINFO)
547		ret = setup_rt_frame(sig, ka, info, oldset, regs);
548	else
549		ret = setup_frame(sig, ka, oldset, regs);
550
551	if (ka->sa.sa_flags & SA_ONESHOT)
552		ka->sa.sa_handler = SIG_DFL;
553
554	if (ret == 0) {
555		spin_lock_irq(&current->sighand->siglock);
556		sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
557		if (!(ka->sa.sa_flags & SA_NODEFER))
558			sigaddset(&current->blocked,sig);
559		recalc_sigpending();
560		spin_unlock_irq(&current->sighand->siglock);
561	}
562
563	return ret;
564}
565
566/*
567 * Note that 'init' is a special process: it doesn't get signals it doesn't
568 * want to handle. Thus you cannot kill init even with a SIGKILL even by
569 * mistake.
570 *
571 * Note that we go through the signals twice: once to check the signals that
572 * the kernel can handle, and then we build all the user-level signal handling
573 * stack-frames in one go after that.
574 */
575static void do_signal(struct pt_regs *regs, unsigned int save_r0)
576{
577	siginfo_t info;
578	int signr;
579	struct k_sigaction ka;
580	sigset_t *oldset;
581
582	/*
583	 * We want the common case to go fast, which
584	 * is why we may in certain cases get here from
585	 * kernel mode. Just return without doing anything
586	 * if so.
587	 */
588	if (!user_mode(regs))
589		return;
590
591	if (try_to_freeze())
592		goto no_signal;
593
594	if (current_thread_info()->status & TS_RESTORE_SIGMASK)
595		oldset = &current->saved_sigmask;
596	else
597		oldset = &current->blocked;
598
599	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
600	if (signr > 0) {
601		handle_syscall_restart(save_r0, regs, &ka.sa);
602
603		/* Whee!  Actually deliver the signal.  */
604		if (handle_signal(signr, &ka, &info, oldset,
605				  regs, save_r0) == 0) {
606			/*
607			 * A signal was successfully delivered; the saved
608			 * sigmask will have been stored in the signal frame,
609			 * and will be restored by sigreturn, so we can simply
610			 * clear the TS_RESTORE_SIGMASK flag
611			 */
612			current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
613
614			tracehook_signal_handler(signr, &info, &ka, regs,
615					test_thread_flag(TIF_SINGLESTEP));
616		}
617
618		return;
619	}
620
621no_signal:
622	/* Did we come from a system call? */
623	if (regs->tra >= 0) {
624		/* Restart the system call - no handlers present */
625		if (regs->regs[0] == -ERESTARTNOHAND ||
626		    regs->regs[0] == -ERESTARTSYS ||
627		    regs->regs[0] == -ERESTARTNOINTR) {
628			regs->regs[0] = save_r0;
629			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
630		} else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
631			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
632			regs->regs[3] = __NR_restart_syscall;
633		}
634	}
635
636	/*
637	 * If there's no signal to deliver, we just put the saved sigmask
638	 * back.
639	 */
640	if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
641		current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
642		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
643	}
644}
645
646asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
647				 unsigned long thread_info_flags)
648{
649	/* deal with pending signal delivery */
650	if (thread_info_flags & _TIF_SIGPENDING)
651		do_signal(regs, save_r0);
652
653	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
654		clear_thread_flag(TIF_NOTIFY_RESUME);
655		tracehook_notify_resume(regs);
656		if (current->replacement_session_keyring)
657			key_replace_session_keyring();
658	}
659}