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