PageRenderTime 47ms CodeModel.GetById 17ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/sh/kernel/signal_32.c

https://gitlab.com/teobaluta/opw
C | 545 lines | 380 code | 91 blank | 74 comment | 56 complexity | 017896b99e7d55443a66bdb7830cdd29 MD5 | raw file
  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/io.h>
 26#include <linux/tracehook.h>
 27#include <asm/ucontext.h>
 28#include <asm/uaccess.h>
 29#include <asm/pgtable.h>
 30#include <asm/cacheflush.h>
 31#include <asm/syscalls.h>
 32#include <asm/fpu.h>
 33
 34struct fdpic_func_descriptor {
 35	unsigned long	text;
 36	unsigned long	GOT;
 37};
 38
 39/*
 40 * The following define adds a 64 byte gap between the signal
 41 * stack frame and previous contents of the stack.  This allows
 42 * frame unwinding in a function epilogue but only if a frame
 43 * pointer is used in the function.  This is necessary because
 44 * current gcc compilers (<4.3) do not generate unwind info on
 45 * SH for function epilogues.
 46 */
 47#define UNWINDGUARD 64
 48
 49/*
 50 * Do a signal return; undo the signal stack.
 51 */
 52
 53#define MOVW(n)	 (0x9300|((n)-2))	/* Move mem word at PC+n to R3 */
 54#if defined(CONFIG_CPU_SH2)
 55#define TRAP_NOARG 0xc320		/* Syscall w/no args (NR in R3) */
 56#else
 57#define TRAP_NOARG 0xc310		/* Syscall w/no args (NR in R3) */
 58#endif
 59#define OR_R0_R0 0x200b			/* or r0,r0 (insert to avoid hardware bug) */
 60
 61struct sigframe
 62{
 63	struct sigcontext sc;
 64	unsigned long extramask[_NSIG_WORDS-1];
 65	u16 retcode[8];
 66};
 67
 68struct rt_sigframe
 69{
 70	struct siginfo info;
 71	struct ucontext uc;
 72	u16 retcode[8];
 73};
 74
 75#ifdef CONFIG_SH_FPU
 76static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
 77{
 78	struct task_struct *tsk = current;
 79
 80	if (!(boot_cpu_data.flags & CPU_HAS_FPU))
 81		return 0;
 82
 83	set_used_math();
 84	return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
 85				sizeof(long)*(16*2+2));
 86}
 87
 88static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
 89				      struct pt_regs *regs)
 90{
 91	struct task_struct *tsk = current;
 92
 93	if (!(boot_cpu_data.flags & CPU_HAS_FPU))
 94		return 0;
 95
 96	if (!used_math())
 97		return __put_user(0, &sc->sc_ownedfp);
 98
 99	if (__put_user(1, &sc->sc_ownedfp))
100		return -EFAULT;
101
102	/* This will cause a "finit" to be triggered by the next
103	   attempted FPU operation by the 'current' process.
104	   */
105	clear_used_math();
106
107	unlazy_fpu(tsk, regs);
108	return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
109			      sizeof(long)*(16*2+2));
110}
111#endif /* CONFIG_SH_FPU */
112
113static int
114restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
115{
116	unsigned int err = 0;
117
118#define COPY(x)		err |= __get_user(regs->x, &sc->sc_##x)
119			COPY(regs[1]);
120	COPY(regs[2]);	COPY(regs[3]);
121	COPY(regs[4]);	COPY(regs[5]);
122	COPY(regs[6]);	COPY(regs[7]);
123	COPY(regs[8]);	COPY(regs[9]);
124	COPY(regs[10]);	COPY(regs[11]);
125	COPY(regs[12]);	COPY(regs[13]);
126	COPY(regs[14]);	COPY(regs[15]);
127	COPY(gbr);	COPY(mach);
128	COPY(macl);	COPY(pr);
129	COPY(sr);	COPY(pc);
130#undef COPY
131
132#ifdef CONFIG_SH_FPU
133	if (boot_cpu_data.flags & CPU_HAS_FPU) {
134		int owned_fp;
135		struct task_struct *tsk = current;
136
137		regs->sr |= SR_FD; /* Release FPU */
138		clear_fpu(tsk, regs);
139		clear_used_math();
140		err |= __get_user (owned_fp, &sc->sc_ownedfp);
141		if (owned_fp)
142			err |= restore_sigcontext_fpu(sc);
143	}
144#endif
145
146	regs->tra = -1;		/* disable syscall checks */
147	err |= __get_user(*r0_p, &sc->sc_regs[0]);
148	return err;
149}
150
151asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
152			     unsigned long r6, unsigned long r7,
153			     struct pt_regs __regs)
154{
155	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
156	struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
157	sigset_t set;
158	int r0;
159
160        /* Always make any pending restarted system calls return -EINTR */
161	current_thread_info()->restart_block.fn = do_no_restart_syscall;
162
163	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
164		goto badframe;
165
166	if (__get_user(set.sig[0], &frame->sc.oldmask)
167	    || (_NSIG_WORDS > 1
168		&& __copy_from_user(&set.sig[1], &frame->extramask,
169				    sizeof(frame->extramask))))
170		goto badframe;
171
172	set_current_blocked(&set);
173
174	if (restore_sigcontext(regs, &frame->sc, &r0))
175		goto badframe;
176	return r0;
177
178badframe:
179	force_sig(SIGSEGV, current);
180	return 0;
181}
182
183asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
184				unsigned long r6, unsigned long r7,
185				struct pt_regs __regs)
186{
187	struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
188	struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
189	sigset_t set;
190	int r0;
191
192	/* Always make any pending restarted system calls return -EINTR */
193	current_thread_info()->restart_block.fn = do_no_restart_syscall;
194
195	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
196		goto badframe;
197
198	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
199		goto badframe;
200
201	set_current_blocked(&set);
202
203	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
204		goto badframe;
205
206	if (restore_altstack(&frame->uc.uc_stack))
207		goto badframe;
208
209	return r0;
210
211badframe:
212	force_sig(SIGSEGV, current);
213	return 0;
214}
215
216/*
217 * Set up a signal frame.
218 */
219
220static int
221setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
222		 unsigned long mask)
223{
224	int err = 0;
225
226#define COPY(x)		err |= __put_user(regs->x, &sc->sc_##x)
227	COPY(regs[0]);	COPY(regs[1]);
228	COPY(regs[2]);	COPY(regs[3]);
229	COPY(regs[4]);	COPY(regs[5]);
230	COPY(regs[6]);	COPY(regs[7]);
231	COPY(regs[8]);	COPY(regs[9]);
232	COPY(regs[10]);	COPY(regs[11]);
233	COPY(regs[12]);	COPY(regs[13]);
234	COPY(regs[14]);	COPY(regs[15]);
235	COPY(gbr);	COPY(mach);
236	COPY(macl);	COPY(pr);
237	COPY(sr);	COPY(pc);
238#undef COPY
239
240#ifdef CONFIG_SH_FPU
241	err |= save_sigcontext_fpu(sc, regs);
242#endif
243
244	/* non-iBCS2 extensions.. */
245	err |= __put_user(mask, &sc->oldmask);
246
247	return err;
248}
249
250/*
251 * Determine which stack to use..
252 */
253static inline void __user *
254get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
255{
256	if (ka->sa.sa_flags & SA_ONSTACK) {
257		if (sas_ss_flags(sp) == 0)
258			sp = current->sas_ss_sp + current->sas_ss_size;
259	}
260
261	return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
262}
263
264/* These symbols are defined with the addresses in the vsyscall page.
265   See vsyscall-trapa.S.  */
266extern void __kernel_sigreturn(void);
267extern void __kernel_rt_sigreturn(void);
268
269static int setup_frame(int sig, struct k_sigaction *ka,
270			sigset_t *set, struct pt_regs *regs)
271{
272	struct sigframe __user *frame;
273	int err = 0;
274	int signal;
275
276	frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
277
278	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
279		goto give_sigsegv;
280
281	signal = current_thread_info()->exec_domain
282		&& current_thread_info()->exec_domain->signal_invmap
283		&& sig < 32
284		? current_thread_info()->exec_domain->signal_invmap[sig]
285		: sig;
286
287	err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
288
289	if (_NSIG_WORDS > 1)
290		err |= __copy_to_user(frame->extramask, &set->sig[1],
291				      sizeof(frame->extramask));
292
293	/* Set up to return from userspace.  If provided, use a stub
294	   already in userspace.  */
295	if (ka->sa.sa_flags & SA_RESTORER) {
296		regs->pr = (unsigned long) ka->sa.sa_restorer;
297#ifdef CONFIG_VSYSCALL
298	} else if (likely(current->mm->context.vdso)) {
299		regs->pr = VDSO_SYM(&__kernel_sigreturn);
300#endif
301	} else {
302		/* Generate return code (system call to sigreturn) */
303		err |= __put_user(MOVW(7), &frame->retcode[0]);
304		err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
305		err |= __put_user(OR_R0_R0, &frame->retcode[2]);
306		err |= __put_user(OR_R0_R0, &frame->retcode[3]);
307		err |= __put_user(OR_R0_R0, &frame->retcode[4]);
308		err |= __put_user(OR_R0_R0, &frame->retcode[5]);
309		err |= __put_user(OR_R0_R0, &frame->retcode[6]);
310		err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
311		regs->pr = (unsigned long) frame->retcode;
312		flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
313	}
314
315	if (err)
316		goto give_sigsegv;
317
318	/* Set up registers for signal handler */
319	regs->regs[15] = (unsigned long) frame;
320	regs->regs[4] = signal; /* Arg for signal handler */
321	regs->regs[5] = 0;
322	regs->regs[6] = (unsigned long) &frame->sc;
323
324	if (current->personality & FDPIC_FUNCPTRS) {
325		struct fdpic_func_descriptor __user *funcptr =
326			(struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
327
328		err |= __get_user(regs->pc, &funcptr->text);
329		err |= __get_user(regs->regs[12], &funcptr->GOT);
330	} else
331		regs->pc = (unsigned long)ka->sa.sa_handler;
332
333	if (err)
334		goto give_sigsegv;
335
336	set_fs(USER_DS);
337
338	pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
339		 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
340
341	return 0;
342
343give_sigsegv:
344	force_sigsegv(sig, current);
345	return -EFAULT;
346}
347
348static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
349			   sigset_t *set, struct pt_regs *regs)
350{
351	struct rt_sigframe __user *frame;
352	int err = 0;
353	int signal;
354
355	frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
356
357	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
358		goto give_sigsegv;
359
360	signal = current_thread_info()->exec_domain
361		&& current_thread_info()->exec_domain->signal_invmap
362		&& sig < 32
363		? current_thread_info()->exec_domain->signal_invmap[sig]
364		: sig;
365
366	err |= copy_siginfo_to_user(&frame->info, info);
367
368	/* Create the ucontext.  */
369	err |= __put_user(0, &frame->uc.uc_flags);
370	err |= __put_user(NULL, &frame->uc.uc_link);
371	err |= __save_altstack(&frame->uc.uc_stack, regs->regs[15]);
372	err |= setup_sigcontext(&frame->uc.uc_mcontext,
373			        regs, set->sig[0]);
374	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
375
376	/* Set up to return from userspace.  If provided, use a stub
377	   already in userspace.  */
378	if (ka->sa.sa_flags & SA_RESTORER) {
379		regs->pr = (unsigned long) ka->sa.sa_restorer;
380#ifdef CONFIG_VSYSCALL
381	} else if (likely(current->mm->context.vdso)) {
382		regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
383#endif
384	} else {
385		/* Generate return code (system call to rt_sigreturn) */
386		err |= __put_user(MOVW(7), &frame->retcode[0]);
387		err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
388		err |= __put_user(OR_R0_R0, &frame->retcode[2]);
389		err |= __put_user(OR_R0_R0, &frame->retcode[3]);
390		err |= __put_user(OR_R0_R0, &frame->retcode[4]);
391		err |= __put_user(OR_R0_R0, &frame->retcode[5]);
392		err |= __put_user(OR_R0_R0, &frame->retcode[6]);
393		err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
394		regs->pr = (unsigned long) frame->retcode;
395		flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
396	}
397
398	if (err)
399		goto give_sigsegv;
400
401	/* Set up registers for signal handler */
402	regs->regs[15] = (unsigned long) frame;
403	regs->regs[4] = signal; /* Arg for signal handler */
404	regs->regs[5] = (unsigned long) &frame->info;
405	regs->regs[6] = (unsigned long) &frame->uc;
406
407	if (current->personality & FDPIC_FUNCPTRS) {
408		struct fdpic_func_descriptor __user *funcptr =
409			(struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
410
411		err |= __get_user(regs->pc, &funcptr->text);
412		err |= __get_user(regs->regs[12], &funcptr->GOT);
413	} else
414		regs->pc = (unsigned long)ka->sa.sa_handler;
415
416	if (err)
417		goto give_sigsegv;
418
419	set_fs(USER_DS);
420
421	pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
422		 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
423
424	return 0;
425
426give_sigsegv:
427	force_sigsegv(sig, current);
428	return -EFAULT;
429}
430
431static inline void
432handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
433		       struct sigaction *sa)
434{
435	/* If we're not from a syscall, bail out */
436	if (regs->tra < 0)
437		return;
438
439	/* check for system call restart.. */
440	switch (regs->regs[0]) {
441		case -ERESTART_RESTARTBLOCK:
442		case -ERESTARTNOHAND:
443		no_system_call_restart:
444			regs->regs[0] = -EINTR;
445			break;
446
447		case -ERESTARTSYS:
448			if (!(sa->sa_flags & SA_RESTART))
449				goto no_system_call_restart;
450		/* fallthrough */
451		case -ERESTARTNOINTR:
452			regs->regs[0] = save_r0;
453			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
454			break;
455	}
456}
457
458/*
459 * OK, we're invoking a handler
460 */
461static void
462handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
463	      struct pt_regs *regs, unsigned int save_r0)
464{
465	sigset_t *oldset = sigmask_to_save();
466	int ret;
467
468	/* Set up the stack frame */
469	if (ka->sa.sa_flags & SA_SIGINFO)
470		ret = setup_rt_frame(sig, ka, info, oldset, regs);
471	else
472		ret = setup_frame(sig, ka, oldset, regs);
473
474	if (ret)
475		return;
476	signal_delivered(sig, info, ka, regs,
477			test_thread_flag(TIF_SINGLESTEP));
478}
479
480/*
481 * Note that 'init' is a special process: it doesn't get signals it doesn't
482 * want to handle. Thus you cannot kill init even with a SIGKILL even by
483 * mistake.
484 *
485 * Note that we go through the signals twice: once to check the signals that
486 * the kernel can handle, and then we build all the user-level signal handling
487 * stack-frames in one go after that.
488 */
489static void do_signal(struct pt_regs *regs, unsigned int save_r0)
490{
491	siginfo_t info;
492	int signr;
493	struct k_sigaction ka;
494
495	/*
496	 * We want the common case to go fast, which
497	 * is why we may in certain cases get here from
498	 * kernel mode. Just return without doing anything
499	 * if so.
500	 */
501	if (!user_mode(regs))
502		return;
503
504	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
505	if (signr > 0) {
506		handle_syscall_restart(save_r0, regs, &ka.sa);
507
508		/* Whee!  Actually deliver the signal.  */
509		handle_signal(signr, &ka, &info, regs, save_r0);
510		return;
511	}
512
513	/* Did we come from a system call? */
514	if (regs->tra >= 0) {
515		/* Restart the system call - no handlers present */
516		if (regs->regs[0] == -ERESTARTNOHAND ||
517		    regs->regs[0] == -ERESTARTSYS ||
518		    regs->regs[0] == -ERESTARTNOINTR) {
519			regs->regs[0] = save_r0;
520			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
521		} else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
522			regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
523			regs->regs[3] = __NR_restart_syscall;
524		}
525	}
526
527	/*
528	 * If there's no signal to deliver, we just put the saved sigmask
529	 * back.
530	 */
531	restore_saved_sigmask();
532}
533
534asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
535				 unsigned long thread_info_flags)
536{
537	/* deal with pending signal delivery */
538	if (thread_info_flags & _TIF_SIGPENDING)
539		do_signal(regs, save_r0);
540
541	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
542		clear_thread_flag(TIF_NOTIFY_RESUME);
543		tracehook_notify_resume(regs);
544	}
545}