Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
signal.c
Go to the documentation of this file.
1 /* MN10300 Signal handling
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells ([email protected])
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/personality.h>
24 #include <linux/suspend.h>
25 #include <linux/tracehook.h>
26 #include <asm/cacheflush.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/fpu.h>
30 #include "sigframe.h"
31 
32 #define DEBUG_SIG 0
33 
34 /*
35  * atomically swap in the new signal mask, and wait for a signal.
36  */
37 asmlinkage long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
38 {
39  sigset_t blocked;
40  siginitset(&blocked, mask);
41  return sigsuspend(&blocked);
42 }
43 
44 /*
45  * set signal action syscall
46  */
48  const struct old_sigaction __user *act,
49  struct old_sigaction __user *oact)
50 {
51  struct k_sigaction new_ka, old_ka;
52  int ret;
53 
54  if (act) {
56  if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
57  __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
58  __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
59  __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
60  __get_user(mask, &act->sa_mask))
61  return -EFAULT;
62  siginitset(&new_ka.sa.sa_mask, mask);
63  }
64 
65  ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
66 
67  if (!ret && oact) {
68  if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
69  __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
70  __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
71  __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
72  __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
73  return -EFAULT;
74  }
75 
76  return ret;
77 }
78 
79 /*
80  * set alternate signal stack syscall
81  */
82 asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t *uoss)
83 {
84  return do_sigaltstack(uss, uoss, current_frame()->sp);
85 }
86 
87 /*
88  * do a signal return; undo the signal stack.
89  */
90 static int restore_sigcontext(struct pt_regs *regs,
91  struct sigcontext __user *sc, long *_d0)
92 {
93  unsigned int err = 0;
94 
95  /* Always make any pending restarted system calls return -EINTR */
96  current_thread_info()->restart_block.fn = do_no_restart_syscall;
97 
98  if (is_using_fpu(current))
100 
101 #define COPY(x) err |= __get_user(regs->x, &sc->x)
102  COPY(d1); COPY(d2); COPY(d3);
103  COPY(a0); COPY(a1); COPY(a2); COPY(a3);
104  COPY(e0); COPY(e1); COPY(e2); COPY(e3);
105  COPY(e4); COPY(e5); COPY(e6); COPY(e7);
106  COPY(lar); COPY(lir);
107  COPY(mdr); COPY(mdrq);
108  COPY(mcvf); COPY(mcrl); COPY(mcrh);
109  COPY(sp); COPY(pc);
110 #undef COPY
111 
112  {
113  unsigned int tmpflags;
114 #ifndef CONFIG_MN10300_USING_JTAG
115 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
116  EPSW_T | EPSW_nAR)
117 #else
118 #define USER_EPSW (EPSW_FLAG_Z | EPSW_FLAG_N | EPSW_FLAG_C | EPSW_FLAG_V | \
119  EPSW_nAR)
120 #endif
121  err |= __get_user(tmpflags, &sc->epsw);
122  regs->epsw = (regs->epsw & ~USER_EPSW) |
123  (tmpflags & USER_EPSW);
124  regs->orig_d0 = -1; /* disable syscall checks */
125  }
126 
127  {
128  struct fpucontext *buf;
129  err |= __get_user(buf, &sc->fpucontext);
130  if (buf) {
131  if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
132  goto badframe;
133  err |= fpu_restore_sigcontext(buf);
134  }
135  }
136 
137  err |= __get_user(*_d0, &sc->d0);
138  return err;
139 
140 badframe:
141  return 1;
142 }
143 
144 /*
145  * standard signal return syscall
146  */
148 {
149  struct sigframe __user *frame;
150  sigset_t set;
151  long d0;
152 
153  frame = (struct sigframe __user *) current_frame()->sp;
154  if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
155  goto badframe;
156  if (__get_user(set.sig[0], &frame->sc.oldmask))
157  goto badframe;
158 
159  if (_NSIG_WORDS > 1 &&
160  __copy_from_user(&set.sig[1], &frame->extramask,
161  sizeof(frame->extramask)))
162  goto badframe;
163 
164  set_current_blocked(&set);
165 
166  if (restore_sigcontext(current_frame(), &frame->sc, &d0))
167  goto badframe;
168 
169  return d0;
170 
171 badframe:
173  return 0;
174 }
175 
176 /*
177  * realtime signal return syscall
178  */
180 {
181  struct rt_sigframe __user *frame;
182  sigset_t set;
183  long d0;
184 
185  frame = (struct rt_sigframe __user *) current_frame()->sp;
186  if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
187  goto badframe;
188  if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
189  goto badframe;
190 
191  set_current_blocked(&set);
192 
193  if (restore_sigcontext(current_frame(), &frame->uc.uc_mcontext, &d0))
194  goto badframe;
195 
196  if (do_sigaltstack(&frame->uc.uc_stack, NULL, current_frame()->sp) ==
197  -EFAULT)
198  goto badframe;
199 
200  return d0;
201 
202 badframe:
204  return 0;
205 }
206 
207 /*
208  * store the userspace context into a signal frame
209  */
210 static int setup_sigcontext(struct sigcontext __user *sc,
211  struct fpucontext *fpuctx,
212  struct pt_regs *regs,
213  unsigned long mask)
214 {
215  int tmp, err = 0;
216 
217 #define COPY(x) err |= __put_user(regs->x, &sc->x)
218  COPY(d0); COPY(d1); COPY(d2); COPY(d3);
219  COPY(a0); COPY(a1); COPY(a2); COPY(a3);
220  COPY(e0); COPY(e1); COPY(e2); COPY(e3);
221  COPY(e4); COPY(e5); COPY(e6); COPY(e7);
222  COPY(lar); COPY(lir);
223  COPY(mdr); COPY(mdrq);
224  COPY(mcvf); COPY(mcrl); COPY(mcrh);
225  COPY(sp); COPY(epsw); COPY(pc);
226 #undef COPY
227 
228  tmp = fpu_setup_sigcontext(fpuctx);
229  if (tmp < 0)
230  err = 1;
231  else
232  err |= __put_user(tmp ? fpuctx : NULL, &sc->fpucontext);
233 
234  /* non-iBCS2 extensions.. */
235  err |= __put_user(mask, &sc->oldmask);
236 
237  return err;
238 }
239 
240 /*
241  * determine which stack to use..
242  */
243 static inline void __user *get_sigframe(struct k_sigaction *ka,
244  struct pt_regs *regs,
245  size_t frame_size)
246 {
247  unsigned long sp;
248 
249  /* default to using normal stack */
250  sp = regs->sp;
251 
252  /* this is the X/Open sanctioned signal stack switching. */
253  if (ka->sa.sa_flags & SA_ONSTACK) {
254  if (sas_ss_flags(sp) == 0)
255  sp = current->sas_ss_sp + current->sas_ss_size;
256  }
257 
258  return (void __user *) ((sp - frame_size) & ~7UL);
259 }
260 
261 /*
262  * set up a normal signal frame
263  */
264 static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
265  struct pt_regs *regs)
266 {
267  struct sigframe __user *frame;
268  int rsig;
269 
270  frame = get_sigframe(ka, regs, sizeof(*frame));
271 
272  if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
273  goto give_sigsegv;
274 
275  rsig = sig;
276  if (sig < 32 &&
279  rsig = current_thread_info()->exec_domain->signal_invmap[sig];
280 
281  if (__put_user(rsig, &frame->sig) < 0 ||
282  __put_user(&frame->sc, &frame->psc) < 0)
283  goto give_sigsegv;
284 
285  if (setup_sigcontext(&frame->sc, &frame->fpuctx, regs, set->sig[0]))
286  goto give_sigsegv;
287 
288  if (_NSIG_WORDS > 1) {
289  if (__copy_to_user(frame->extramask, &set->sig[1],
290  sizeof(frame->extramask)))
291  goto give_sigsegv;
292  }
293 
294  /* set up to return from userspace. If provided, use a stub already in
295  * userspace */
296  if (ka->sa.sa_flags & SA_RESTORER) {
297  if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
298  goto give_sigsegv;
299  } else {
300  if (__put_user((void (*)(void))frame->retcode,
301  &frame->pretcode))
302  goto give_sigsegv;
303  /* this is mov $,d0; syscall 0 */
304  if (__put_user(0x2c, (char *)(frame->retcode + 0)) ||
305  __put_user(__NR_sigreturn, (char *)(frame->retcode + 1)) ||
306  __put_user(0x00, (char *)(frame->retcode + 2)) ||
307  __put_user(0xf0, (char *)(frame->retcode + 3)) ||
308  __put_user(0xe0, (char *)(frame->retcode + 4)))
309  goto give_sigsegv;
310  flush_icache_range((unsigned long) frame->retcode,
311  (unsigned long) frame->retcode + 5);
312  }
313 
314  /* set up registers for signal handler */
315  regs->sp = (unsigned long) frame;
316  regs->pc = (unsigned long) ka->sa.sa_handler;
317  regs->d0 = sig;
318  regs->d1 = (unsigned long) &frame->sc;
319 
320 #if DEBUG_SIG
321  printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
322  sig, current->comm, current->pid, frame, regs->pc,
323  frame->pretcode);
324 #endif
325 
326  return 0;
327 
328 give_sigsegv:
329  force_sigsegv(sig, current);
330  return -EFAULT;
331 }
332 
333 /*
334  * set up a realtime signal frame
335  */
336 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
337  sigset_t *set, struct pt_regs *regs)
338 {
339  struct rt_sigframe __user *frame;
340  int rsig;
341 
342  frame = get_sigframe(ka, regs, sizeof(*frame));
343 
344  if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
345  goto give_sigsegv;
346 
347  rsig = sig;
348  if (sig < 32 &&
351  rsig = current_thread_info()->exec_domain->signal_invmap[sig];
352 
353  if (__put_user(rsig, &frame->sig) ||
354  __put_user(&frame->info, &frame->pinfo) ||
355  __put_user(&frame->uc, &frame->puc) ||
356  copy_siginfo_to_user(&frame->info, info))
357  goto give_sigsegv;
358 
359  /* create the ucontext. */
360  if (__put_user(0, &frame->uc.uc_flags) ||
361  __put_user(0, &frame->uc.uc_link) ||
362  __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
363  __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
364  __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size) ||
365  setup_sigcontext(&frame->uc.uc_mcontext,
366  &frame->fpuctx, regs, set->sig[0]) ||
367  __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
368  goto give_sigsegv;
369 
370  /* set up to return from userspace. If provided, use a stub already in
371  * userspace */
372  if (ka->sa.sa_flags & SA_RESTORER) {
373  if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
374  goto give_sigsegv;
375  } else {
376  if (__put_user((void(*)(void))frame->retcode,
377  &frame->pretcode) ||
378  /* This is mov $,d0; syscall 0 */
379  __put_user(0x2c, (char *)(frame->retcode + 0)) ||
381  (char *)(frame->retcode + 1)) ||
382  __put_user(0x00, (char *)(frame->retcode + 2)) ||
383  __put_user(0xf0, (char *)(frame->retcode + 3)) ||
384  __put_user(0xe0, (char *)(frame->retcode + 4)))
385  goto give_sigsegv;
386 
388  (u_long) frame->retcode + 5);
389  }
390 
391  /* Set up registers for signal handler */
392  regs->sp = (unsigned long) frame;
393  regs->pc = (unsigned long) ka->sa.sa_handler;
394  regs->d0 = sig;
395  regs->d1 = (long) &frame->info;
396 
397 #if DEBUG_SIG
398  printk(KERN_DEBUG "SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
399  sig, current->comm, current->pid, frame, regs->pc,
400  frame->pretcode);
401 #endif
402 
403  return 0;
404 
405 give_sigsegv:
406  force_sigsegv(sig, current);
407  return -EFAULT;
408 }
409 
410 static inline void stepback(struct pt_regs *regs)
411 {
412  regs->pc -= 2;
413  regs->orig_d0 = -1;
414 }
415 
416 /*
417  * handle the actual delivery of a signal to userspace
418  */
419 static int handle_signal(int sig,
420  siginfo_t *info, struct k_sigaction *ka,
421  struct pt_regs *regs)
422 {
423  sigset_t *oldset = sigmask_to_save();
424  int ret;
425 
426  /* Are we from a system call? */
427  if (regs->orig_d0 >= 0) {
428  /* If so, check system call restarting.. */
429  switch (regs->d0) {
430  case -ERESTART_RESTARTBLOCK:
431  case -ERESTARTNOHAND:
432  regs->d0 = -EINTR;
433  break;
434 
435  case -ERESTARTSYS:
436  if (!(ka->sa.sa_flags & SA_RESTART)) {
437  regs->d0 = -EINTR;
438  break;
439  }
440 
441  /* fallthrough */
442  case -ERESTARTNOINTR:
443  regs->d0 = regs->orig_d0;
444  stepback(regs);
445  }
446  }
447 
448  /* Set up the stack frame */
449  if (ka->sa.sa_flags & SA_SIGINFO)
450  ret = setup_rt_frame(sig, ka, info, oldset, regs);
451  else
452  ret = setup_frame(sig, ka, oldset, regs);
453  if (ret)
454  return ret;
455 
456  signal_delivered(sig, info, ka, regs,
457  test_thread_flag(TIF_SINGLESTEP));
458  return 0;
459 }
460 
461 /*
462  * handle a potential signal
463  */
464 static void do_signal(struct pt_regs *regs)
465 {
466  struct k_sigaction ka;
467  siginfo_t info;
468  int signr;
469 
470  signr = get_signal_to_deliver(&info, &ka, regs, NULL);
471  if (signr > 0) {
472  if (handle_signal(signr, &info, &ka, regs) == 0) {
473  }
474 
475  return;
476  }
477 
478  /* did we come from a system call? */
479  if (regs->orig_d0 >= 0) {
480  /* restart the system call - no handlers present */
481  switch (regs->d0) {
482  case -ERESTARTNOHAND:
483  case -ERESTARTSYS:
484  case -ERESTARTNOINTR:
485  regs->d0 = regs->orig_d0;
486  stepback(regs);
487  break;
488 
489  case -ERESTART_RESTARTBLOCK:
490  regs->d0 = __NR_restart_syscall;
491  stepback(regs);
492  break;
493  }
494  }
495 
496  /* if there's no signal to deliver, we just put the saved sigmask
497  * back */
498  restore_saved_sigmask();
499 }
500 
501 /*
502  * notification of userspace execution resumption
503  * - triggered by current->work.notify_resume
504  */
505 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
506 {
507  /* Pending single-step? */
508  if (thread_info_flags & _TIF_SINGLESTEP) {
509 #ifndef CONFIG_MN10300_USING_JTAG
510  regs->epsw |= EPSW_T;
511  clear_thread_flag(TIF_SINGLESTEP);
512 #else
513  BUG(); /* no h/w single-step if using JTAG unit */
514 #endif
515  }
516 
517  /* deal with pending signal delivery */
518  if (thread_info_flags & _TIF_SIGPENDING)
519  do_signal(regs);
520 
521  if (thread_info_flags & _TIF_NOTIFY_RESUME) {
522  clear_thread_flag(TIF_NOTIFY_RESUME);
523  tracehook_notify_resume(current_frame());
524  }
525 }