signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Architecture-specific signal handling support.
  4. *
  5. * Copyright (C) 1999-2004 Hewlett-Packard Co
  6. * David Mosberger-Tang <davidm@hpl.hp.com>
  7. *
  8. * Derived from i386 and Alpha versions.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/tracehook.h>
  15. #include <linux/sched.h>
  16. #include <linux/signal.h>
  17. #include <linux/smp.h>
  18. #include <linux/stddef.h>
  19. #include <linux/tty.h>
  20. #include <linux/binfmts.h>
  21. #include <linux/unistd.h>
  22. #include <linux/wait.h>
  23. #include <asm/intrinsics.h>
  24. #include <linux/uaccess.h>
  25. #include <asm/rse.h>
  26. #include <asm/sigcontext.h>
  27. #include "sigframe.h"
  28. #define DEBUG_SIG 0
  29. #define STACK_ALIGN 16 /* minimal alignment for stack pointer */
  30. #if _NSIG_WORDS > 1
  31. # define PUT_SIGSET(k,u) __copy_to_user((u)->sig, (k)->sig, sizeof(sigset_t))
  32. # define GET_SIGSET(k,u) __copy_from_user((k)->sig, (u)->sig, sizeof(sigset_t))
  33. #else
  34. # define PUT_SIGSET(k,u) __put_user((k)->sig[0], &(u)->sig[0])
  35. # define GET_SIGSET(k,u) __get_user((k)->sig[0], &(u)->sig[0])
  36. #endif
  37. static long
  38. restore_sigcontext (struct sigcontext __user *sc, struct sigscratch *scr)
  39. {
  40. unsigned long ip, flags, nat, um, cfm, rsc;
  41. long err;
  42. /* Always make any pending restarted system calls return -EINTR */
  43. current->restart_block.fn = do_no_restart_syscall;
  44. /* restore scratch that always needs gets updated during signal delivery: */
  45. err = __get_user(flags, &sc->sc_flags);
  46. err |= __get_user(nat, &sc->sc_nat);
  47. err |= __get_user(ip, &sc->sc_ip); /* instruction pointer */
  48. err |= __get_user(cfm, &sc->sc_cfm);
  49. err |= __get_user(um, &sc->sc_um); /* user mask */
  50. err |= __get_user(rsc, &sc->sc_ar_rsc);
  51. err |= __get_user(scr->pt.ar_unat, &sc->sc_ar_unat);
  52. err |= __get_user(scr->pt.ar_fpsr, &sc->sc_ar_fpsr);
  53. err |= __get_user(scr->pt.ar_pfs, &sc->sc_ar_pfs);
  54. err |= __get_user(scr->pt.pr, &sc->sc_pr); /* predicates */
  55. err |= __get_user(scr->pt.b0, &sc->sc_br[0]); /* b0 (rp) */
  56. err |= __get_user(scr->pt.b6, &sc->sc_br[6]); /* b6 */
  57. err |= __copy_from_user(&scr->pt.r1, &sc->sc_gr[1], 8); /* r1 */
  58. err |= __copy_from_user(&scr->pt.r8, &sc->sc_gr[8], 4*8); /* r8-r11 */
  59. err |= __copy_from_user(&scr->pt.r12, &sc->sc_gr[12], 2*8); /* r12-r13 */
  60. err |= __copy_from_user(&scr->pt.r15, &sc->sc_gr[15], 8); /* r15 */
  61. scr->pt.cr_ifs = cfm | (1UL << 63);
  62. scr->pt.ar_rsc = rsc | (3 << 2); /* force PL3 */
  63. /* establish new instruction pointer: */
  64. scr->pt.cr_iip = ip & ~0x3UL;
  65. ia64_psr(&scr->pt)->ri = ip & 0x3;
  66. scr->pt.cr_ipsr = (scr->pt.cr_ipsr & ~IA64_PSR_UM) | (um & IA64_PSR_UM);
  67. scr->scratch_unat = ia64_put_scratch_nat_bits(&scr->pt, nat);
  68. if (!(flags & IA64_SC_FLAG_IN_SYSCALL)) {
  69. /* Restore most scratch-state only when not in syscall. */
  70. err |= __get_user(scr->pt.ar_ccv, &sc->sc_ar_ccv); /* ar.ccv */
  71. err |= __get_user(scr->pt.b7, &sc->sc_br[7]); /* b7 */
  72. err |= __get_user(scr->pt.r14, &sc->sc_gr[14]); /* r14 */
  73. err |= __copy_from_user(&scr->pt.ar_csd, &sc->sc_ar25, 2*8); /* ar.csd & ar.ssd */
  74. err |= __copy_from_user(&scr->pt.r2, &sc->sc_gr[2], 2*8); /* r2-r3 */
  75. err |= __copy_from_user(&scr->pt.r16, &sc->sc_gr[16], 16*8); /* r16-r31 */
  76. }
  77. if ((flags & IA64_SC_FLAG_FPH_VALID) != 0) {
  78. struct ia64_psr *psr = ia64_psr(&scr->pt);
  79. err |= __copy_from_user(current->thread.fph, &sc->sc_fr[32], 96*16);
  80. psr->mfh = 0; /* drop signal handler's fph contents... */
  81. preempt_disable();
  82. if (psr->dfh)
  83. ia64_drop_fpu(current);
  84. else {
  85. /* We already own the local fph, otherwise psr->dfh wouldn't be 0. */
  86. __ia64_load_fpu(current->thread.fph);
  87. ia64_set_local_fpu_owner(current);
  88. }
  89. preempt_enable();
  90. }
  91. return err;
  92. }
  93. long
  94. ia64_rt_sigreturn (struct sigscratch *scr)
  95. {
  96. extern char ia64_strace_leave_kernel, ia64_leave_kernel;
  97. struct sigcontext __user *sc;
  98. sigset_t set;
  99. long retval;
  100. sc = &((struct sigframe __user *) (scr->pt.r12 + 16))->sc;
  101. /*
  102. * When we return to the previously executing context, r8 and r10 have already
  103. * been setup the way we want them. Indeed, if the signal wasn't delivered while
  104. * in a system call, we must not touch r8 or r10 as otherwise user-level state
  105. * could be corrupted.
  106. */
  107. retval = (long) &ia64_leave_kernel;
  108. if (test_thread_flag(TIF_SYSCALL_TRACE)
  109. || test_thread_flag(TIF_SYSCALL_AUDIT))
  110. /*
  111. * strace expects to be notified after sigreturn returns even though the
  112. * context to which we return may not be in the middle of a syscall.
  113. * Thus, the return-value that strace displays for sigreturn is
  114. * meaningless.
  115. */
  116. retval = (long) &ia64_strace_leave_kernel;
  117. if (!access_ok(VERIFY_READ, sc, sizeof(*sc)))
  118. goto give_sigsegv;
  119. if (GET_SIGSET(&set, &sc->sc_mask))
  120. goto give_sigsegv;
  121. set_current_blocked(&set);
  122. if (restore_sigcontext(sc, scr))
  123. goto give_sigsegv;
  124. #if DEBUG_SIG
  125. printk("SIG return (%s:%d): sp=%lx ip=%lx\n",
  126. current->comm, current->pid, scr->pt.r12, scr->pt.cr_iip);
  127. #endif
  128. if (restore_altstack(&sc->sc_stack))
  129. goto give_sigsegv;
  130. return retval;
  131. give_sigsegv:
  132. force_sig(SIGSEGV, current);
  133. return retval;
  134. }
  135. /*
  136. * This does just the minimum required setup of sigcontext.
  137. * Specifically, it only installs data that is either not knowable at
  138. * the user-level or that gets modified before execution in the
  139. * trampoline starts. Everything else is done at the user-level.
  140. */
  141. static long
  142. setup_sigcontext (struct sigcontext __user *sc, sigset_t *mask, struct sigscratch *scr)
  143. {
  144. unsigned long flags = 0, ifs, cfm, nat;
  145. long err = 0;
  146. ifs = scr->pt.cr_ifs;
  147. if (on_sig_stack((unsigned long) sc))
  148. flags |= IA64_SC_FLAG_ONSTACK;
  149. if ((ifs & (1UL << 63)) == 0)
  150. /* if cr_ifs doesn't have the valid bit set, we got here through a syscall */
  151. flags |= IA64_SC_FLAG_IN_SYSCALL;
  152. cfm = ifs & ((1UL << 38) - 1);
  153. ia64_flush_fph(current);
  154. if ((current->thread.flags & IA64_THREAD_FPH_VALID)) {
  155. flags |= IA64_SC_FLAG_FPH_VALID;
  156. err = __copy_to_user(&sc->sc_fr[32], current->thread.fph, 96*16);
  157. }
  158. nat = ia64_get_scratch_nat_bits(&scr->pt, scr->scratch_unat);
  159. err |= __put_user(flags, &sc->sc_flags);
  160. err |= __put_user(nat, &sc->sc_nat);
  161. err |= PUT_SIGSET(mask, &sc->sc_mask);
  162. err |= __put_user(cfm, &sc->sc_cfm);
  163. err |= __put_user(scr->pt.cr_ipsr & IA64_PSR_UM, &sc->sc_um);
  164. err |= __put_user(scr->pt.ar_rsc, &sc->sc_ar_rsc);
  165. err |= __put_user(scr->pt.ar_unat, &sc->sc_ar_unat); /* ar.unat */
  166. err |= __put_user(scr->pt.ar_fpsr, &sc->sc_ar_fpsr); /* ar.fpsr */
  167. err |= __put_user(scr->pt.ar_pfs, &sc->sc_ar_pfs);
  168. err |= __put_user(scr->pt.pr, &sc->sc_pr); /* predicates */
  169. err |= __put_user(scr->pt.b0, &sc->sc_br[0]); /* b0 (rp) */
  170. err |= __put_user(scr->pt.b6, &sc->sc_br[6]); /* b6 */
  171. err |= __copy_to_user(&sc->sc_gr[1], &scr->pt.r1, 8); /* r1 */
  172. err |= __copy_to_user(&sc->sc_gr[8], &scr->pt.r8, 4*8); /* r8-r11 */
  173. err |= __copy_to_user(&sc->sc_gr[12], &scr->pt.r12, 2*8); /* r12-r13 */
  174. err |= __copy_to_user(&sc->sc_gr[15], &scr->pt.r15, 8); /* r15 */
  175. err |= __put_user(scr->pt.cr_iip + ia64_psr(&scr->pt)->ri, &sc->sc_ip);
  176. if (!(flags & IA64_SC_FLAG_IN_SYSCALL)) {
  177. /* Copy scratch regs to sigcontext if the signal didn't interrupt a syscall. */
  178. err |= __put_user(scr->pt.ar_ccv, &sc->sc_ar_ccv); /* ar.ccv */
  179. err |= __put_user(scr->pt.b7, &sc->sc_br[7]); /* b7 */
  180. err |= __put_user(scr->pt.r14, &sc->sc_gr[14]); /* r14 */
  181. err |= __copy_to_user(&sc->sc_ar25, &scr->pt.ar_csd, 2*8); /* ar.csd & ar.ssd */
  182. err |= __copy_to_user(&sc->sc_gr[2], &scr->pt.r2, 2*8); /* r2-r3 */
  183. err |= __copy_to_user(&sc->sc_gr[16], &scr->pt.r16, 16*8); /* r16-r31 */
  184. }
  185. return err;
  186. }
  187. /*
  188. * Check whether the register-backing store is already on the signal stack.
  189. */
  190. static inline int
  191. rbs_on_sig_stack (unsigned long bsp)
  192. {
  193. return (bsp - current->sas_ss_sp < current->sas_ss_size);
  194. }
  195. static long
  196. setup_frame(struct ksignal *ksig, sigset_t *set, struct sigscratch *scr)
  197. {
  198. extern char __kernel_sigtramp[];
  199. unsigned long tramp_addr, new_rbs = 0, new_sp;
  200. struct sigframe __user *frame;
  201. long err;
  202. new_sp = scr->pt.r12;
  203. tramp_addr = (unsigned long) __kernel_sigtramp;
  204. if (ksig->ka.sa.sa_flags & SA_ONSTACK) {
  205. int onstack = sas_ss_flags(new_sp);
  206. if (onstack == 0) {
  207. new_sp = current->sas_ss_sp + current->sas_ss_size;
  208. /*
  209. * We need to check for the register stack being on the
  210. * signal stack separately, because it's switched
  211. * separately (memory stack is switched in the kernel,
  212. * register stack is switched in the signal trampoline).
  213. */
  214. if (!rbs_on_sig_stack(scr->pt.ar_bspstore))
  215. new_rbs = ALIGN(current->sas_ss_sp,
  216. sizeof(long));
  217. } else if (onstack == SS_ONSTACK) {
  218. unsigned long check_sp;
  219. /*
  220. * If we are on the alternate signal stack and would
  221. * overflow it, don't. Return an always-bogus address
  222. * instead so we will die with SIGSEGV.
  223. */
  224. check_sp = (new_sp - sizeof(*frame)) & -STACK_ALIGN;
  225. if (!likely(on_sig_stack(check_sp))) {
  226. force_sigsegv(ksig->sig, current);
  227. return 1;
  228. }
  229. }
  230. }
  231. frame = (void __user *) ((new_sp - sizeof(*frame)) & -STACK_ALIGN);
  232. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) {
  233. force_sigsegv(ksig->sig, current);
  234. return 1;
  235. }
  236. err = __put_user(ksig->sig, &frame->arg0);
  237. err |= __put_user(&frame->info, &frame->arg1);
  238. err |= __put_user(&frame->sc, &frame->arg2);
  239. err |= __put_user(new_rbs, &frame->sc.sc_rbs_base);
  240. err |= __put_user(0, &frame->sc.sc_loadrs); /* initialize to zero */
  241. err |= __put_user(ksig->ka.sa.sa_handler, &frame->handler);
  242. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  243. err |= __save_altstack(&frame->sc.sc_stack, scr->pt.r12);
  244. err |= setup_sigcontext(&frame->sc, set, scr);
  245. if (unlikely(err)) {
  246. force_sigsegv(ksig->sig, current);
  247. return 1;
  248. }
  249. scr->pt.r12 = (unsigned long) frame - 16; /* new stack pointer */
  250. scr->pt.ar_fpsr = FPSR_DEFAULT; /* reset fpsr for signal handler */
  251. scr->pt.cr_iip = tramp_addr;
  252. ia64_psr(&scr->pt)->ri = 0; /* start executing in first slot */
  253. ia64_psr(&scr->pt)->be = 0; /* force little-endian byte-order */
  254. /*
  255. * Force the interruption function mask to zero. This has no effect when a
  256. * system-call got interrupted by a signal (since, in that case, scr->pt_cr_ifs is
  257. * ignored), but it has the desirable effect of making it possible to deliver a
  258. * signal with an incomplete register frame (which happens when a mandatory RSE
  259. * load faults). Furthermore, it has no negative effect on the getting the user's
  260. * dirty partition preserved, because that's governed by scr->pt.loadrs.
  261. */
  262. scr->pt.cr_ifs = (1UL << 63);
  263. /*
  264. * Note: this affects only the NaT bits of the scratch regs (the ones saved in
  265. * pt_regs), which is exactly what we want.
  266. */
  267. scr->scratch_unat = 0; /* ensure NaT bits of r12 is clear */
  268. #if DEBUG_SIG
  269. printk("SIG deliver (%s:%d): sig=%d sp=%lx ip=%lx handler=%p\n",
  270. current->comm, current->pid, ksig->sig, scr->pt.r12, frame->sc.sc_ip, frame->handler);
  271. #endif
  272. return 0;
  273. }
  274. static long
  275. handle_signal (struct ksignal *ksig, struct sigscratch *scr)
  276. {
  277. int ret = setup_frame(ksig, sigmask_to_save(), scr);
  278. if (!ret)
  279. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  280. return ret;
  281. }
  282. /*
  283. * Note that `init' is a special process: it doesn't get signals it doesn't want to
  284. * handle. Thus you cannot kill init even with a SIGKILL even by mistake.
  285. */
  286. void
  287. ia64_do_signal (struct sigscratch *scr, long in_syscall)
  288. {
  289. long restart = in_syscall;
  290. long errno = scr->pt.r8;
  291. struct ksignal ksig;
  292. /*
  293. * This only loops in the rare cases of handle_signal() failing, in which case we
  294. * need to push through a forced SIGSEGV.
  295. */
  296. while (1) {
  297. get_signal(&ksig);
  298. /*
  299. * get_signal_to_deliver() may have run a debugger (via notify_parent())
  300. * and the debugger may have modified the state (e.g., to arrange for an
  301. * inferior call), thus it's important to check for restarting _after_
  302. * get_signal_to_deliver().
  303. */
  304. if ((long) scr->pt.r10 != -1)
  305. /*
  306. * A system calls has to be restarted only if one of the error codes
  307. * ERESTARTNOHAND, ERESTARTSYS, or ERESTARTNOINTR is returned. If r10
  308. * isn't -1 then r8 doesn't hold an error code and we don't need to
  309. * restart the syscall, so we can clear the "restart" flag here.
  310. */
  311. restart = 0;
  312. if (ksig.sig <= 0)
  313. break;
  314. if (unlikely(restart)) {
  315. switch (errno) {
  316. case ERESTART_RESTARTBLOCK:
  317. case ERESTARTNOHAND:
  318. scr->pt.r8 = EINTR;
  319. /* note: scr->pt.r10 is already -1 */
  320. break;
  321. case ERESTARTSYS:
  322. if ((ksig.ka.sa.sa_flags & SA_RESTART) == 0) {
  323. scr->pt.r8 = EINTR;
  324. /* note: scr->pt.r10 is already -1 */
  325. break;
  326. }
  327. case ERESTARTNOINTR:
  328. ia64_decrement_ip(&scr->pt);
  329. restart = 0; /* don't restart twice if handle_signal() fails... */
  330. }
  331. }
  332. /*
  333. * Whee! Actually deliver the signal. If the delivery failed, we need to
  334. * continue to iterate in this loop so we can deliver the SIGSEGV...
  335. */
  336. if (handle_signal(&ksig, scr))
  337. return;
  338. }
  339. /* Did we come from a system call? */
  340. if (restart) {
  341. /* Restart the system call - no handlers present */
  342. if (errno == ERESTARTNOHAND || errno == ERESTARTSYS || errno == ERESTARTNOINTR
  343. || errno == ERESTART_RESTARTBLOCK)
  344. {
  345. /*
  346. * Note: the syscall number is in r15 which is saved in
  347. * pt_regs so all we need to do here is adjust ip so that
  348. * the "break" instruction gets re-executed.
  349. */
  350. ia64_decrement_ip(&scr->pt);
  351. if (errno == ERESTART_RESTARTBLOCK)
  352. scr->pt.r15 = __NR_restart_syscall;
  353. }
  354. }
  355. /* if there's no signal to deliver, we just put the saved sigmask
  356. * back */
  357. restore_saved_sigmask();
  358. }