signal.c 16 KB

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