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 (C) 2004-2006 Atmel Corporation
3  *
4  * Based on linux/arch/sh/kernel/signal.c
5  * Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
6  * Copyright (C) 1991, 1992 Linus Torvalds
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/unistd.h>
18 #include <linux/tracehook.h>
19 
20 #include <asm/uaccess.h>
21 #include <asm/ucontext.h>
22 #include <asm/syscalls.h>
23 
24 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
25  struct pt_regs *regs)
26 {
27  return do_sigaltstack(uss, uoss, regs->sp);
28 }
29 
30 struct rt_sigframe
31 {
32  struct siginfo info;
33  struct ucontext uc;
34  unsigned long retcode;
35 };
36 
37 static int
38 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
39 {
40  int err = 0;
41 
42 #define COPY(x) err |= __get_user(regs->x, &sc->x)
43  COPY(sr);
44  COPY(pc);
45  COPY(lr);
46  COPY(sp);
47  COPY(r12);
48  COPY(r11);
49  COPY(r10);
50  COPY(r9);
51  COPY(r8);
52  COPY(r7);
53  COPY(r6);
54  COPY(r5);
55  COPY(r4);
56  COPY(r3);
57  COPY(r2);
58  COPY(r1);
59  COPY(r0);
60 #undef COPY
61 
62  /*
63  * Don't allow anyone to pretend they're running in supervisor
64  * mode or something...
65  */
66  err |= !valid_user_regs(regs);
67 
68  return err;
69 }
70 
71 
73 {
74  struct rt_sigframe __user *frame;
75  sigset_t set;
76 
77  /* Always make any pending restarted system calls return -EINTR */
78  current_thread_info()->restart_block.fn = do_no_restart_syscall;
79 
80  frame = (struct rt_sigframe __user *)regs->sp;
81  pr_debug("SIG return: frame = %p\n", frame);
82 
83  if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
84  goto badframe;
85 
86  if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
87  goto badframe;
88 
89  set_current_blocked(&set);
90 
91  if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
92  goto badframe;
93 
94  if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
95  goto badframe;
96 
97  pr_debug("Context restored: pc = %08lx, lr = %08lx, sp = %08lx\n",
98  regs->pc, regs->lr, regs->sp);
99 
100  return regs->r12;
101 
102 badframe:
104  return 0;
105 }
106 
107 static int
108 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
109 {
110  int err = 0;
111 
112 #define COPY(x) err |= __put_user(regs->x, &sc->x)
113  COPY(sr);
114  COPY(pc);
115  COPY(lr);
116  COPY(sp);
117  COPY(r12);
118  COPY(r11);
119  COPY(r10);
120  COPY(r9);
121  COPY(r8);
122  COPY(r7);
123  COPY(r6);
124  COPY(r5);
125  COPY(r4);
126  COPY(r3);
127  COPY(r2);
128  COPY(r1);
129  COPY(r0);
130 #undef COPY
131 
132  return err;
133 }
134 
135 static inline void __user *
136 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
137 {
138  unsigned long sp = regs->sp;
139 
140  if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
141  sp = current->sas_ss_sp + current->sas_ss_size;
142 
143  return (void __user *)((sp - framesize) & ~3);
144 }
145 
146 static int
147 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
148  sigset_t *set, struct pt_regs *regs)
149 {
150  struct rt_sigframe __user *frame;
151  int err = 0;
152 
153  frame = get_sigframe(ka, regs, sizeof(*frame));
154  err = -EFAULT;
155  if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
156  goto out;
157 
158  /*
159  * Set up the return code:
160  *
161  * mov r8, __NR_rt_sigreturn
162  * scall
163  *
164  * Note: This will blow up since we're using a non-executable
165  * stack. Better use SA_RESTORER.
166  */
167 #if __NR_rt_sigreturn > 127
168 # error __NR_rt_sigreturn must be < 127 to fit in a short mov
169 #endif
170  err = __put_user(0x3008d733 | (__NR_rt_sigreturn << 20),
171  &frame->retcode);
172 
173  err |= copy_siginfo_to_user(&frame->info, info);
174 
175  /* Set up the ucontext */
176  err |= __put_user(0, &frame->uc.uc_flags);
177  err |= __put_user(NULL, &frame->uc.uc_link);
178  err |= __put_user((void __user *)current->sas_ss_sp,
179  &frame->uc.uc_stack.ss_sp);
180  err |= __put_user(sas_ss_flags(regs->sp),
181  &frame->uc.uc_stack.ss_flags);
182  err |= __put_user(current->sas_ss_size,
183  &frame->uc.uc_stack.ss_size);
184  err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
185  err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
186 
187  if (err)
188  goto out;
189 
190  regs->r12 = sig;
191  regs->r11 = (unsigned long) &frame->info;
192  regs->r10 = (unsigned long) &frame->uc;
193  regs->sp = (unsigned long) frame;
194  if (ka->sa.sa_flags & SA_RESTORER)
195  regs->lr = (unsigned long)ka->sa.sa_restorer;
196  else {
197  printk(KERN_NOTICE "[%s:%d] did not set SA_RESTORER\n",
198  current->comm, current->pid);
199  regs->lr = (unsigned long) &frame->retcode;
200  }
201 
202  pr_debug("SIG deliver [%s:%d]: sig=%d sp=0x%lx pc=0x%lx->0x%p lr=0x%lx\n",
203  current->comm, current->pid, sig, regs->sp,
204  regs->pc, ka->sa.sa_handler, regs->lr);
205 
206  regs->pc = (unsigned long) ka->sa.sa_handler;
207 
208 out:
209  return err;
210 }
211 
212 static inline void setup_syscall_restart(struct pt_regs *regs)
213 {
214  if (regs->r12 == -ERESTART_RESTARTBLOCK)
215  regs->r8 = __NR_restart_syscall;
216  else
217  regs->r12 = regs->r12_orig;
218  regs->pc -= 2;
219 }
220 
221 static inline void
222 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
223  struct pt_regs *regs, int syscall)
224 {
225  int ret;
226 
227  /*
228  * Set up the stack frame
229  */
230  ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs);
231 
232  /*
233  * Check that the resulting registers are sane
234  */
235  ret |= !valid_user_regs(regs);
236 
237  /*
238  * Block the signal if we were successful.
239  */
240  if (ret != 0)
241  force_sigsegv(sig, current);
242  else
243  signal_delivered(sig, info, ka, regs, 0);
244 }
245 
246 /*
247  * Note that 'init' is a special process: it doesn't get signals it
248  * doesn't want to handle. Thus you cannot kill init even with a
249  * SIGKILL even by mistake.
250  */
251 static void do_signal(struct pt_regs *regs, int syscall)
252 {
253  siginfo_t info;
254  int signr;
255  struct k_sigaction ka;
256 
257  /*
258  * We want the common case to go fast, which is why we may in
259  * certain cases get here from kernel mode. Just return
260  * without doing anything if so.
261  */
262  if (!user_mode(regs))
263  return;
264 
265  signr = get_signal_to_deliver(&info, &ka, regs, NULL);
266  if (syscall) {
267  switch (regs->r12) {
268  case -ERESTART_RESTARTBLOCK:
269  case -ERESTARTNOHAND:
270  if (signr > 0) {
271  regs->r12 = -EINTR;
272  break;
273  }
274  /* fall through */
275  case -ERESTARTSYS:
276  if (signr > 0 && !(ka.sa.sa_flags & SA_RESTART)) {
277  regs->r12 = -EINTR;
278  break;
279  }
280  /* fall through */
281  case -ERESTARTNOINTR:
282  setup_syscall_restart(regs);
283  }
284  }
285 
286  if (signr == 0) {
287  /* No signal to deliver -- put the saved sigmask back */
288  restore_saved_sigmask();
289  return;
290  }
291 
292  handle_signal(signr, &ka, &info, regs, syscall);
293 }
294 
295 asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
296 {
297  int syscall = 0;
298 
300  syscall = 1;
301 
302  if (ti->flags & _TIF_SIGPENDING)
303  do_signal(regs, syscall);
304 
305  if (ti->flags & _TIF_NOTIFY_RESUME) {
306  clear_thread_flag(TIF_NOTIFY_RESUME);
307  tracehook_notify_resume(regs);
308  }
309 }