signal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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);
  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_thread_info()->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 k_sigaction *ka,
  174. struct pt_regs *regs)
  175. {
  176. unsigned long sp, sp_top;
  177. struct rt_sigframe __user *frame;
  178. sp = sp_top = regs->sp;
  179. /*
  180. * This is the X/Open sanctioned signal stack switching.
  181. */
  182. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
  183. sp = sp_top = current->sas_ss_sp + current->sas_ss_size;
  184. sp = (sp - sizeof(struct rt_sigframe)) & ~15;
  185. frame = (struct rt_sigframe __user *)sp;
  186. /*
  187. * Check that we can actually write to the signal frame.
  188. */
  189. if (!access_ok(VERIFY_WRITE, frame, sp_top - sp))
  190. frame = NULL;
  191. return frame;
  192. }
  193. static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  194. void __user *frame, int usig)
  195. {
  196. __sigrestore_t sigtramp;
  197. regs->regs[0] = usig;
  198. regs->sp = (unsigned long)frame;
  199. regs->regs[29] = regs->sp + offsetof(struct rt_sigframe, fp);
  200. regs->pc = (unsigned long)ka->sa.sa_handler;
  201. if (ka->sa.sa_flags & SA_RESTORER)
  202. sigtramp = ka->sa.sa_restorer;
  203. else
  204. sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
  205. regs->regs[30] = (unsigned long)sigtramp;
  206. }
  207. static int setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
  208. sigset_t *set, struct pt_regs *regs)
  209. {
  210. struct rt_sigframe __user *frame;
  211. int err = 0;
  212. frame = get_sigframe(ka, regs);
  213. if (!frame)
  214. return 1;
  215. __put_user_error(0, &frame->uc.uc_flags, err);
  216. __put_user_error(NULL, &frame->uc.uc_link, err);
  217. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  218. err |= setup_sigframe(frame, regs, set);
  219. if (err == 0) {
  220. setup_return(regs, ka, frame, usig);
  221. if (ka->sa.sa_flags & SA_SIGINFO) {
  222. err |= copy_siginfo_to_user(&frame->info, info);
  223. regs->regs[1] = (unsigned long)&frame->info;
  224. regs->regs[2] = (unsigned long)&frame->uc;
  225. }
  226. }
  227. return err;
  228. }
  229. static void setup_restart_syscall(struct pt_regs *regs)
  230. {
  231. if (is_compat_task())
  232. compat_setup_restart_syscall(regs);
  233. else
  234. regs->regs[8] = __NR_restart_syscall;
  235. }
  236. /*
  237. * OK, we're invoking a handler
  238. */
  239. static void handle_signal(unsigned long sig, struct k_sigaction *ka,
  240. siginfo_t *info, struct pt_regs *regs)
  241. {
  242. struct thread_info *thread = current_thread_info();
  243. struct task_struct *tsk = current;
  244. sigset_t *oldset = sigmask_to_save();
  245. int usig = sig;
  246. int ret;
  247. /*
  248. * translate the signal
  249. */
  250. if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
  251. usig = thread->exec_domain->signal_invmap[usig];
  252. /*
  253. * Set up the stack frame
  254. */
  255. if (is_compat_task()) {
  256. if (ka->sa.sa_flags & SA_SIGINFO)
  257. ret = compat_setup_rt_frame(usig, ka, info, oldset,
  258. regs);
  259. else
  260. ret = compat_setup_frame(usig, ka, oldset, regs);
  261. } else {
  262. ret = setup_rt_frame(usig, ka, info, oldset, regs);
  263. }
  264. /*
  265. * Check that the resulting registers are actually sane.
  266. */
  267. ret |= !valid_user_regs(&regs->user_regs);
  268. if (ret != 0) {
  269. force_sigsegv(sig, tsk);
  270. return;
  271. }
  272. /*
  273. * Fast forward the stepping logic so we step into the signal
  274. * handler.
  275. */
  276. user_fastforward_single_step(tsk);
  277. signal_delivered(sig, info, ka, regs, 0);
  278. }
  279. /*
  280. * Note that 'init' is a special process: it doesn't get signals it doesn't
  281. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  282. * mistake.
  283. *
  284. * Note that we go through the signals twice: once to check the signals that
  285. * the kernel can handle, and then we build all the user-level signal handling
  286. * stack-frames in one go after that.
  287. */
  288. static void do_signal(struct pt_regs *regs)
  289. {
  290. unsigned long continue_addr = 0, restart_addr = 0;
  291. struct k_sigaction ka;
  292. siginfo_t info;
  293. int signr, retval = 0;
  294. int syscall = (int)regs->syscallno;
  295. /*
  296. * If we were from a system call, check for system call restarting...
  297. */
  298. if (syscall >= 0) {
  299. continue_addr = regs->pc;
  300. restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
  301. retval = regs->regs[0];
  302. /*
  303. * Avoid additional syscall restarting via ret_to_user.
  304. */
  305. regs->syscallno = ~0UL;
  306. /*
  307. * Prepare for system call restart. We do this here so that a
  308. * debugger will see the already changed PC.
  309. */
  310. switch (retval) {
  311. case -ERESTARTNOHAND:
  312. case -ERESTARTSYS:
  313. case -ERESTARTNOINTR:
  314. case -ERESTART_RESTARTBLOCK:
  315. regs->regs[0] = regs->orig_x0;
  316. regs->pc = restart_addr;
  317. break;
  318. }
  319. }
  320. /*
  321. * Get the signal to deliver. When running under ptrace, at this point
  322. * the debugger may change all of our registers.
  323. */
  324. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  325. if (signr > 0) {
  326. /*
  327. * Depending on the signal settings, we may need to revert the
  328. * decision to restart the system call, but skip this if a
  329. * debugger has chosen to restart at a different PC.
  330. */
  331. if (regs->pc == restart_addr &&
  332. (retval == -ERESTARTNOHAND ||
  333. retval == -ERESTART_RESTARTBLOCK ||
  334. (retval == -ERESTARTSYS &&
  335. !(ka.sa.sa_flags & SA_RESTART)))) {
  336. regs->regs[0] = -EINTR;
  337. regs->pc = continue_addr;
  338. }
  339. handle_signal(signr, &ka, &info, regs);
  340. return;
  341. }
  342. /*
  343. * Handle restarting a different system call. As above, if a debugger
  344. * has chosen to restart at a different PC, ignore the restart.
  345. */
  346. if (syscall >= 0 && regs->pc == restart_addr) {
  347. if (retval == -ERESTART_RESTARTBLOCK)
  348. setup_restart_syscall(regs);
  349. user_rewind_single_step(current);
  350. }
  351. restore_saved_sigmask();
  352. }
  353. asmlinkage void do_notify_resume(struct pt_regs *regs,
  354. unsigned int thread_flags)
  355. {
  356. if (thread_flags & _TIF_SIGPENDING)
  357. do_signal(regs);
  358. if (thread_flags & _TIF_NOTIFY_RESUME) {
  359. clear_thread_flag(TIF_NOTIFY_RESUME);
  360. tracehook_notify_resume(regs);
  361. }
  362. if (thread_flags & _TIF_FOREIGN_FPSTATE)
  363. fpsimd_restore_current_state();
  364. }