compat_signal.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/mm.h>
  17. #include <linux/smp.h>
  18. #include <linux/kernel.h>
  19. #include <linux/signal.h>
  20. #include <linux/errno.h>
  21. #include <linux/wait.h>
  22. #include <linux/unistd.h>
  23. #include <linux/stddef.h>
  24. #include <linux/personality.h>
  25. #include <linux/suspend.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/elf.h>
  28. #include <linux/compat.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/processor.h>
  32. #include <asm/ucontext.h>
  33. #include <asm/sigframe.h>
  34. #include <asm/syscalls.h>
  35. #include <asm/vdso.h>
  36. #include <arch/interrupts.h>
  37. struct compat_ucontext {
  38. compat_ulong_t uc_flags;
  39. compat_uptr_t uc_link;
  40. struct compat_sigaltstack uc_stack;
  41. struct sigcontext uc_mcontext;
  42. sigset_t uc_sigmask; /* mask last for extensibility */
  43. };
  44. struct compat_rt_sigframe {
  45. unsigned char save_area[C_ABI_SAVE_AREA_SIZE]; /* caller save area */
  46. struct compat_siginfo info;
  47. struct compat_ucontext uc;
  48. };
  49. /* The assembly shim for this function arranges to ignore the return value. */
  50. long compat_sys_rt_sigreturn(void)
  51. {
  52. struct pt_regs *regs = current_pt_regs();
  53. struct compat_rt_sigframe __user *frame =
  54. (struct compat_rt_sigframe __user *) compat_ptr(regs->sp);
  55. sigset_t set;
  56. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  57. goto badframe;
  58. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  59. goto badframe;
  60. set_current_blocked(&set);
  61. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  62. goto badframe;
  63. if (compat_restore_altstack(&frame->uc.uc_stack))
  64. goto badframe;
  65. return 0;
  66. badframe:
  67. signal_fault("bad sigreturn frame", regs, frame, 0);
  68. return 0;
  69. }
  70. /*
  71. * Determine which stack to use..
  72. */
  73. static inline void __user *compat_get_sigframe(struct k_sigaction *ka,
  74. struct pt_regs *regs,
  75. size_t frame_size)
  76. {
  77. unsigned long sp;
  78. /* Default to using normal stack */
  79. sp = (unsigned long)compat_ptr(regs->sp);
  80. /*
  81. * If we are on the alternate signal stack and would overflow
  82. * it, don't. Return an always-bogus address instead so we
  83. * will die with SIGSEGV.
  84. */
  85. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
  86. return (void __user __force *)-1UL;
  87. /* This is the X/Open sanctioned signal stack switching. */
  88. if (ka->sa.sa_flags & SA_ONSTACK) {
  89. if (sas_ss_flags(sp) == 0)
  90. sp = current->sas_ss_sp + current->sas_ss_size;
  91. }
  92. sp -= frame_size;
  93. /*
  94. * Align the stack pointer according to the TILE ABI,
  95. * i.e. so that on function entry (sp & 15) == 0.
  96. */
  97. sp &= -16UL;
  98. return (void __user *) sp;
  99. }
  100. int compat_setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  101. struct pt_regs *regs)
  102. {
  103. unsigned long restorer;
  104. struct compat_rt_sigframe __user *frame;
  105. int err = 0, sig = ksig->sig;
  106. frame = compat_get_sigframe(&ksig->ka, regs, sizeof(*frame));
  107. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  108. goto err;
  109. /* Always write at least the signal number for the stack backtracer. */
  110. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  111. /* At sigreturn time, restore the callee-save registers too. */
  112. err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
  113. regs->flags |= PT_FLAGS_RESTORE_REGS;
  114. } else {
  115. err |= __put_user(ksig->info.si_signo, &frame->info.si_signo);
  116. }
  117. /* Create the ucontext. */
  118. err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
  119. err |= __put_user(0, &frame->uc.uc_flags);
  120. err |= __put_user(0, &frame->uc.uc_link);
  121. err |= __compat_save_altstack(&frame->uc.uc_stack, regs->sp);
  122. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
  123. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  124. if (err)
  125. goto err;
  126. restorer = VDSO_SYM(&__vdso_rt_sigreturn);
  127. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  128. restorer = ptr_to_compat_reg(ksig->ka.sa.sa_restorer);
  129. /*
  130. * Set up registers for signal handler.
  131. * Registers that we don't modify keep the value they had from
  132. * user-space at the time we took the signal.
  133. * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
  134. * since some things rely on this (e.g. glibc's debug/segfault.c).
  135. */
  136. regs->pc = ptr_to_compat_reg(ksig->ka.sa.sa_handler);
  137. regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
  138. regs->sp = ptr_to_compat_reg(frame);
  139. regs->lr = restorer;
  140. regs->regs[0] = (unsigned long) sig;
  141. regs->regs[1] = ptr_to_compat_reg(&frame->info);
  142. regs->regs[2] = ptr_to_compat_reg(&frame->uc);
  143. regs->flags |= PT_FLAGS_CALLER_SAVES;
  144. return 0;
  145. err:
  146. trace_unhandled_signal("bad sigreturn frame", regs,
  147. (unsigned long)frame, SIGSEGV);
  148. return -EFAULT;
  149. }