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) 1991, 1992 Linus Torvalds
3  * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
4  *
5  * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6  * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
7  * 2000-2002 x86-64 support by Andi Kleen
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/tracehook.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/personality.h>
22 #include <linux/uaccess.h>
24 #include <linux/uprobes.h>
25 
26 #include <asm/processor.h>
27 #include <asm/ucontext.h>
28 #include <asm/i387.h>
29 #include <asm/fpu-internal.h>
30 #include <asm/vdso.h>
31 #include <asm/mce.h>
32 #include <asm/sighandling.h>
33 
34 #ifdef CONFIG_X86_64
35 #include <asm/proto.h>
36 #include <asm/ia32_unistd.h>
37 #include <asm/sys_ia32.h>
38 #endif /* CONFIG_X86_64 */
39 
40 #include <asm/syscall.h>
41 #include <asm/syscalls.h>
42 
43 #include <asm/sigframe.h>
44 
45 #ifdef CONFIG_X86_32
46 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
47 #else
48 # define FIX_EFLAGS __FIX_EFLAGS
49 #endif
50 
51 #define COPY(x) do { \
52  get_user_ex(regs->x, &sc->x); \
53 } while (0)
54 
55 #define GET_SEG(seg) ({ \
56  unsigned short tmp; \
57  get_user_ex(tmp, &sc->seg); \
58  tmp; \
59 })
60 
61 #define COPY_SEG(seg) do { \
62  regs->seg = GET_SEG(seg); \
63 } while (0)
64 
65 #define COPY_SEG_CPL3(seg) do { \
66  regs->seg = GET_SEG(seg) | 3; \
67 } while (0)
68 
69 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
70  unsigned long *pax)
71 {
72  void __user *buf;
73  unsigned int tmpflags;
74  unsigned int err = 0;
75 
76  /* Always make any pending restarted system calls return -EINTR */
77  current_thread_info()->restart_block.fn = do_no_restart_syscall;
78 
79  get_user_try {
80 
81 #ifdef CONFIG_X86_32
82  set_user_gs(regs, GET_SEG(gs));
83  COPY_SEG(fs);
84  COPY_SEG(es);
85  COPY_SEG(ds);
86 #endif /* CONFIG_X86_32 */
87 
88  COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
89  COPY(dx); COPY(cx); COPY(ip);
90 
91 #ifdef CONFIG_X86_64
92  COPY(r8);
93  COPY(r9);
94  COPY(r10);
95  COPY(r11);
96  COPY(r12);
97  COPY(r13);
98  COPY(r14);
99  COPY(r15);
100 #endif /* CONFIG_X86_64 */
101 
102 #ifdef CONFIG_X86_32
103  COPY_SEG_CPL3(cs);
104  COPY_SEG_CPL3(ss);
105 #else /* !CONFIG_X86_32 */
106  /* Kernel saves and restores only the CS segment register on signals,
107  * which is the bare minimum needed to allow mixed 32/64-bit code.
108  * App's signal handler can save/restore other segments if needed. */
109  COPY_SEG_CPL3(cs);
110 #endif /* CONFIG_X86_32 */
111 
112  get_user_ex(tmpflags, &sc->flags);
113  regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
114  regs->orig_ax = -1; /* disable syscall checks */
115 
116  get_user_ex(buf, &sc->fpstate);
117 
118  get_user_ex(*pax, &sc->ax);
119  } get_user_catch(err);
120 
121  err |= restore_xstate_sig(buf, config_enabled(CONFIG_X86_32));
122 
123  return err;
124 }
125 
126 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
127  struct pt_regs *regs, unsigned long mask)
128 {
129  int err = 0;
130 
131  put_user_try {
132 
133 #ifdef CONFIG_X86_32
134  put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
135  put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
136  put_user_ex(regs->es, (unsigned int __user *)&sc->es);
137  put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
138 #endif /* CONFIG_X86_32 */
139 
140  put_user_ex(regs->di, &sc->di);
141  put_user_ex(regs->si, &sc->si);
142  put_user_ex(regs->bp, &sc->bp);
143  put_user_ex(regs->sp, &sc->sp);
144  put_user_ex(regs->bx, &sc->bx);
145  put_user_ex(regs->dx, &sc->dx);
146  put_user_ex(regs->cx, &sc->cx);
147  put_user_ex(regs->ax, &sc->ax);
148 #ifdef CONFIG_X86_64
149  put_user_ex(regs->r8, &sc->r8);
150  put_user_ex(regs->r9, &sc->r9);
151  put_user_ex(regs->r10, &sc->r10);
152  put_user_ex(regs->r11, &sc->r11);
153  put_user_ex(regs->r12, &sc->r12);
154  put_user_ex(regs->r13, &sc->r13);
155  put_user_ex(regs->r14, &sc->r14);
156  put_user_ex(regs->r15, &sc->r15);
157 #endif /* CONFIG_X86_64 */
158 
159  put_user_ex(current->thread.trap_nr, &sc->trapno);
160  put_user_ex(current->thread.error_code, &sc->err);
161  put_user_ex(regs->ip, &sc->ip);
162 #ifdef CONFIG_X86_32
163  put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
164  put_user_ex(regs->flags, &sc->flags);
165  put_user_ex(regs->sp, &sc->sp_at_signal);
166  put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
167 #else /* !CONFIG_X86_32 */
168  put_user_ex(regs->flags, &sc->flags);
169  put_user_ex(regs->cs, &sc->cs);
170  put_user_ex(0, &sc->gs);
171  put_user_ex(0, &sc->fs);
172 #endif /* CONFIG_X86_32 */
173 
174  put_user_ex(fpstate, &sc->fpstate);
175 
176  /* non-iBCS2 extensions.. */
177  put_user_ex(mask, &sc->oldmask);
178  put_user_ex(current->thread.cr2, &sc->cr2);
179  } put_user_catch(err);
180 
181  return err;
182 }
183 
184 /*
185  * Set up a signal frame.
186  */
187 
188 /*
189  * Determine which stack to use..
190  */
191 static unsigned long align_sigframe(unsigned long sp)
192 {
193 #ifdef CONFIG_X86_32
194  /*
195  * Align the stack pointer according to the i386 ABI,
196  * i.e. so that on function entry ((sp + 4) & 15) == 0.
197  */
198  sp = ((sp + 4) & -16ul) - 4;
199 #else /* !CONFIG_X86_32 */
200  sp = round_down(sp, 16) - 8;
201 #endif
202  return sp;
203 }
204 
205 static inline void __user *
206 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
207  void __user **fpstate)
208 {
209  /* Default to using normal stack */
210  unsigned long math_size = 0;
211  unsigned long sp = regs->sp;
212  unsigned long buf_fx = 0;
213  int onsigstack = on_sig_stack(sp);
214 
215  /* redzone */
216  if (config_enabled(CONFIG_X86_64))
217  sp -= 128;
218 
219  if (!onsigstack) {
220  /* This is the X/Open sanctioned signal stack switching. */
221  if (ka->sa.sa_flags & SA_ONSTACK) {
222  if (current->sas_ss_size)
223  sp = current->sas_ss_sp + current->sas_ss_size;
224  } else if (config_enabled(CONFIG_X86_32) &&
225  (regs->ss & 0xffff) != __USER_DS &&
226  !(ka->sa.sa_flags & SA_RESTORER) &&
227  ka->sa.sa_restorer) {
228  /* This is the legacy signal stack switching. */
229  sp = (unsigned long) ka->sa.sa_restorer;
230  }
231  }
232 
233  if (used_math()) {
234  sp = alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
235  &buf_fx, &math_size);
236  *fpstate = (void __user *)sp;
237  }
238 
239  sp = align_sigframe(sp - frame_size);
240 
241  /*
242  * If we are on the alternate signal stack and would overflow it, don't.
243  * Return an always-bogus address instead so we will die with SIGSEGV.
244  */
245  if (onsigstack && !likely(on_sig_stack(sp)))
246  return (void __user *)-1L;
247 
248  /* save i387 and extended state */
249  if (used_math() &&
250  save_xstate_sig(*fpstate, (void __user *)buf_fx, math_size) < 0)
251  return (void __user *)-1L;
252 
253  return (void __user *)sp;
254 }
255 
256 #ifdef CONFIG_X86_32
257 static const struct {
258  u16 poplmovl;
259  u32 val;
260  u16 int80;
261 } __attribute__((packed)) retcode = {
262  0xb858, /* popl %eax; movl $..., %eax */
264  0x80cd, /* int $0x80 */
265 };
266 
267 static const struct {
268  u8 movl;
269  u32 val;
270  u16 int80;
271  u8 pad;
272 } __attribute__((packed)) rt_retcode = {
273  0xb8, /* movl $..., %eax */
275  0x80cd, /* int $0x80 */
276  0
277 };
278 
279 static int
280 __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
281  struct pt_regs *regs)
282 {
283  struct sigframe __user *frame;
284  void __user *restorer;
285  int err = 0;
286  void __user *fpstate = NULL;
287 
288  frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
289 
290  if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
291  return -EFAULT;
292 
293  if (__put_user(sig, &frame->sig))
294  return -EFAULT;
295 
296  if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
297  return -EFAULT;
298 
299  if (_NSIG_WORDS > 1) {
300  if (__copy_to_user(&frame->extramask, &set->sig[1],
301  sizeof(frame->extramask)))
302  return -EFAULT;
303  }
304 
305  if (current->mm->context.vdso)
306  restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
307  else
308  restorer = &frame->retcode;
309  if (ka->sa.sa_flags & SA_RESTORER)
310  restorer = ka->sa.sa_restorer;
311 
312  /* Set up to return from userspace. */
313  err |= __put_user(restorer, &frame->pretcode);
314 
315  /*
316  * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
317  *
318  * WE DO NOT USE IT ANY MORE! It's only left here for historical
319  * reasons and because gdb uses it as a signature to notice
320  * signal handler stack frames.
321  */
322  err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
323 
324  if (err)
325  return -EFAULT;
326 
327  /* Set up registers for signal handler */
328  regs->sp = (unsigned long)frame;
329  regs->ip = (unsigned long)ka->sa.sa_handler;
330  regs->ax = (unsigned long)sig;
331  regs->dx = 0;
332  regs->cx = 0;
333 
334  regs->ds = __USER_DS;
335  regs->es = __USER_DS;
336  regs->ss = __USER_DS;
337  regs->cs = __USER_CS;
338 
339  return 0;
340 }
341 
342 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
343  sigset_t *set, struct pt_regs *regs)
344 {
345  struct rt_sigframe __user *frame;
346  void __user *restorer;
347  int err = 0;
348  void __user *fpstate = NULL;
349 
350  frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
351 
352  if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
353  return -EFAULT;
354 
355  put_user_try {
356  put_user_ex(sig, &frame->sig);
357  put_user_ex(&frame->info, &frame->pinfo);
358  put_user_ex(&frame->uc, &frame->puc);
359 
360  /* Create the ucontext. */
361  if (cpu_has_xsave)
362  put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
363  else
364  put_user_ex(0, &frame->uc.uc_flags);
365  put_user_ex(0, &frame->uc.uc_link);
366  put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
367  put_user_ex(sas_ss_flags(regs->sp),
368  &frame->uc.uc_stack.ss_flags);
369  put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
370 
371  /* Set up to return from userspace. */
372  restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
373  if (ka->sa.sa_flags & SA_RESTORER)
374  restorer = ka->sa.sa_restorer;
375  put_user_ex(restorer, &frame->pretcode);
376 
377  /*
378  * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
379  *
380  * WE DO NOT USE IT ANY MORE! It's only left here for historical
381  * reasons and because gdb uses it as a signature to notice
382  * signal handler stack frames.
383  */
384  put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
385  } put_user_catch(err);
386 
387  err |= copy_siginfo_to_user(&frame->info, info);
388  err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
389  regs, set->sig[0]);
390  err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
391 
392  if (err)
393  return -EFAULT;
394 
395  /* Set up registers for signal handler */
396  regs->sp = (unsigned long)frame;
397  regs->ip = (unsigned long)ka->sa.sa_handler;
398  regs->ax = (unsigned long)sig;
399  regs->dx = (unsigned long)&frame->info;
400  regs->cx = (unsigned long)&frame->uc;
401 
402  regs->ds = __USER_DS;
403  regs->es = __USER_DS;
404  regs->ss = __USER_DS;
405  regs->cs = __USER_CS;
406 
407  return 0;
408 }
409 #else /* !CONFIG_X86_32 */
410 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
411  sigset_t *set, struct pt_regs *regs)
412 {
413  struct rt_sigframe __user *frame;
414  void __user *fp = NULL;
415  int err = 0;
416  struct task_struct *me = current;
417 
418  frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
419 
420  if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
421  return -EFAULT;
422 
423  if (ka->sa.sa_flags & SA_SIGINFO) {
424  if (copy_siginfo_to_user(&frame->info, info))
425  return -EFAULT;
426  }
427 
428  put_user_try {
429  /* Create the ucontext. */
430  if (cpu_has_xsave)
431  put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
432  else
433  put_user_ex(0, &frame->uc.uc_flags);
434  put_user_ex(0, &frame->uc.uc_link);
435  put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
436  put_user_ex(sas_ss_flags(regs->sp),
437  &frame->uc.uc_stack.ss_flags);
438  put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
439 
440  /* Set up to return from userspace. If provided, use a stub
441  already in userspace. */
442  /* x86-64 should always use SA_RESTORER. */
443  if (ka->sa.sa_flags & SA_RESTORER) {
444  put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
445  } else {
446  /* could use a vstub here */
447  err |= -EFAULT;
448  }
449  } put_user_catch(err);
450 
451  err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
452  err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
453 
454  if (err)
455  return -EFAULT;
456 
457  /* Set up registers for signal handler */
458  regs->di = sig;
459  /* In case the signal handler was declared without prototypes */
460  regs->ax = 0;
461 
462  /* This also works for non SA_SIGINFO handlers because they expect the
463  next argument after the signal number on the stack. */
464  regs->si = (unsigned long)&frame->info;
465  regs->dx = (unsigned long)&frame->uc;
466  regs->ip = (unsigned long) ka->sa.sa_handler;
467 
468  regs->sp = (unsigned long)frame;
469 
470  /* Set up the CS register to run signal handlers in 64-bit mode,
471  even if the handler happens to be interrupting 32-bit code. */
472  regs->cs = __USER_CS;
473 
474  return 0;
475 }
476 #endif /* CONFIG_X86_32 */
477 
478 static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
479  siginfo_t *info, compat_sigset_t *set,
480  struct pt_regs *regs)
481 {
482 #ifdef CONFIG_X86_X32_ABI
483  struct rt_sigframe_x32 __user *frame;
484  void __user *restorer;
485  int err = 0;
486  void __user *fpstate = NULL;
487 
488  frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
489 
490  if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
491  return -EFAULT;
492 
493  if (ka->sa.sa_flags & SA_SIGINFO) {
494  if (copy_siginfo_to_user32(&frame->info, info))
495  return -EFAULT;
496  }
497 
498  put_user_try {
499  /* Create the ucontext. */
500  if (cpu_has_xsave)
501  put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
502  else
503  put_user_ex(0, &frame->uc.uc_flags);
504  put_user_ex(0, &frame->uc.uc_link);
505  put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
506  put_user_ex(sas_ss_flags(regs->sp),
507  &frame->uc.uc_stack.ss_flags);
508  put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
509  put_user_ex(0, &frame->uc.uc__pad0);
510 
511  if (ka->sa.sa_flags & SA_RESTORER) {
512  restorer = ka->sa.sa_restorer;
513  } else {
514  /* could use a vstub here */
515  restorer = NULL;
516  err |= -EFAULT;
517  }
518  put_user_ex(restorer, &frame->pretcode);
519  } put_user_catch(err);
520 
521  err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
522  regs, set->sig[0]);
523  err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
524 
525  if (err)
526  return -EFAULT;
527 
528  /* Set up registers for signal handler */
529  regs->sp = (unsigned long) frame;
530  regs->ip = (unsigned long) ka->sa.sa_handler;
531 
532  /* We use the x32 calling convention here... */
533  regs->di = sig;
534  regs->si = (unsigned long) &frame->info;
535  regs->dx = (unsigned long) &frame->uc;
536 
537  loadsegment(ds, __USER_DS);
538  loadsegment(es, __USER_DS);
539 
540  regs->cs = __USER_CS;
541  regs->ss = __USER_DS;
542 #endif /* CONFIG_X86_X32_ABI */
543 
544  return 0;
545 }
546 
547 #ifdef CONFIG_X86_32
548 /*
549  * Atomically swap in the new signal mask, and wait for a signal.
550  */
551 asmlinkage int
552 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
553 {
554  sigset_t blocked;
555  siginitset(&blocked, mask);
556  return sigsuspend(&blocked);
557 }
558 
559 asmlinkage int
560 sys_sigaction(int sig, const struct old_sigaction __user *act,
561  struct old_sigaction __user *oact)
562 {
563  struct k_sigaction new_ka, old_ka;
564  int ret = 0;
565 
566  if (act) {
568 
569  if (!access_ok(VERIFY_READ, act, sizeof(*act)))
570  return -EFAULT;
571 
572  get_user_try {
573  get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
574  get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
575  get_user_ex(mask, &act->sa_mask);
576  get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
577  } get_user_catch(ret);
578 
579  if (ret)
580  return -EFAULT;
581  siginitset(&new_ka.sa.sa_mask, mask);
582  }
583 
584  ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
585 
586  if (!ret && oact) {
587  if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
588  return -EFAULT;
589 
590  put_user_try {
591  put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
592  put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
593  put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
594  put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
595  } put_user_catch(ret);
596 
597  if (ret)
598  return -EFAULT;
599  }
600 
601  return ret;
602 }
603 #endif /* CONFIG_X86_32 */
604 
605 long
606 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
607  struct pt_regs *regs)
608 {
609  return do_sigaltstack(uss, uoss, regs->sp);
610 }
611 
612 /*
613  * Do a signal return; undo the signal stack.
614  */
615 #ifdef CONFIG_X86_32
616 unsigned long sys_sigreturn(struct pt_regs *regs)
617 {
618  struct sigframe __user *frame;
619  unsigned long ax;
620  sigset_t set;
621 
622  frame = (struct sigframe __user *)(regs->sp - 8);
623 
624  if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
625  goto badframe;
626  if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
627  && __copy_from_user(&set.sig[1], &frame->extramask,
628  sizeof(frame->extramask))))
629  goto badframe;
630 
631  set_current_blocked(&set);
632 
633  if (restore_sigcontext(regs, &frame->sc, &ax))
634  goto badframe;
635  return ax;
636 
637 badframe:
638  signal_fault(regs, frame, "sigreturn");
639 
640  return 0;
641 }
642 #endif /* CONFIG_X86_32 */
643 
644 long sys_rt_sigreturn(struct pt_regs *regs)
645 {
646  struct rt_sigframe __user *frame;
647  unsigned long ax;
648  sigset_t set;
649 
650  frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
651  if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
652  goto badframe;
653  if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
654  goto badframe;
655 
656  set_current_blocked(&set);
657 
658  if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
659  goto badframe;
660 
661  if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
662  goto badframe;
663 
664  return ax;
665 
666 badframe:
667  signal_fault(regs, frame, "rt_sigreturn");
668  return 0;
669 }
670 
671 /*
672  * OK, we're invoking a handler:
673  */
674 static int signr_convert(int sig)
675 {
676 #ifdef CONFIG_X86_32
677  struct thread_info *info = current_thread_info();
678 
679  if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
680  return info->exec_domain->signal_invmap[sig];
681 #endif /* CONFIG_X86_32 */
682  return sig;
683 }
684 
685 static int
686 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
687  struct pt_regs *regs)
688 {
689  int usig = signr_convert(sig);
690  sigset_t *set = sigmask_to_save();
691  compat_sigset_t *cset = (compat_sigset_t *) set;
692 
693  /* Set up the stack frame */
694  if (is_ia32_frame()) {
695  if (ka->sa.sa_flags & SA_SIGINFO)
696  return ia32_setup_rt_frame(usig, ka, info, cset, regs);
697  else
698  return ia32_setup_frame(usig, ka, cset, regs);
699  } else if (is_x32_frame()) {
700  return x32_setup_rt_frame(usig, ka, info, cset, regs);
701  } else {
702  return __setup_rt_frame(sig, ka, info, set, regs);
703  }
704 }
705 
706 static void
707 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
708  struct pt_regs *regs)
709 {
710  /* Are we from a system call? */
711  if (syscall_get_nr(current, regs) >= 0) {
712  /* If so, check system call restarting.. */
713  switch (syscall_get_error(current, regs)) {
714  case -ERESTART_RESTARTBLOCK:
715  case -ERESTARTNOHAND:
716  regs->ax = -EINTR;
717  break;
718 
719  case -ERESTARTSYS:
720  if (!(ka->sa.sa_flags & SA_RESTART)) {
721  regs->ax = -EINTR;
722  break;
723  }
724  /* fallthrough */
725  case -ERESTARTNOINTR:
726  regs->ax = regs->orig_ax;
727  regs->ip -= 2;
728  break;
729  }
730  }
731 
732  /*
733  * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
734  * flag so that register information in the sigcontext is correct.
735  */
736  if (unlikely(regs->flags & X86_EFLAGS_TF) &&
737  likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
738  regs->flags &= ~X86_EFLAGS_TF;
739 
740  if (setup_rt_frame(sig, ka, info, regs) < 0) {
741  force_sigsegv(sig, current);
742  return;
743  }
744 
745  /*
746  * Clear the direction flag as per the ABI for function entry.
747  */
748  regs->flags &= ~X86_EFLAGS_DF;
749 
750  /*
751  * Clear TF when entering the signal handler, but
752  * notify any tracer that was single-stepping it.
753  * The tracer may want to single-step inside the
754  * handler too.
755  */
756  regs->flags &= ~X86_EFLAGS_TF;
757 
758  signal_delivered(sig, info, ka, regs,
759  test_thread_flag(TIF_SINGLESTEP));
760 }
761 
762 #ifdef CONFIG_X86_32
763 #define NR_restart_syscall __NR_restart_syscall
764 #else /* !CONFIG_X86_32 */
765 #define NR_restart_syscall \
766  test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
767 #endif /* CONFIG_X86_32 */
768 
769 /*
770  * Note that 'init' is a special process: it doesn't get signals it doesn't
771  * want to handle. Thus you cannot kill init even with a SIGKILL even by
772  * mistake.
773  */
774 static void do_signal(struct pt_regs *regs)
775 {
776  struct k_sigaction ka;
777  siginfo_t info;
778  int signr;
779 
780  signr = get_signal_to_deliver(&info, &ka, regs, NULL);
781  if (signr > 0) {
782  /* Whee! Actually deliver the signal. */
783  handle_signal(signr, &info, &ka, regs);
784  return;
785  }
786 
787  /* Did we come from a system call? */
788  if (syscall_get_nr(current, regs) >= 0) {
789  /* Restart the system call - no handlers present */
790  switch (syscall_get_error(current, regs)) {
791  case -ERESTARTNOHAND:
792  case -ERESTARTSYS:
793  case -ERESTARTNOINTR:
794  regs->ax = regs->orig_ax;
795  regs->ip -= 2;
796  break;
797 
798  case -ERESTART_RESTARTBLOCK:
799  regs->ax = NR_restart_syscall;
800  regs->ip -= 2;
801  break;
802  }
803  }
804 
805  /*
806  * If there's no signal to deliver, we just put the saved sigmask
807  * back.
808  */
809  restore_saved_sigmask();
810 }
811 
812 /*
813  * notification of userspace execution resumption
814  * - triggered by the TIF_WORK_MASK flags
815  */
816 void
817 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
818 {
819  rcu_user_exit();
820 
821 #ifdef CONFIG_X86_MCE
822  /* notify userspace of pending MCEs */
823  if (thread_info_flags & _TIF_MCE_NOTIFY)
825 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
826 
827  if (thread_info_flags & _TIF_UPROBE)
828  uprobe_notify_resume(regs);
829 
830  /* deal with pending signal delivery */
831  if (thread_info_flags & _TIF_SIGPENDING)
832  do_signal(regs);
833 
834  if (thread_info_flags & _TIF_NOTIFY_RESUME) {
835  clear_thread_flag(TIF_NOTIFY_RESUME);
836  tracehook_notify_resume(regs);
837  }
838  if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
840 
841  rcu_user_enter();
842 }
843 
844 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
845 {
846  struct task_struct *me = current;
847 
848  if (show_unhandled_signals && printk_ratelimit()) {
849  printk("%s"
850  "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
851  task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
852  me->comm, me->pid, where, frame,
853  regs->ip, regs->sp, regs->orig_ax);
854  print_vma_addr(" in ", regs->ip);
855  pr_cont("\n");
856  }
857 
858  force_sig(SIGSEGV, me);
859 }
860 
861 #ifdef CONFIG_X86_X32_ABI
862 asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
863 {
864  struct rt_sigframe_x32 __user *frame;
865  sigset_t set;
866  unsigned long ax;
867  struct pt_regs tregs;
868 
869  frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
870 
871  if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
872  goto badframe;
873  if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
874  goto badframe;
875 
876  set_current_blocked(&set);
877 
878  if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
879  goto badframe;
880 
881  tregs = *regs;
882  if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
883  goto badframe;
884 
885  return ax;
886 
887 badframe:
888  signal_fault(regs, frame, "x32 rt_sigreturn");
889  return 0;
890 }
891 #endif