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 /*
2  * linux/arch/cris/kernel/signal.c
3  *
4  * Based on arch/i386/kernel/signal.c by
5  * Copyright (C) 1991, 1992 Linus Torvalds
6  * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson *
7  *
8  * Ideas also taken from arch/arm.
9  *
10  * Copyright (C) 2000-2007 Axis Communications AB
11  *
12  * Authors: Bjorn Wesen ([email protected])
13  *
14  */
15 
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/smp.h>
19 #include <linux/kernel.h>
20 #include <linux/signal.h>
21 #include <linux/errno.h>
22 #include <linux/wait.h>
23 #include <linux/ptrace.h>
24 #include <linux/unistd.h>
25 #include <linux/stddef.h>
26 
27 #include <asm/processor.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
30 #include <arch/system.h>
31 
32 #define DEBUG_SIG 0
33 
34 /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
35 /* manipulate regs so that upon return, it will be re-executed */
36 
37 /* We rely on that pc points to the instruction after "break 13", so the
38  * library must never do strange things like putting it in a delay slot.
39  */
40 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
41 
42 void do_signal(int canrestart, struct pt_regs *regs);
43 
44 /*
45  * Atomically swap in the new signal mask, and wait for a signal. Define
46  * dummy arguments to be able to reach the regs argument. (Note that this
47  * arrangement relies on old_sigset_t occupying one register.)
48  */
50 {
51  sigset_t blocked;
52  siginitset(&blocked, mask);
53  return sigsuspend(&blocked);
54 }
55 
56 int sys_sigaction(int sig, const struct old_sigaction __user *act,
57  struct old_sigaction *oact)
58 {
59  struct k_sigaction new_ka, old_ka;
60  int ret;
61 
62  if (act) {
64  if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
65  __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
66  __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
67  __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
68  __get_user(mask, &act->sa_mask))
69  return -EFAULT;
70  siginitset(&new_ka.sa.sa_mask, mask);
71  }
72 
73  ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
74 
75  if (!ret && oact) {
76  if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
77  __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
78  __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
79  __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
80  __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
81  return -EFAULT;
82  }
83 
84  return ret;
85 }
86 
87 int sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
88 {
89  return do_sigaltstack(uss, uoss, rdusp());
90 }
91 
92 
93 /*
94  * Do a signal return; undo the signal stack.
95  */
96 
97 struct sigframe {
98  struct sigcontext sc;
99  unsigned long extramask[_NSIG_WORDS-1];
100  unsigned char retcode[8]; /* trampoline code */
101 };
102 
103 struct rt_sigframe {
104  struct siginfo *pinfo;
105  void *puc;
106  struct siginfo info;
107  struct ucontext uc;
108  unsigned char retcode[8]; /* trampoline code */
109 };
110 
111 
112 static int
113 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
114 {
115  unsigned int err = 0;
116  unsigned long old_usp;
117 
118  /* Always make any pending restarted system calls return -EINTR */
119  current_thread_info()->restart_block.fn = do_no_restart_syscall;
120 
121  /* restore the regs from &sc->regs (same as sc, since regs is first)
122  * (sc is already checked for VERIFY_READ since the sigframe was
123  * checked in sys_sigreturn previously)
124  */
125 
126  if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
127  goto badframe;
128 
129  /* make sure the U-flag is set so user-mode cannot fool us */
130 
131  regs->dccr |= 1 << 8;
132 
133  /* restore the old USP as it was before we stacked the sc etc.
134  * (we cannot just pop the sigcontext since we aligned the sp and
135  * stuff after pushing it)
136  */
137 
138  err |= __get_user(old_usp, &sc->usp);
139 
140  wrusp(old_usp);
141 
142  /* TODO: the other ports use regs->orig_XX to disable syscall checks
143  * after this completes, but we don't use that mechanism. maybe we can
144  * use it now ?
145  */
146 
147  return err;
148 
149 badframe:
150  return 1;
151 }
152 
153 /* Define dummy arguments to be able to reach the regs argument. */
154 
155 asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
156  long srp, struct pt_regs *regs)
157 {
158  struct sigframe __user *frame = (struct sigframe *)rdusp();
159  sigset_t set;
160 
161  /*
162  * Since we stacked the signal on a dword boundary,
163  * then frame should be dword aligned here. If it's
164  * not, then the user is trying to mess with us.
165  */
166  if (((long)frame) & 3)
167  goto badframe;
168 
169  if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
170  goto badframe;
171  if (__get_user(set.sig[0], &frame->sc.oldmask)
172  || (_NSIG_WORDS > 1
173  && __copy_from_user(&set.sig[1], frame->extramask,
174  sizeof(frame->extramask))))
175  goto badframe;
176 
177  set_current_blocked(&set);
178 
179  if (restore_sigcontext(regs, &frame->sc))
180  goto badframe;
181 
182  /* TODO: SIGTRAP when single-stepping as in arm ? */
183 
184  return regs->r10;
185 
186 badframe:
188  return 0;
189 }
190 
191 /* Define dummy arguments to be able to reach the regs argument. */
192 
193 asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
194  long mof, long srp, struct pt_regs *regs)
195 {
196  struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
197  sigset_t set;
198 
199  /*
200  * Since we stacked the signal on a dword boundary,
201  * then frame should be dword aligned here. If it's
202  * not, then the user is trying to mess with us.
203  */
204  if (((long)frame) & 3)
205  goto badframe;
206 
207  if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
208  goto badframe;
209  if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
210  goto badframe;
211 
212  set_current_blocked(&set);
213 
214  if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
215  goto badframe;
216 
217  if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
218  goto badframe;
219 
220  return regs->r10;
221 
222 badframe:
224  return 0;
225 }
226 
227 /*
228  * Set up a signal frame.
229  */
230 
231 static int setup_sigcontext(struct sigcontext __user *sc,
232  struct pt_regs *regs, unsigned long mask)
233 {
234  int err = 0;
235  unsigned long usp = rdusp();
236 
237  /* copy the regs. they are first in sc so we can use sc directly */
238 
239  err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
240 
241  /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
242  the signal handler. The frametype will be restored to its previous
243  value in restore_sigcontext. */
245 
246  /* then some other stuff */
247 
248  err |= __put_user(mask, &sc->oldmask);
249 
250  err |= __put_user(usp, &sc->usp);
251 
252  return err;
253 }
254 
255 /* Figure out where we want to put the new signal frame
256  * - usually on the stack. */
257 
258 static inline void __user *
259 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
260 {
261  unsigned long sp = rdusp();
262 
263  /* This is the X/Open sanctioned signal stack switching. */
264  if (ka->sa.sa_flags & SA_ONSTACK) {
265  if (! on_sig_stack(sp))
266  sp = current->sas_ss_sp + current->sas_ss_size;
267  }
268 
269  /* make sure the frame is dword-aligned */
270 
271  sp &= ~3;
272 
273  return (void __user*)(sp - frame_size);
274 }
275 
276 /* grab and setup a signal frame.
277  *
278  * basically we stack a lot of state info, and arrange for the
279  * user-mode program to return to the kernel using either a
280  * trampoline which performs the syscall sigreturn, or a provided
281  * user-mode trampoline.
282  */
283 
284 static int setup_frame(int sig, struct k_sigaction *ka,
285  sigset_t *set, struct pt_regs *regs)
286 {
287  struct sigframe __user *frame;
288  unsigned long return_ip;
289  int err = 0;
290 
291  frame = get_sigframe(ka, regs, sizeof(*frame));
292 
293  if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
294  goto give_sigsegv;
295 
296  err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
297  if (err)
298  goto give_sigsegv;
299 
300  if (_NSIG_WORDS > 1) {
301  err |= __copy_to_user(frame->extramask, &set->sig[1],
302  sizeof(frame->extramask));
303  }
304  if (err)
305  goto give_sigsegv;
306 
307  /* Set up to return from userspace. If provided, use a stub
308  already in userspace. */
309  if (ka->sa.sa_flags & SA_RESTORER) {
310  return_ip = (unsigned long)ka->sa.sa_restorer;
311  } else {
312  /* trampoline - the desired return ip is the retcode itself */
313  return_ip = (unsigned long)&frame->retcode;
314  /* This is movu.w __NR_sigreturn, r9; break 13; */
315  err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
316  err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
317  err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
318  }
319 
320  if (err)
321  goto give_sigsegv;
322 
323  /* Set up registers for signal handler */
324 
325  regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
326  regs->srp = return_ip; /* what we enter LATER */
327  regs->r10 = sig; /* first argument is signo */
328 
329  /* actually move the usp to reflect the stacked frame */
330 
331  wrusp((unsigned long)frame);
332 
333  return 0;
334 
335 give_sigsegv:
336  force_sigsegv(sig, current);
337  return -EFAULT;
338 }
339 
340 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
341  sigset_t *set, struct pt_regs *regs)
342 {
343  struct rt_sigframe __user *frame;
344  unsigned long return_ip;
345  int err = 0;
346 
347  frame = get_sigframe(ka, regs, sizeof(*frame));
348 
349  if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
350  goto give_sigsegv;
351 
352  err |= __put_user(&frame->info, &frame->pinfo);
353  err |= __put_user(&frame->uc, &frame->puc);
354  err |= copy_siginfo_to_user(&frame->info, info);
355  if (err)
356  goto give_sigsegv;
357 
358  /* Clear all the bits of the ucontext we don't use. */
359  err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
360 
361  err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
362 
363  err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
364 
365  if (err)
366  goto give_sigsegv;
367 
368  /* Set up to return from userspace. If provided, use a stub
369  already in userspace. */
370  if (ka->sa.sa_flags & SA_RESTORER) {
371  return_ip = (unsigned long)ka->sa.sa_restorer;
372  } else {
373  /* trampoline - the desired return ip is the retcode itself */
374  return_ip = (unsigned long)&frame->retcode;
375  /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
376  err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
378  (short __user *)(frame->retcode+2));
379  err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
380  }
381 
382  if (err)
383  goto give_sigsegv;
384 
385  /* TODO what is the current->exec_domain stuff and invmap ? */
386 
387  /* Set up registers for signal handler */
388 
389  /* What we enter NOW */
390  regs->irp = (unsigned long) ka->sa.sa_handler;
391  /* What we enter LATER */
392  regs->srp = return_ip;
393  /* First argument is signo */
394  regs->r10 = sig;
395  /* Second argument is (siginfo_t *) */
396  regs->r11 = (unsigned long)&frame->info;
397  /* Third argument is unused */
398  regs->r12 = 0;
399 
400  /* Actually move the usp to reflect the stacked frame */
401  wrusp((unsigned long)frame);
402 
403  return 0;
404 
405 give_sigsegv:
406  force_sigsegv(sig, current);
407  return -EFAULT;
408 }
409 
410 /*
411  * OK, we're invoking a handler
412  */
413 
414 static inline void handle_signal(int canrestart, unsigned long sig,
415  siginfo_t *info, struct k_sigaction *ka,
416  struct pt_regs *regs)
417 {
418  sigset_t *oldset = sigmask_to_save();
419  int ret;
420 
421  /* Are we from a system call? */
422  if (canrestart) {
423  /* If so, check system call restarting.. */
424  switch (regs->r10) {
425  case -ERESTART_RESTARTBLOCK:
426  case -ERESTARTNOHAND:
427  /* ERESTARTNOHAND means that the syscall should
428  * only be restarted if there was no handler for
429  * the signal, and since we only get here if there
430  * is a handler, we don't restart */
431  regs->r10 = -EINTR;
432  break;
433  case -ERESTARTSYS:
434  /* ERESTARTSYS means to restart the syscall if
435  * there is no handler or the handler was
436  * registered with SA_RESTART */
437  if (!(ka->sa.sa_flags & SA_RESTART)) {
438  regs->r10 = -EINTR;
439  break;
440  }
441  /* fallthrough */
442  case -ERESTARTNOINTR:
443  /* ERESTARTNOINTR means that the syscall should
444  * be called again after the signal handler returns. */
445  RESTART_CRIS_SYS(regs);
446  }
447  }
448 
449  /* Set up the stack frame */
450  if (ka->sa.sa_flags & SA_SIGINFO)
451  ret = setup_rt_frame(sig, ka, info, oldset, regs);
452  else
453  ret = setup_frame(sig, ka, oldset, regs);
454 
455  if (ret == 0)
456  signal_delivered(sig, info, ka, regs, 0);
457 }
458 
459 /*
460  * Note that 'init' is a special process: it doesn't get signals it doesn't
461  * want to handle. Thus you cannot kill init even with a SIGKILL even by
462  * mistake.
463  *
464  * Also note that the regs structure given here as an argument, is the latest
465  * pushed pt_regs. It may or may not be the same as the first pushed registers
466  * when the initial usermode->kernelmode transition took place. Therefore
467  * we can use user_mode(regs) to see if we came directly from kernel or user
468  * mode below.
469  */
470 
471 void do_signal(int canrestart, struct pt_regs *regs)
472 {
473  siginfo_t info;
474  int signr;
475  struct k_sigaction ka;
476 
477  /*
478  * We want the common case to go fast, which
479  * is why we may in certain cases get here from
480  * kernel mode. Just return without doing anything
481  * if so.
482  */
483  if (!user_mode(regs))
484  return;
485 
486  signr = get_signal_to_deliver(&info, &ka, regs, NULL);
487  if (signr > 0) {
488  /* Whee! Actually deliver the signal. */
489  handle_signal(canrestart, signr, &info, &ka, regs);
490  return;
491  }
492 
493  /* Did we come from a system call? */
494  if (canrestart) {
495  /* Restart the system call - no handlers present */
496  if (regs->r10 == -ERESTARTNOHAND ||
497  regs->r10 == -ERESTARTSYS ||
498  regs->r10 == -ERESTARTNOINTR) {
499  RESTART_CRIS_SYS(regs);
500  }
501  if (regs->r10 == -ERESTART_RESTARTBLOCK) {
502  regs->r9 = __NR_restart_syscall;
503  regs->irp -= 2;
504  }
505  }
506 
507  /* if there's no signal to deliver, we just put the saved sigmask
508  * back */
509  restore_saved_sigmask();
510 }