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  * Copyright 2004-2010 Analog Devices Inc.
3  *
4  * Licensed under the GPL-2 or later
5  */
6 
7 #include <linux/signal.h>
8 #include <linux/syscalls.h>
9 #include <linux/ptrace.h>
10 #include <linux/tty.h>
11 #include <linux/personality.h>
12 #include <linux/binfmts.h>
13 #include <linux/uaccess.h>
14 #include <linux/tracehook.h>
15 
16 #include <asm/cacheflush.h>
17 #include <asm/ucontext.h>
18 #include <asm/fixed_code.h>
19 #include <asm/syscall.h>
20 
21 /* Location of the trace bit in SYSCFG. */
22 #define TRACE_BITS 0x0001
23 
25  unsigned long text;
26  unsigned long GOT;
27 };
28 
29 struct rt_sigframe {
30  int sig;
31  struct siginfo *pinfo;
32  void *puc;
33  /* This is no longer needed by the kernel, but unfortunately userspace
34  * code expects it to be there. */
35  char retcode[8];
36  struct siginfo info;
37  struct ucontext uc;
38 };
39 
40 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
41 {
42  return do_sigaltstack(uss, uoss, rdusp());
43 }
44 
45 static inline int
46 rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
47 {
48  unsigned long usp = 0;
49  int err = 0;
50 
51  /* Always make any pending restarted system calls return -EINTR */
52  current_thread_info()->restart_block.fn = do_no_restart_syscall;
53 
54 #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
55 
56  /* restore passed registers */
59  RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
60  RESTORE(p4); RESTORE(p5);
61  err |= __get_user(usp, &sc->sc_usp);
62  wrusp(usp);
63  RESTORE(a0w); RESTORE(a1w);
64  RESTORE(a0x); RESTORE(a1x);
65  RESTORE(astat);
66  RESTORE(rets);
67  RESTORE(pc);
68  RESTORE(retx);
69  RESTORE(fp);
70  RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
71  RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
72  RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
73  RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
74  RESTORE(lc0); RESTORE(lc1);
75  RESTORE(lt0); RESTORE(lt1);
76  RESTORE(lb0); RESTORE(lb1);
77  RESTORE(seqstat);
78 
79  regs->orig_p0 = -1; /* disable syscall checks */
80 
81  *pr0 = regs->r0;
82  return err;
83 }
84 
86 {
87  struct pt_regs *regs = (struct pt_regs *)__unused;
88  unsigned long usp = rdusp();
89  struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
90  sigset_t set;
91  int r0;
92 
93  if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
94  goto badframe;
95  if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
96  goto badframe;
97 
98  set_current_blocked(&set);
99 
100  if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
101  goto badframe;
102 
103  if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
104  goto badframe;
105 
106  return r0;
107 
108  badframe:
110  return 0;
111 }
112 
113 static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
114 {
115  int err = 0;
116 
117 #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
118 
119  SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
120  SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
121  SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
122  SETUP(p4); SETUP(p5);
123  err |= __put_user(rdusp(), &sc->sc_usp);
124  SETUP(a0w); SETUP(a1w);
125  SETUP(a0x); SETUP(a1x);
126  SETUP(astat);
127  SETUP(rets);
128  SETUP(pc);
129  SETUP(retx);
130  SETUP(fp);
131  SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
132  SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
133  SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
134  SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
135  SETUP(lc0); SETUP(lc1);
136  SETUP(lt0); SETUP(lt1);
137  SETUP(lb0); SETUP(lb1);
138  SETUP(seqstat);
139 
140  return err;
141 }
142 
143 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
144  size_t frame_size)
145 {
146  unsigned long usp;
147 
148  /* Default to using normal stack. */
149  usp = rdusp();
150 
151  /* This is the X/Open sanctioned signal stack switching. */
152  if (ka->sa.sa_flags & SA_ONSTACK) {
153  if (!on_sig_stack(usp))
154  usp = current->sas_ss_sp + current->sas_ss_size;
155  }
156  return (void *)((usp - frame_size) & -8UL);
157 }
158 
159 static int
160 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
161  sigset_t * set, struct pt_regs *regs)
162 {
163  struct rt_sigframe *frame;
164  int err = 0;
165 
166  frame = get_sigframe(ka, regs, sizeof(*frame));
167 
170  && sig < 32
172  signal_invmap[sig] : sig), &frame->sig);
173 
174  err |= __put_user(&frame->info, &frame->pinfo);
175  err |= __put_user(&frame->uc, &frame->puc);
176  err |= copy_siginfo_to_user(&frame->info, info);
177 
178  /* Create the ucontext. */
179  err |= __put_user(0, &frame->uc.uc_flags);
180  err |= __put_user(0, &frame->uc.uc_link);
181  err |=
182  __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
183  err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
184  err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
185  err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
186  err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
187 
188  if (err)
189  return -EFAULT;
190 
191  /* Set up registers for signal handler */
192  if (current->personality & FDPIC_FUNCPTRS) {
193  struct fdpic_func_descriptor __user *funcptr =
194  (struct fdpic_func_descriptor *) ka->sa.sa_handler;
195  u32 pc, p3;
196  err |= __get_user(pc, &funcptr->text);
197  err |= __get_user(p3, &funcptr->GOT);
198  if (err)
199  return -EFAULT;
200  regs->pc = pc;
201  regs->p3 = p3;
202  } else
203  regs->pc = (unsigned long)ka->sa.sa_handler;
204  wrusp((unsigned long)frame);
205  regs->rets = SIGRETURN_STUB;
206 
207  regs->r0 = frame->sig;
208  regs->r1 = (unsigned long)(&frame->info);
209  regs->r2 = (unsigned long)(&frame->uc);
210 
211  return 0;
212 }
213 
214 static inline void
215 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
216 {
217  switch (regs->r0) {
218  case -ERESTARTNOHAND:
219  if (!has_handler)
220  goto do_restart;
221  regs->r0 = -EINTR;
222  break;
223 
224  case -ERESTARTSYS:
225  if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
226  regs->r0 = -EINTR;
227  break;
228  }
229  /* fallthrough */
230  case -ERESTARTNOINTR:
231  do_restart:
232  regs->p0 = regs->orig_p0;
233  regs->r0 = regs->orig_r0;
234  regs->pc -= 2;
235  break;
236 
237  case -ERESTART_RESTARTBLOCK:
238  regs->p0 = __NR_restart_syscall;
239  regs->pc -= 2;
240  break;
241  }
242 }
243 
244 /*
245  * OK, we're invoking a handler
246  */
247 static void
248 handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
249  struct pt_regs *regs)
250 {
251  /* are we from a system call? to see pt_regs->orig_p0 */
252  if (regs->orig_p0 >= 0)
253  /* If so, check system call restarting.. */
254  handle_restart(regs, ka, 1);
255 
256  /* set up the stack frame */
257  if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
258  force_sigsegv(sig, current);
259  else
260  signal_delivered(sig, info, ka, regs,
261  test_thread_flag(TIF_SINGLESTEP));
262 }
263 
264 /*
265  * Note that 'init' is a special process: it doesn't get signals it doesn't
266  * want to handle. Thus you cannot kill init even with a SIGKILL even by
267  * mistake.
268  *
269  * Note that we go through the signals twice: once to check the signals
270  * that the kernel can handle, and then we build all the user-level signal
271  * handling stack-frames in one go after that.
272  */
273 asmlinkage void do_signal(struct pt_regs *regs)
274 {
275  siginfo_t info;
276  int signr;
277  struct k_sigaction ka;
278 
279  current->thread.esp0 = (unsigned long)regs;
280 
281  signr = get_signal_to_deliver(&info, &ka, regs, NULL);
282  if (signr > 0) {
283  /* Whee! Actually deliver the signal. */
284  handle_signal(signr, &info, &ka, regs);
285  return;
286  }
287 
288  /* Did we come from a system call? */
289  if (regs->orig_p0 >= 0)
290  /* Restart the system call - no handlers present */
291  handle_restart(regs, NULL, 0);
292 
293  /* if there's no signal to deliver, we just put the saved sigmask
294  * back */
295  restore_saved_sigmask();
296 }
297 
298 /*
299  * notification of userspace execution resumption
300  */
302 {
303  if (test_thread_flag(TIF_SIGPENDING))
304  do_signal(regs);
305 
306  if (test_thread_flag(TIF_NOTIFY_RESUME)) {
307  clear_thread_flag(TIF_NOTIFY_RESUME);
308  tracehook_notify_resume(regs);
309  }
310 }
311