signal.c 9.9 KB

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