signal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Based on arch/arm/kernel/signal.c
  3. *
  4. * Copyright (C) 1995-2009 Russell King
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/errno.h>
  20. #include <linux/signal.h>
  21. #include <linux/personality.h>
  22. #include <linux/freezer.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/tracehook.h>
  25. #include <linux/ratelimit.h>
  26. #include <asm/compat.h>
  27. #include <asm/debug-monitors.h>
  28. #include <asm/elf.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/ucontext.h>
  31. #include <asm/unistd.h>
  32. #include <asm/fpsimd.h>
  33. #include <asm/signal32.h>
  34. #include <asm/vdso.h>
  35. /*
  36. * Do a signal return; undo the signal stack. These are aligned to 128-bit.
  37. */
  38. struct rt_sigframe {
  39. struct siginfo info;
  40. struct ucontext uc;
  41. u64 fp;
  42. u64 lr;
  43. };
  44. static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
  45. {
  46. struct fpsimd_state *fpsimd = &current->thread.fpsimd_state;
  47. int err;
  48. /* dump the hardware registers to the fpsimd_state structure */
  49. fpsimd_save_state(fpsimd);
  50. /* copy the FP and status/control registers */
  51. err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
  52. __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
  53. __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
  54. /* copy the magic/size information */
  55. __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
  56. __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
  57. return err ? -EFAULT : 0;
  58. }
  59. static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
  60. {
  61. struct fpsimd_state fpsimd;
  62. __u32 magic, size;
  63. int err = 0;
  64. /* check the magic/size information */
  65. __get_user_error(magic, &ctx->head.magic, err);
  66. __get_user_error(size, &ctx->head.size, err);
  67. if (err)
  68. return -EFAULT;
  69. if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
  70. return -EINVAL;
  71. /* copy the FP and status/control registers */
  72. err = __copy_from_user(fpsimd.vregs, ctx->vregs,
  73. sizeof(fpsimd.vregs));
  74. __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
  75. __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
  76. /* load the hardware registers from the fpsimd_state structure */
  77. if (!err) {
  78. preempt_disable();
  79. fpsimd_load_state(&fpsimd);
  80. preempt_enable();
  81. }
  82. return err ? -EFAULT : 0;
  83. }
  84. static int restore_sigframe(struct pt_regs *regs,
  85. struct rt_sigframe __user *sf)
  86. {
  87. sigset_t set;
  88. int i, err;
  89. struct aux_context __user *aux =
  90. (struct aux_context __user *)sf->uc.uc_mcontext.__reserved;
  91. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  92. if (err == 0)
  93. set_current_blocked(&set);
  94. for (i = 0; i < 31; i++)
  95. __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
  96. err);
  97. __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
  98. __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
  99. __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
  100. /*
  101. * Avoid sys_rt_sigreturn() restarting.
  102. */
  103. regs->syscallno = ~0UL;
  104. err |= !valid_user_regs(&regs->user_regs);
  105. if (err == 0)
  106. err |= restore_fpsimd_context(&aux->fpsimd);
  107. return err;
  108. }
  109. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  110. {
  111. struct rt_sigframe __user *frame;
  112. /* Always make any pending restarted system calls return -EINTR */
  113. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  114. /*
  115. * Since we stacked the signal on a 128-bit boundary, then 'sp' should
  116. * be word aligned here.
  117. */
  118. if (regs->sp & 15)
  119. goto badframe;
  120. frame = (struct rt_sigframe __user *)regs->sp;
  121. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  122. goto badframe;
  123. if (restore_sigframe(regs, frame))
  124. goto badframe;
  125. if (restore_altstack(&frame->uc.uc_stack))
  126. goto badframe;
  127. return regs->regs[0];
  128. badframe:
  129. if (show_unhandled_signals)
  130. pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
  131. current->comm, task_pid_nr(current), __func__,
  132. regs->pc, regs->sp);
  133. force_sig(SIGSEGV, current);
  134. return 0;
  135. }
  136. static int setup_sigframe(struct rt_sigframe __user *sf,
  137. struct pt_regs *regs, sigset_t *set)
  138. {
  139. int i, err = 0;
  140. struct aux_context __user *aux =
  141. (struct aux_context __user *)sf->uc.uc_mcontext.__reserved;
  142. /* set up the stack frame for unwinding */
  143. __put_user_error(regs->regs[29], &sf->fp, err);
  144. __put_user_error(regs->regs[30], &sf->lr, err);
  145. for (i = 0; i < 31; i++)
  146. __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
  147. err);
  148. __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
  149. __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
  150. __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
  151. __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
  152. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
  153. if (err == 0)
  154. err |= preserve_fpsimd_context(&aux->fpsimd);
  155. /* set the "end" magic */
  156. __put_user_error(0, &aux->end.magic, err);
  157. __put_user_error(0, &aux->end.size, err);
  158. return err;
  159. }
  160. static struct rt_sigframe __user *get_sigframe(struct k_sigaction *ka,
  161. struct pt_regs *regs)
  162. {
  163. unsigned long sp, sp_top;
  164. struct rt_sigframe __user *frame;
  165. sp = sp_top = regs->sp;
  166. /*
  167. * This is the X/Open sanctioned signal stack switching.
  168. */
  169. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
  170. sp = sp_top = current->sas_ss_sp + current->sas_ss_size;
  171. sp = (sp - sizeof(struct rt_sigframe)) & ~15;
  172. frame = (struct rt_sigframe __user *)sp;
  173. /*
  174. * Check that we can actually write to the signal frame.
  175. */
  176. if (!access_ok(VERIFY_WRITE, frame, sp_top - sp))
  177. frame = NULL;
  178. return frame;
  179. }
  180. static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  181. void __user *frame, int usig)
  182. {
  183. __sigrestore_t sigtramp;
  184. regs->regs[0] = usig;
  185. regs->sp = (unsigned long)frame;
  186. regs->regs[29] = regs->sp + offsetof(struct rt_sigframe, fp);
  187. regs->pc = (unsigned long)ka->sa.sa_handler;
  188. if (ka->sa.sa_flags & SA_RESTORER)
  189. sigtramp = ka->sa.sa_restorer;
  190. else
  191. sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
  192. regs->regs[30] = (unsigned long)sigtramp;
  193. }
  194. static int setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
  195. sigset_t *set, struct pt_regs *regs)
  196. {
  197. struct rt_sigframe __user *frame;
  198. int err = 0;
  199. frame = get_sigframe(ka, regs);
  200. if (!frame)
  201. return 1;
  202. __put_user_error(0, &frame->uc.uc_flags, err);
  203. __put_user_error(NULL, &frame->uc.uc_link, err);
  204. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  205. err |= setup_sigframe(frame, regs, set);
  206. if (err == 0) {
  207. setup_return(regs, ka, frame, usig);
  208. if (ka->sa.sa_flags & SA_SIGINFO) {
  209. err |= copy_siginfo_to_user(&frame->info, info);
  210. regs->regs[1] = (unsigned long)&frame->info;
  211. regs->regs[2] = (unsigned long)&frame->uc;
  212. }
  213. }
  214. return err;
  215. }
  216. static void setup_restart_syscall(struct pt_regs *regs)
  217. {
  218. if (is_compat_task())
  219. compat_setup_restart_syscall(regs);
  220. else
  221. regs->regs[8] = __NR_restart_syscall;
  222. }
  223. /*
  224. * OK, we're invoking a handler
  225. */
  226. static void handle_signal(unsigned long sig, struct k_sigaction *ka,
  227. siginfo_t *info, struct pt_regs *regs)
  228. {
  229. struct thread_info *thread = current_thread_info();
  230. struct task_struct *tsk = current;
  231. sigset_t *oldset = sigmask_to_save();
  232. int usig = sig;
  233. int ret;
  234. /*
  235. * translate the signal
  236. */
  237. if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
  238. usig = thread->exec_domain->signal_invmap[usig];
  239. /*
  240. * Set up the stack frame
  241. */
  242. if (is_compat_task()) {
  243. if (ka->sa.sa_flags & SA_SIGINFO)
  244. ret = compat_setup_rt_frame(usig, ka, info, oldset,
  245. regs);
  246. else
  247. ret = compat_setup_frame(usig, ka, oldset, regs);
  248. } else {
  249. ret = setup_rt_frame(usig, ka, info, oldset, regs);
  250. }
  251. /*
  252. * Check that the resulting registers are actually sane.
  253. */
  254. ret |= !valid_user_regs(&regs->user_regs);
  255. if (ret != 0) {
  256. force_sigsegv(sig, tsk);
  257. return;
  258. }
  259. /*
  260. * Fast forward the stepping logic so we step into the signal
  261. * handler.
  262. */
  263. user_fastforward_single_step(tsk);
  264. signal_delivered(sig, info, ka, regs, 0);
  265. }
  266. /*
  267. * Note that 'init' is a special process: it doesn't get signals it doesn't
  268. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  269. * mistake.
  270. *
  271. * Note that we go through the signals twice: once to check the signals that
  272. * the kernel can handle, and then we build all the user-level signal handling
  273. * stack-frames in one go after that.
  274. */
  275. static void do_signal(struct pt_regs *regs)
  276. {
  277. unsigned long continue_addr = 0, restart_addr = 0;
  278. struct k_sigaction ka;
  279. siginfo_t info;
  280. int signr, retval = 0;
  281. int syscall = (int)regs->syscallno;
  282. /*
  283. * If we were from a system call, check for system call restarting...
  284. */
  285. if (syscall >= 0) {
  286. continue_addr = regs->pc;
  287. restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
  288. retval = regs->regs[0];
  289. /*
  290. * Avoid additional syscall restarting via ret_to_user.
  291. */
  292. regs->syscallno = ~0UL;
  293. /*
  294. * Prepare for system call restart. We do this here so that a
  295. * debugger will see the already changed PC.
  296. */
  297. switch (retval) {
  298. case -ERESTARTNOHAND:
  299. case -ERESTARTSYS:
  300. case -ERESTARTNOINTR:
  301. case -ERESTART_RESTARTBLOCK:
  302. regs->regs[0] = regs->orig_x0;
  303. regs->pc = restart_addr;
  304. break;
  305. }
  306. }
  307. /*
  308. * Get the signal to deliver. When running under ptrace, at this point
  309. * the debugger may change all of our registers.
  310. */
  311. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  312. if (signr > 0) {
  313. /*
  314. * Depending on the signal settings, we may need to revert the
  315. * decision to restart the system call, but skip this if a
  316. * debugger has chosen to restart at a different PC.
  317. */
  318. if (regs->pc == restart_addr &&
  319. (retval == -ERESTARTNOHAND ||
  320. retval == -ERESTART_RESTARTBLOCK ||
  321. (retval == -ERESTARTSYS &&
  322. !(ka.sa.sa_flags & SA_RESTART)))) {
  323. regs->regs[0] = -EINTR;
  324. regs->pc = continue_addr;
  325. }
  326. handle_signal(signr, &ka, &info, regs);
  327. return;
  328. }
  329. /*
  330. * Handle restarting a different system call. As above, if a debugger
  331. * has chosen to restart at a different PC, ignore the restart.
  332. */
  333. if (syscall >= 0 && regs->pc == restart_addr) {
  334. if (retval == -ERESTART_RESTARTBLOCK)
  335. setup_restart_syscall(regs);
  336. user_rewind_single_step(current);
  337. }
  338. restore_saved_sigmask();
  339. }
  340. asmlinkage void do_notify_resume(struct pt_regs *regs,
  341. unsigned int thread_flags)
  342. {
  343. if (thread_flags & _TIF_SIGPENDING)
  344. do_signal(regs);
  345. if (thread_flags & _TIF_NOTIFY_RESUME) {
  346. clear_thread_flag(TIF_NOTIFY_RESUME);
  347. tracehook_notify_resume(regs);
  348. }
  349. }