signal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/compat.h>
  20. #include <linux/errno.h>
  21. #include <linux/signal.h>
  22. #include <linux/personality.h>
  23. #include <linux/freezer.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/tracehook.h>
  26. #include <linux/ratelimit.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_preserve_current_state();
  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. fpsimd_update_current_state(&fpsimd);
  79. return err ? -EFAULT : 0;
  80. }
  81. static int restore_sigframe(struct pt_regs *regs,
  82. struct rt_sigframe __user *sf)
  83. {
  84. sigset_t set;
  85. int i, err;
  86. void *aux = sf->uc.uc_mcontext.__reserved;
  87. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  88. if (err == 0)
  89. set_current_blocked(&set);
  90. for (i = 0; i < 31; i++)
  91. __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
  92. err);
  93. __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
  94. __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
  95. __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
  96. /*
  97. * Avoid sys_rt_sigreturn() restarting.
  98. */
  99. regs->syscallno = ~0UL;
  100. err |= !valid_user_regs(&regs->user_regs, current);
  101. if (err == 0) {
  102. struct fpsimd_context *fpsimd_ctx =
  103. container_of(aux, struct fpsimd_context, head);
  104. err |= restore_fpsimd_context(fpsimd_ctx);
  105. }
  106. return err;
  107. }
  108. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  109. {
  110. struct rt_sigframe __user *frame;
  111. /* Always make any pending restarted system calls return -EINTR */
  112. current->restart_block.fn = do_no_restart_syscall;
  113. /*
  114. * Since we stacked the signal on a 128-bit boundary, then 'sp' should
  115. * be word aligned here.
  116. */
  117. if (regs->sp & 15)
  118. goto badframe;
  119. frame = (struct rt_sigframe __user *)regs->sp;
  120. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  121. goto badframe;
  122. if (restore_sigframe(regs, frame))
  123. goto badframe;
  124. if (restore_altstack(&frame->uc.uc_stack))
  125. goto badframe;
  126. return regs->regs[0];
  127. badframe:
  128. if (show_unhandled_signals)
  129. pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
  130. current->comm, task_pid_nr(current), __func__,
  131. regs->pc, regs->sp);
  132. force_sig(SIGSEGV, current);
  133. return 0;
  134. }
  135. static int setup_sigframe(struct rt_sigframe __user *sf,
  136. struct pt_regs *regs, sigset_t *set)
  137. {
  138. int i, err = 0;
  139. void *aux = sf->uc.uc_mcontext.__reserved;
  140. struct _aarch64_ctx *end;
  141. /* set up the stack frame for unwinding */
  142. __put_user_error(regs->regs[29], &sf->fp, err);
  143. __put_user_error(regs->regs[30], &sf->lr, err);
  144. for (i = 0; i < 31; i++)
  145. __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
  146. err);
  147. __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
  148. __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
  149. __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
  150. __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
  151. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
  152. if (err == 0) {
  153. struct fpsimd_context *fpsimd_ctx =
  154. container_of(aux, struct fpsimd_context, head);
  155. err |= preserve_fpsimd_context(fpsimd_ctx);
  156. aux += sizeof(*fpsimd_ctx);
  157. }
  158. /* fault information, if valid */
  159. if (current->thread.fault_code) {
  160. struct esr_context *esr_ctx =
  161. container_of(aux, struct esr_context, head);
  162. __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
  163. __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
  164. __put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
  165. aux += sizeof(*esr_ctx);
  166. }
  167. /* set the "end" magic */
  168. end = aux;
  169. __put_user_error(0, &end->magic, err);
  170. __put_user_error(0, &end->size, err);
  171. return err;
  172. }
  173. static struct rt_sigframe __user *get_sigframe(struct ksignal *ksig,
  174. struct pt_regs *regs)
  175. {
  176. unsigned long sp, sp_top;
  177. struct rt_sigframe __user *frame;
  178. sp = sp_top = sigsp(regs->sp, ksig);
  179. sp = (sp - sizeof(struct rt_sigframe)) & ~15;
  180. frame = (struct rt_sigframe __user *)sp;
  181. /*
  182. * Check that we can actually write to the signal frame.
  183. */
  184. if (!access_ok(VERIFY_WRITE, frame, sp_top - sp))
  185. frame = NULL;
  186. return frame;
  187. }
  188. static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  189. void __user *frame, int usig)
  190. {
  191. __sigrestore_t sigtramp;
  192. regs->regs[0] = usig;
  193. regs->sp = (unsigned long)frame;
  194. regs->regs[29] = regs->sp + offsetof(struct rt_sigframe, fp);
  195. regs->pc = (unsigned long)ka->sa.sa_handler;
  196. if (ka->sa.sa_flags & SA_RESTORER)
  197. sigtramp = ka->sa.sa_restorer;
  198. else
  199. sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
  200. regs->regs[30] = (unsigned long)sigtramp;
  201. }
  202. static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
  203. struct pt_regs *regs)
  204. {
  205. struct rt_sigframe __user *frame;
  206. int err = 0;
  207. frame = get_sigframe(ksig, regs);
  208. if (!frame)
  209. return 1;
  210. __put_user_error(0, &frame->uc.uc_flags, err);
  211. __put_user_error(NULL, &frame->uc.uc_link, err);
  212. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  213. err |= setup_sigframe(frame, regs, set);
  214. if (err == 0) {
  215. setup_return(regs, &ksig->ka, frame, usig);
  216. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  217. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  218. regs->regs[1] = (unsigned long)&frame->info;
  219. regs->regs[2] = (unsigned long)&frame->uc;
  220. }
  221. }
  222. return err;
  223. }
  224. static void setup_restart_syscall(struct pt_regs *regs)
  225. {
  226. if (is_compat_task())
  227. compat_setup_restart_syscall(regs);
  228. else
  229. regs->regs[8] = __NR_restart_syscall;
  230. }
  231. /*
  232. * OK, we're invoking a handler
  233. */
  234. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  235. {
  236. struct task_struct *tsk = current;
  237. sigset_t *oldset = sigmask_to_save();
  238. int usig = ksig->sig;
  239. int ret;
  240. /*
  241. * Set up the stack frame
  242. */
  243. if (is_compat_task()) {
  244. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  245. ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
  246. else
  247. ret = compat_setup_frame(usig, ksig, oldset, regs);
  248. } else {
  249. ret = setup_rt_frame(usig, ksig, oldset, regs);
  250. }
  251. /*
  252. * Check that the resulting registers are actually sane.
  253. */
  254. ret |= !valid_user_regs(&regs->user_regs, current);
  255. /*
  256. * Fast forward the stepping logic so we step into the signal
  257. * handler.
  258. */
  259. if (!ret)
  260. user_fastforward_single_step(tsk);
  261. signal_setup_done(ret, ksig, 0);
  262. }
  263. /*
  264. * Note that 'init' is a special process: it doesn't get signals it doesn't
  265. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  266. * mistake.
  267. *
  268. * Note that we go through the signals twice: once to check the signals that
  269. * the kernel can handle, and then we build all the user-level signal handling
  270. * stack-frames in one go after that.
  271. */
  272. static void do_signal(struct pt_regs *regs)
  273. {
  274. unsigned long continue_addr = 0, restart_addr = 0;
  275. int retval = 0;
  276. int syscall = (int)regs->syscallno;
  277. struct ksignal ksig;
  278. /*
  279. * If we were from a system call, check for system call restarting...
  280. */
  281. if (syscall >= 0) {
  282. continue_addr = regs->pc;
  283. restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
  284. retval = regs->regs[0];
  285. /*
  286. * Avoid additional syscall restarting via ret_to_user.
  287. */
  288. regs->syscallno = ~0UL;
  289. /*
  290. * Prepare for system call restart. We do this here so that a
  291. * debugger will see the already changed PC.
  292. */
  293. switch (retval) {
  294. case -ERESTARTNOHAND:
  295. case -ERESTARTSYS:
  296. case -ERESTARTNOINTR:
  297. case -ERESTART_RESTARTBLOCK:
  298. regs->regs[0] = regs->orig_x0;
  299. regs->pc = restart_addr;
  300. break;
  301. }
  302. }
  303. /*
  304. * Get the signal to deliver. When running under ptrace, at this point
  305. * the debugger may change all of our registers.
  306. */
  307. if (get_signal(&ksig)) {
  308. /*
  309. * Depending on the signal settings, we may need to revert the
  310. * decision to restart the system call, but skip this if a
  311. * debugger has chosen to restart at a different PC.
  312. */
  313. if (regs->pc == restart_addr &&
  314. (retval == -ERESTARTNOHAND ||
  315. retval == -ERESTART_RESTARTBLOCK ||
  316. (retval == -ERESTARTSYS &&
  317. !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
  318. regs->regs[0] = -EINTR;
  319. regs->pc = continue_addr;
  320. }
  321. handle_signal(&ksig, regs);
  322. return;
  323. }
  324. /*
  325. * Handle restarting a different system call. As above, if a debugger
  326. * has chosen to restart at a different PC, ignore the restart.
  327. */
  328. if (syscall >= 0 && regs->pc == restart_addr) {
  329. if (retval == -ERESTART_RESTARTBLOCK)
  330. setup_restart_syscall(regs);
  331. user_rewind_single_step(current);
  332. }
  333. restore_saved_sigmask();
  334. }
  335. asmlinkage void do_notify_resume(struct pt_regs *regs,
  336. unsigned int thread_flags)
  337. {
  338. if (thread_flags & _TIF_SIGPENDING)
  339. do_signal(regs);
  340. if (thread_flags & _TIF_NOTIFY_RESUME) {
  341. clear_thread_flag(TIF_NOTIFY_RESUME);
  342. tracehook_notify_resume(regs);
  343. }
  344. if (thread_flags & _TIF_FOREIGN_FPSTATE)
  345. fpsimd_restore_current_state();
  346. }