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/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. };
  42. static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
  43. {
  44. struct fpsimd_state *fpsimd = &current->thread.fpsimd_state;
  45. int err;
  46. /* dump the hardware registers to the fpsimd_state structure */
  47. fpsimd_save_state(fpsimd);
  48. /* copy the FP and status/control registers */
  49. err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
  50. __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
  51. __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
  52. /* copy the magic/size information */
  53. __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
  54. __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
  55. return err ? -EFAULT : 0;
  56. }
  57. static int restore_fpsimd_context(struct fpsimd_context __user *ctx)
  58. {
  59. struct fpsimd_state fpsimd;
  60. __u32 magic, size;
  61. int err = 0;
  62. /* check the magic/size information */
  63. __get_user_error(magic, &ctx->head.magic, err);
  64. __get_user_error(size, &ctx->head.size, err);
  65. if (err)
  66. return -EFAULT;
  67. if (magic != FPSIMD_MAGIC || size != sizeof(struct fpsimd_context))
  68. return -EINVAL;
  69. /* copy the FP and status/control registers */
  70. err = __copy_from_user(fpsimd.vregs, ctx->vregs,
  71. sizeof(fpsimd.vregs));
  72. __get_user_error(fpsimd.fpsr, &ctx->fpsr, err);
  73. __get_user_error(fpsimd.fpcr, &ctx->fpcr, err);
  74. /* load the hardware registers from the fpsimd_state structure */
  75. if (!err) {
  76. preempt_disable();
  77. fpsimd_load_state(&fpsimd);
  78. preempt_enable();
  79. }
  80. return err ? -EFAULT : 0;
  81. }
  82. static int restore_sigframe(struct pt_regs *regs,
  83. struct rt_sigframe __user *sf)
  84. {
  85. sigset_t set;
  86. int i, err;
  87. struct aux_context __user *aux =
  88. (struct aux_context __user *)sf->uc.uc_mcontext.__reserved;
  89. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  90. if (err == 0)
  91. set_current_blocked(&set);
  92. for (i = 0; i < 31; i++)
  93. __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
  94. err);
  95. __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
  96. __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
  97. __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
  98. /*
  99. * Avoid sys_rt_sigreturn() restarting.
  100. */
  101. regs->syscallno = ~0UL;
  102. err |= !valid_user_regs(&regs->user_regs);
  103. if (err == 0)
  104. err |= restore_fpsimd_context(&aux->fpsimd);
  105. return err;
  106. }
  107. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  108. {
  109. struct rt_sigframe __user *frame;
  110. /* Always make any pending restarted system calls return -EINTR */
  111. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  112. /*
  113. * Since we stacked the signal on a 128-bit boundary, then 'sp' should
  114. * be word aligned here.
  115. */
  116. if (regs->sp & 15)
  117. goto badframe;
  118. frame = (struct rt_sigframe __user *)regs->sp;
  119. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  120. goto badframe;
  121. if (restore_sigframe(regs, frame))
  122. goto badframe;
  123. if (do_sigaltstack(&frame->uc.uc_stack,
  124. NULL, regs->sp) == -EFAULT)
  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. asmlinkage long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  136. unsigned long sp)
  137. {
  138. return do_sigaltstack(uss, uoss, sp);
  139. }
  140. static int setup_sigframe(struct rt_sigframe __user *sf,
  141. struct pt_regs *regs, sigset_t *set)
  142. {
  143. int i, err = 0;
  144. struct aux_context __user *aux =
  145. (struct aux_context __user *)sf->uc.uc_mcontext.__reserved;
  146. for (i = 0; i < 31; i++)
  147. __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
  148. err);
  149. __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
  150. __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
  151. __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
  152. __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
  153. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
  154. if (err == 0)
  155. err |= preserve_fpsimd_context(&aux->fpsimd);
  156. /* set the "end" magic */
  157. __put_user_error(0, &aux->end.magic, err);
  158. __put_user_error(0, &aux->end.size, err);
  159. return err;
  160. }
  161. static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
  162. int framesize)
  163. {
  164. unsigned long sp, sp_top;
  165. void __user *frame;
  166. sp = sp_top = regs->sp;
  167. /*
  168. * This is the X/Open sanctioned signal stack switching.
  169. */
  170. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
  171. sp = sp_top = current->sas_ss_sp + current->sas_ss_size;
  172. /* room for stack frame (FP, LR) */
  173. sp -= 16;
  174. sp = (sp - framesize) & ~15;
  175. frame = (void __user *)sp;
  176. /*
  177. * Check that we can actually write to the signal frame.
  178. */
  179. if (!access_ok(VERIFY_WRITE, frame, sp_top - sp))
  180. frame = NULL;
  181. return frame;
  182. }
  183. static int setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  184. void __user *frame, int usig)
  185. {
  186. int err = 0;
  187. __sigrestore_t sigtramp;
  188. unsigned long __user *sp = (unsigned long __user *)regs->sp;
  189. /* set up the stack frame */
  190. __put_user_error(regs->regs[29], sp - 2, err);
  191. __put_user_error(regs->regs[30], sp - 1, err);
  192. regs->regs[0] = usig;
  193. regs->regs[29] = regs->sp - 16;
  194. regs->sp = (unsigned long)frame;
  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. return err;
  202. }
  203. static int setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
  204. sigset_t *set, struct pt_regs *regs)
  205. {
  206. struct rt_sigframe __user *frame;
  207. stack_t stack;
  208. int err = 0;
  209. frame = get_sigframe(ka, regs, sizeof(*frame));
  210. if (!frame)
  211. return 1;
  212. __put_user_error(0, &frame->uc.uc_flags, err);
  213. __put_user_error(NULL, &frame->uc.uc_link, err);
  214. memset(&stack, 0, sizeof(stack));
  215. stack.ss_sp = (void __user *)current->sas_ss_sp;
  216. stack.ss_flags = sas_ss_flags(regs->sp);
  217. stack.ss_size = current->sas_ss_size;
  218. err |= __copy_to_user(&frame->uc.uc_stack, &stack, sizeof(stack));
  219. err |= setup_sigframe(frame, regs, set);
  220. if (err == 0)
  221. err = setup_return(regs, ka, frame, usig);
  222. if (err == 0 && ka->sa.sa_flags & SA_SIGINFO) {
  223. err |= copy_siginfo_to_user(&frame->info, info);
  224. regs->regs[1] = (unsigned long)&frame->info;
  225. regs->regs[2] = (unsigned long)&frame->uc;
  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. }