signal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Common signal handling code for both 32 and 64 bits
  3. *
  4. * Copyright (c) 2007 Benjamin Herrenschmidt, IBM Corporation
  5. * Extracted from signal_32.c and signal_64.c
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file README.legal in the main directory of
  9. * this archive for more details.
  10. */
  11. #include <linux/resume_user_mode.h>
  12. #include <linux/signal.h>
  13. #include <linux/uprobes.h>
  14. #include <linux/key.h>
  15. #include <linux/context_tracking.h>
  16. #include <linux/livepatch.h>
  17. #include <linux/syscalls.h>
  18. #include <asm/hw_breakpoint.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/switch_to.h>
  21. #include <asm/unistd.h>
  22. #include <asm/debug.h>
  23. #include <asm/tm.h>
  24. #include "signal.h"
  25. #ifdef CONFIG_VSX
  26. unsigned long copy_fpr_to_user(void __user *to,
  27. struct task_struct *task)
  28. {
  29. u64 buf[ELF_NFPREG];
  30. int i;
  31. /* save FPR copy to local buffer then write to the thread_struct */
  32. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  33. buf[i] = task->thread.TS_FPR(i);
  34. buf[i] = task->thread.fp_state.fpscr;
  35. return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
  36. }
  37. unsigned long copy_fpr_from_user(struct task_struct *task,
  38. void __user *from)
  39. {
  40. u64 buf[ELF_NFPREG];
  41. int i;
  42. if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
  43. return 1;
  44. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  45. task->thread.TS_FPR(i) = buf[i];
  46. task->thread.fp_state.fpscr = buf[i];
  47. return 0;
  48. }
  49. unsigned long copy_vsx_to_user(void __user *to,
  50. struct task_struct *task)
  51. {
  52. u64 buf[ELF_NVSRHALFREG];
  53. int i;
  54. /* save FPR copy to local buffer then write to the thread_struct */
  55. for (i = 0; i < ELF_NVSRHALFREG; i++)
  56. buf[i] = task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET];
  57. return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
  58. }
  59. unsigned long copy_vsx_from_user(struct task_struct *task,
  60. void __user *from)
  61. {
  62. u64 buf[ELF_NVSRHALFREG];
  63. int i;
  64. if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
  65. return 1;
  66. for (i = 0; i < ELF_NVSRHALFREG ; i++)
  67. task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  68. return 0;
  69. }
  70. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  71. unsigned long copy_ckfpr_to_user(void __user *to,
  72. struct task_struct *task)
  73. {
  74. u64 buf[ELF_NFPREG];
  75. int i;
  76. /* save FPR copy to local buffer then write to the thread_struct */
  77. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  78. buf[i] = task->thread.TS_CKFPR(i);
  79. buf[i] = task->thread.ckfp_state.fpscr;
  80. return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
  81. }
  82. unsigned long copy_ckfpr_from_user(struct task_struct *task,
  83. void __user *from)
  84. {
  85. u64 buf[ELF_NFPREG];
  86. int i;
  87. if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
  88. return 1;
  89. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  90. task->thread.TS_CKFPR(i) = buf[i];
  91. task->thread.ckfp_state.fpscr = buf[i];
  92. return 0;
  93. }
  94. unsigned long copy_ckvsx_to_user(void __user *to,
  95. struct task_struct *task)
  96. {
  97. u64 buf[ELF_NVSRHALFREG];
  98. int i;
  99. /* save FPR copy to local buffer then write to the thread_struct */
  100. for (i = 0; i < ELF_NVSRHALFREG; i++)
  101. buf[i] = task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET];
  102. return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
  103. }
  104. unsigned long copy_ckvsx_from_user(struct task_struct *task,
  105. void __user *from)
  106. {
  107. u64 buf[ELF_NVSRHALFREG];
  108. int i;
  109. if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
  110. return 1;
  111. for (i = 0; i < ELF_NVSRHALFREG ; i++)
  112. task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  113. return 0;
  114. }
  115. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  116. #endif
  117. /* Log an error when sending an unhandled signal to a process. Controlled
  118. * through debug.exception-trace sysctl.
  119. */
  120. int show_unhandled_signals = 1;
  121. unsigned long get_min_sigframe_size(void)
  122. {
  123. if (IS_ENABLED(CONFIG_PPC64))
  124. return get_min_sigframe_size_64();
  125. else
  126. return get_min_sigframe_size_32();
  127. }
  128. #ifdef CONFIG_COMPAT
  129. unsigned long get_min_sigframe_size_compat(void)
  130. {
  131. return get_min_sigframe_size_32();
  132. }
  133. #endif
  134. /*
  135. * Allocate space for the signal frame
  136. */
  137. static unsigned long get_tm_stackpointer(struct task_struct *tsk);
  138. void __user *get_sigframe(struct ksignal *ksig, struct task_struct *tsk,
  139. size_t frame_size, int is_32)
  140. {
  141. unsigned long oldsp, newsp;
  142. unsigned long sp = get_tm_stackpointer(tsk);
  143. /* Default to using normal stack */
  144. if (is_32)
  145. oldsp = sp & 0x0ffffffffUL;
  146. else
  147. oldsp = sp;
  148. oldsp = sigsp(oldsp, ksig);
  149. newsp = (oldsp - frame_size) & ~0xFUL;
  150. return (void __user *)newsp;
  151. }
  152. static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
  153. int has_handler)
  154. {
  155. unsigned long ret = regs->gpr[3];
  156. int restart = 1;
  157. /* syscall ? */
  158. if (!trap_is_syscall(regs))
  159. return;
  160. if (trap_norestart(regs))
  161. return;
  162. /* error signalled ? */
  163. if (trap_is_scv(regs)) {
  164. /* 32-bit compat mode sign extend? */
  165. if (!IS_ERR_VALUE(ret))
  166. return;
  167. ret = -ret;
  168. } else if (!(regs->ccr & 0x10000000)) {
  169. return;
  170. }
  171. switch (ret) {
  172. case ERESTART_RESTARTBLOCK:
  173. case ERESTARTNOHAND:
  174. /* ERESTARTNOHAND means that the syscall should only be
  175. * restarted if there was no handler for the signal, and since
  176. * we only get here if there is a handler, we dont restart.
  177. */
  178. restart = !has_handler;
  179. break;
  180. case ERESTARTSYS:
  181. /* ERESTARTSYS means to restart the syscall if there is no
  182. * handler or the handler was registered with SA_RESTART
  183. */
  184. restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
  185. break;
  186. case ERESTARTNOINTR:
  187. /* ERESTARTNOINTR means that the syscall should be
  188. * called again after the signal handler returns.
  189. */
  190. break;
  191. default:
  192. return;
  193. }
  194. if (restart) {
  195. if (ret == ERESTART_RESTARTBLOCK)
  196. regs->gpr[0] = __NR_restart_syscall;
  197. else
  198. regs->gpr[3] = regs->orig_gpr3;
  199. regs_add_return_ip(regs, -4);
  200. regs->result = 0;
  201. } else {
  202. if (trap_is_scv(regs)) {
  203. regs->result = -EINTR;
  204. regs->gpr[3] = -EINTR;
  205. } else {
  206. regs->result = -EINTR;
  207. regs->gpr[3] = EINTR;
  208. regs->ccr |= 0x10000000;
  209. }
  210. }
  211. }
  212. static void do_signal(struct task_struct *tsk)
  213. {
  214. sigset_t *oldset = sigmask_to_save();
  215. struct ksignal ksig = { .sig = 0 };
  216. int ret;
  217. BUG_ON(tsk != current);
  218. get_signal(&ksig);
  219. /* Is there any syscall restart business here ? */
  220. check_syscall_restart(tsk->thread.regs, &ksig.ka, ksig.sig > 0);
  221. if (ksig.sig <= 0) {
  222. /* No signal to deliver -- put the saved sigmask back */
  223. restore_saved_sigmask();
  224. set_trap_norestart(tsk->thread.regs);
  225. return; /* no signals delivered */
  226. }
  227. /*
  228. * Reenable the DABR before delivering the signal to
  229. * user space. The DABR will have been cleared if it
  230. * triggered inside the kernel.
  231. */
  232. if (!IS_ENABLED(CONFIG_PPC_ADV_DEBUG_REGS)) {
  233. int i;
  234. for (i = 0; i < nr_wp_slots(); i++) {
  235. if (tsk->thread.hw_brk[i].address && tsk->thread.hw_brk[i].type)
  236. __set_breakpoint(i, &tsk->thread.hw_brk[i]);
  237. }
  238. }
  239. /* Re-enable the breakpoints for the signal stack */
  240. thread_change_pc(tsk, tsk->thread.regs);
  241. rseq_signal_deliver(&ksig, tsk->thread.regs);
  242. if (is_32bit_task()) {
  243. if (ksig.ka.sa.sa_flags & SA_SIGINFO)
  244. ret = handle_rt_signal32(&ksig, oldset, tsk);
  245. else
  246. ret = handle_signal32(&ksig, oldset, tsk);
  247. } else {
  248. ret = handle_rt_signal64(&ksig, oldset, tsk);
  249. }
  250. set_trap_norestart(tsk->thread.regs);
  251. signal_setup_done(ret, &ksig, test_thread_flag(TIF_SINGLESTEP));
  252. }
  253. void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
  254. {
  255. if (thread_info_flags & _TIF_UPROBE)
  256. uprobe_notify_resume(regs);
  257. if (thread_info_flags & _TIF_PATCH_PENDING)
  258. klp_update_patch_state(current);
  259. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
  260. BUG_ON(regs != current->thread.regs);
  261. do_signal(current);
  262. }
  263. if (thread_info_flags & _TIF_NOTIFY_RESUME)
  264. resume_user_mode_work(regs);
  265. }
  266. static unsigned long get_tm_stackpointer(struct task_struct *tsk)
  267. {
  268. /* When in an active transaction that takes a signal, we need to be
  269. * careful with the stack. It's possible that the stack has moved back
  270. * up after the tbegin. The obvious case here is when the tbegin is
  271. * called inside a function that returns before a tend. In this case,
  272. * the stack is part of the checkpointed transactional memory state.
  273. * If we write over this non transactionally or in suspend, we are in
  274. * trouble because if we get a tm abort, the program counter and stack
  275. * pointer will be back at the tbegin but our in memory stack won't be
  276. * valid anymore.
  277. *
  278. * To avoid this, when taking a signal in an active transaction, we
  279. * need to use the stack pointer from the checkpointed state, rather
  280. * than the speculated state. This ensures that the signal context
  281. * (written tm suspended) will be written below the stack required for
  282. * the rollback. The transaction is aborted because of the treclaim,
  283. * so any memory written between the tbegin and the signal will be
  284. * rolled back anyway.
  285. *
  286. * For signals taken in non-TM or suspended mode, we use the
  287. * normal/non-checkpointed stack pointer.
  288. */
  289. struct pt_regs *regs = tsk->thread.regs;
  290. unsigned long ret = regs->gpr[1];
  291. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  292. BUG_ON(tsk != current);
  293. if (MSR_TM_ACTIVE(regs->msr)) {
  294. preempt_disable();
  295. tm_reclaim_current(TM_CAUSE_SIGNAL);
  296. if (MSR_TM_TRANSACTIONAL(regs->msr))
  297. ret = tsk->thread.ckpt_regs.gpr[1];
  298. /*
  299. * If we treclaim, we must clear the current thread's TM bits
  300. * before re-enabling preemption. Otherwise we might be
  301. * preempted and have the live MSR[TS] changed behind our back
  302. * (tm_recheckpoint_new_task() would recheckpoint). Besides, we
  303. * enter the signal handler in non-transactional state.
  304. */
  305. regs_set_return_msr(regs, regs->msr & ~MSR_TS_MASK);
  306. preempt_enable();
  307. }
  308. #endif
  309. return ret;
  310. }
  311. static const char fm32[] = KERN_INFO "%s[%d]: bad frame in %s: %p nip %08lx lr %08lx\n";
  312. static const char fm64[] = KERN_INFO "%s[%d]: bad frame in %s: %p nip %016lx lr %016lx\n";
  313. void signal_fault(struct task_struct *tsk, struct pt_regs *regs,
  314. const char *where, void __user *ptr)
  315. {
  316. if (show_unhandled_signals)
  317. printk_ratelimited(regs->msr & MSR_64BIT ? fm64 : fm32, tsk->comm,
  318. task_pid_nr(tsk), where, ptr, regs->nip, regs->link);
  319. }