signal.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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/config.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/sched.h>
  15. #include <linux/signal.h>
  16. #include <linux/smp.h>
  17. #include <linux/smp_lock.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/ia32.h>
  24. #include <asm/intrinsics.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/rse.h>
  27. #include <asm/sigcontext.h>
  28. #include "sigframe.h"
  29. #define DEBUG_SIG 0
  30. #define STACK_ALIGN 16 /* minimal alignment for stack pointer */
  31. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  32. #if _NSIG_WORDS > 1
  33. # define PUT_SIGSET(k,u) __copy_to_user((u)->sig, (k)->sig, sizeof(sigset_t))
  34. # define GET_SIGSET(k,u) __copy_from_user((k)->sig, (u)->sig, sizeof(sigset_t))
  35. #else
  36. # define PUT_SIGSET(k,u) __put_user((k)->sig[0], &(u)->sig[0])
  37. # define GET_SIGSET(k,u) __get_user((k)->sig[0], &(u)->sig[0])
  38. #endif
  39. long
  40. ia64_rt_sigsuspend (sigset_t __user *uset, size_t sigsetsize, struct sigscratch *scr)
  41. {
  42. sigset_t oldset, set;
  43. /* XXX: Don't preclude handling different sized sigset_t's. */
  44. if (sigsetsize != sizeof(sigset_t))
  45. return -EINVAL;
  46. if (!access_ok(VERIFY_READ, uset, sigsetsize))
  47. return -EFAULT;
  48. if (GET_SIGSET(&set, uset))
  49. return -EFAULT;
  50. sigdelsetmask(&set, ~_BLOCKABLE);
  51. spin_lock_irq(&current->sighand->siglock);
  52. {
  53. oldset = current->blocked;
  54. current->blocked = set;
  55. recalc_sigpending();
  56. }
  57. spin_unlock_irq(&current->sighand->siglock);
  58. /*
  59. * The return below usually returns to the signal handler. We need to
  60. * pre-set the correct error code here to ensure that the right values
  61. * get saved in sigcontext by ia64_do_signal.
  62. */
  63. scr->pt.r8 = EINTR;
  64. scr->pt.r10 = -1;
  65. while (1) {
  66. current->state = TASK_INTERRUPTIBLE;
  67. schedule();
  68. if (ia64_do_signal(&oldset, scr, 1))
  69. return -EINTR;
  70. }
  71. }
  72. asmlinkage long
  73. sys_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, long arg2,
  74. long arg3, long arg4, long arg5, long arg6, long arg7,
  75. struct pt_regs regs)
  76. {
  77. return do_sigaltstack(uss, uoss, regs.r12);
  78. }
  79. static long
  80. restore_sigcontext (struct sigcontext __user *sc, struct sigscratch *scr)
  81. {
  82. unsigned long ip, flags, nat, um, cfm, rsc;
  83. long err;
  84. /* Always make any pending restarted system calls return -EINTR */
  85. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  86. /* restore scratch that always needs gets updated during signal delivery: */
  87. err = __get_user(flags, &sc->sc_flags);
  88. err |= __get_user(nat, &sc->sc_nat);
  89. err |= __get_user(ip, &sc->sc_ip); /* instruction pointer */
  90. err |= __get_user(cfm, &sc->sc_cfm);
  91. err |= __get_user(um, &sc->sc_um); /* user mask */
  92. err |= __get_user(rsc, &sc->sc_ar_rsc);
  93. err |= __get_user(scr->pt.ar_unat, &sc->sc_ar_unat);
  94. err |= __get_user(scr->pt.ar_fpsr, &sc->sc_ar_fpsr);
  95. err |= __get_user(scr->pt.ar_pfs, &sc->sc_ar_pfs);
  96. err |= __get_user(scr->pt.pr, &sc->sc_pr); /* predicates */
  97. err |= __get_user(scr->pt.b0, &sc->sc_br[0]); /* b0 (rp) */
  98. err |= __get_user(scr->pt.b6, &sc->sc_br[6]); /* b6 */
  99. err |= __copy_from_user(&scr->pt.r1, &sc->sc_gr[1], 8); /* r1 */
  100. err |= __copy_from_user(&scr->pt.r8, &sc->sc_gr[8], 4*8); /* r8-r11 */
  101. err |= __copy_from_user(&scr->pt.r12, &sc->sc_gr[12], 2*8); /* r12-r13 */
  102. err |= __copy_from_user(&scr->pt.r15, &sc->sc_gr[15], 8); /* r15 */
  103. scr->pt.cr_ifs = cfm | (1UL << 63);
  104. scr->pt.ar_rsc = rsc | (3 << 2); /* force PL3 */
  105. /* establish new instruction pointer: */
  106. scr->pt.cr_iip = ip & ~0x3UL;
  107. ia64_psr(&scr->pt)->ri = ip & 0x3;
  108. scr->pt.cr_ipsr = (scr->pt.cr_ipsr & ~IA64_PSR_UM) | (um & IA64_PSR_UM);
  109. scr->scratch_unat = ia64_put_scratch_nat_bits(&scr->pt, nat);
  110. if (!(flags & IA64_SC_FLAG_IN_SYSCALL)) {
  111. /* Restore most scratch-state only when not in syscall. */
  112. err |= __get_user(scr->pt.ar_ccv, &sc->sc_ar_ccv); /* ar.ccv */
  113. err |= __get_user(scr->pt.b7, &sc->sc_br[7]); /* b7 */
  114. err |= __get_user(scr->pt.r14, &sc->sc_gr[14]); /* r14 */
  115. err |= __copy_from_user(&scr->pt.ar_csd, &sc->sc_ar25, 2*8); /* ar.csd & ar.ssd */
  116. err |= __copy_from_user(&scr->pt.r2, &sc->sc_gr[2], 2*8); /* r2-r3 */
  117. err |= __copy_from_user(&scr->pt.r16, &sc->sc_gr[16], 16*8); /* r16-r31 */
  118. }
  119. if ((flags & IA64_SC_FLAG_FPH_VALID) != 0) {
  120. struct ia64_psr *psr = ia64_psr(&scr->pt);
  121. __copy_from_user(current->thread.fph, &sc->sc_fr[32], 96*16);
  122. psr->mfh = 0; /* drop signal handler's fph contents... */
  123. preempt_disable();
  124. if (psr->dfh)
  125. ia64_drop_fpu(current);
  126. else {
  127. /* We already own the local fph, otherwise psr->dfh wouldn't be 0. */
  128. __ia64_load_fpu(current->thread.fph);
  129. ia64_set_local_fpu_owner(current);
  130. }
  131. preempt_enable();
  132. }
  133. return err;
  134. }
  135. int
  136. copy_siginfo_to_user (siginfo_t __user *to, siginfo_t *from)
  137. {
  138. if (!access_ok(VERIFY_WRITE, to, sizeof(siginfo_t)))
  139. return -EFAULT;
  140. if (from->si_code < 0) {
  141. if (__copy_to_user(to, from, sizeof(siginfo_t)))
  142. return -EFAULT;
  143. return 0;
  144. } else {
  145. int err;
  146. /*
  147. * If you change siginfo_t structure, please be sure this code is fixed
  148. * accordingly. It should never copy any pad contained in the structure
  149. * to avoid security leaks, but must copy the generic 3 ints plus the
  150. * relevant union member.
  151. */
  152. err = __put_user(from->si_signo, &to->si_signo);
  153. err |= __put_user(from->si_errno, &to->si_errno);
  154. err |= __put_user((short)from->si_code, &to->si_code);
  155. switch (from->si_code >> 16) {
  156. case __SI_FAULT >> 16:
  157. err |= __put_user(from->si_flags, &to->si_flags);
  158. err |= __put_user(from->si_isr, &to->si_isr);
  159. case __SI_POLL >> 16:
  160. err |= __put_user(from->si_addr, &to->si_addr);
  161. err |= __put_user(from->si_imm, &to->si_imm);
  162. break;
  163. case __SI_TIMER >> 16:
  164. err |= __put_user(from->si_tid, &to->si_tid);
  165. err |= __put_user(from->si_overrun, &to->si_overrun);
  166. err |= __put_user(from->si_ptr, &to->si_ptr);
  167. break;
  168. case __SI_RT >> 16: /* Not generated by the kernel as of now. */
  169. case __SI_MESGQ >> 16:
  170. err |= __put_user(from->si_uid, &to->si_uid);
  171. err |= __put_user(from->si_pid, &to->si_pid);
  172. err |= __put_user(from->si_ptr, &to->si_ptr);
  173. break;
  174. case __SI_CHLD >> 16:
  175. err |= __put_user(from->si_utime, &to->si_utime);
  176. err |= __put_user(from->si_stime, &to->si_stime);
  177. err |= __put_user(from->si_status, &to->si_status);
  178. default:
  179. err |= __put_user(from->si_uid, &to->si_uid);
  180. err |= __put_user(from->si_pid, &to->si_pid);
  181. break;
  182. }
  183. return err;
  184. }
  185. }
  186. long
  187. ia64_rt_sigreturn (struct sigscratch *scr)
  188. {
  189. extern char ia64_strace_leave_kernel, ia64_leave_kernel;
  190. struct sigcontext __user *sc;
  191. struct siginfo si;
  192. sigset_t set;
  193. long retval;
  194. sc = &((struct sigframe __user *) (scr->pt.r12 + 16))->sc;
  195. /*
  196. * When we return to the previously executing context, r8 and r10 have already
  197. * been setup the way we want them. Indeed, if the signal wasn't delivered while
  198. * in a system call, we must not touch r8 or r10 as otherwise user-level state
  199. * could be corrupted.
  200. */
  201. retval = (long) &ia64_leave_kernel;
  202. if (test_thread_flag(TIF_SYSCALL_TRACE)
  203. || test_thread_flag(TIF_SYSCALL_AUDIT))
  204. /*
  205. * strace expects to be notified after sigreturn returns even though the
  206. * context to which we return may not be in the middle of a syscall.
  207. * Thus, the return-value that strace displays for sigreturn is
  208. * meaningless.
  209. */
  210. retval = (long) &ia64_strace_leave_kernel;
  211. if (!access_ok(VERIFY_READ, sc, sizeof(*sc)))
  212. goto give_sigsegv;
  213. if (GET_SIGSET(&set, &sc->sc_mask))
  214. goto give_sigsegv;
  215. sigdelsetmask(&set, ~_BLOCKABLE);
  216. spin_lock_irq(&current->sighand->siglock);
  217. {
  218. current->blocked = set;
  219. recalc_sigpending();
  220. }
  221. spin_unlock_irq(&current->sighand->siglock);
  222. if (restore_sigcontext(sc, scr))
  223. goto give_sigsegv;
  224. #if DEBUG_SIG
  225. printk("SIG return (%s:%d): sp=%lx ip=%lx\n",
  226. current->comm, current->pid, scr->pt.r12, scr->pt.cr_iip);
  227. #endif
  228. /*
  229. * It is more difficult to avoid calling this function than to
  230. * call it and ignore errors.
  231. */
  232. do_sigaltstack(&sc->sc_stack, NULL, scr->pt.r12);
  233. return retval;
  234. give_sigsegv:
  235. si.si_signo = SIGSEGV;
  236. si.si_errno = 0;
  237. si.si_code = SI_KERNEL;
  238. si.si_pid = current->pid;
  239. si.si_uid = current->uid;
  240. si.si_addr = sc;
  241. force_sig_info(SIGSEGV, &si, current);
  242. return retval;
  243. }
  244. /*
  245. * This does just the minimum required setup of sigcontext.
  246. * Specifically, it only installs data that is either not knowable at
  247. * the user-level or that gets modified before execution in the
  248. * trampoline starts. Everything else is done at the user-level.
  249. */
  250. static long
  251. setup_sigcontext (struct sigcontext __user *sc, sigset_t *mask, struct sigscratch *scr)
  252. {
  253. unsigned long flags = 0, ifs, cfm, nat;
  254. long err;
  255. ifs = scr->pt.cr_ifs;
  256. if (on_sig_stack((unsigned long) sc))
  257. flags |= IA64_SC_FLAG_ONSTACK;
  258. if ((ifs & (1UL << 63)) == 0)
  259. /* if cr_ifs doesn't have the valid bit set, we got here through a syscall */
  260. flags |= IA64_SC_FLAG_IN_SYSCALL;
  261. cfm = ifs & ((1UL << 38) - 1);
  262. ia64_flush_fph(current);
  263. if ((current->thread.flags & IA64_THREAD_FPH_VALID)) {
  264. flags |= IA64_SC_FLAG_FPH_VALID;
  265. __copy_to_user(&sc->sc_fr[32], current->thread.fph, 96*16);
  266. }
  267. nat = ia64_get_scratch_nat_bits(&scr->pt, scr->scratch_unat);
  268. err = __put_user(flags, &sc->sc_flags);
  269. err |= __put_user(nat, &sc->sc_nat);
  270. err |= PUT_SIGSET(mask, &sc->sc_mask);
  271. err |= __put_user(cfm, &sc->sc_cfm);
  272. err |= __put_user(scr->pt.cr_ipsr & IA64_PSR_UM, &sc->sc_um);
  273. err |= __put_user(scr->pt.ar_rsc, &sc->sc_ar_rsc);
  274. err |= __put_user(scr->pt.ar_unat, &sc->sc_ar_unat); /* ar.unat */
  275. err |= __put_user(scr->pt.ar_fpsr, &sc->sc_ar_fpsr); /* ar.fpsr */
  276. err |= __put_user(scr->pt.ar_pfs, &sc->sc_ar_pfs);
  277. err |= __put_user(scr->pt.pr, &sc->sc_pr); /* predicates */
  278. err |= __put_user(scr->pt.b0, &sc->sc_br[0]); /* b0 (rp) */
  279. err |= __put_user(scr->pt.b6, &sc->sc_br[6]); /* b6 */
  280. err |= __copy_to_user(&sc->sc_gr[1], &scr->pt.r1, 8); /* r1 */
  281. err |= __copy_to_user(&sc->sc_gr[8], &scr->pt.r8, 4*8); /* r8-r11 */
  282. err |= __copy_to_user(&sc->sc_gr[12], &scr->pt.r12, 2*8); /* r12-r13 */
  283. err |= __copy_to_user(&sc->sc_gr[15], &scr->pt.r15, 8); /* r15 */
  284. err |= __put_user(scr->pt.cr_iip + ia64_psr(&scr->pt)->ri, &sc->sc_ip);
  285. if (flags & IA64_SC_FLAG_IN_SYSCALL) {
  286. /* Clear scratch registers if the signal interrupted a system call. */
  287. err |= __put_user(0, &sc->sc_ar_ccv); /* ar.ccv */
  288. err |= __put_user(0, &sc->sc_br[7]); /* b7 */
  289. err |= __put_user(0, &sc->sc_gr[14]); /* r14 */
  290. err |= __clear_user(&sc->sc_ar25, 2*8); /* ar.csd & ar.ssd */
  291. err |= __clear_user(&sc->sc_gr[2], 2*8); /* r2-r3 */
  292. err |= __clear_user(&sc->sc_gr[16], 16*8); /* r16-r31 */
  293. } else {
  294. /* Copy scratch regs to sigcontext if the signal didn't interrupt a syscall. */
  295. err |= __put_user(scr->pt.ar_ccv, &sc->sc_ar_ccv); /* ar.ccv */
  296. err |= __put_user(scr->pt.b7, &sc->sc_br[7]); /* b7 */
  297. err |= __put_user(scr->pt.r14, &sc->sc_gr[14]); /* r14 */
  298. err |= __copy_to_user(&sc->sc_ar25, &scr->pt.ar_csd, 2*8); /* ar.csd & ar.ssd */
  299. err |= __copy_to_user(&sc->sc_gr[2], &scr->pt.r2, 2*8); /* r2-r3 */
  300. err |= __copy_to_user(&sc->sc_gr[16], &scr->pt.r16, 16*8); /* r16-r31 */
  301. }
  302. return err;
  303. }
  304. /*
  305. * Check whether the register-backing store is already on the signal stack.
  306. */
  307. static inline int
  308. rbs_on_sig_stack (unsigned long bsp)
  309. {
  310. return (bsp - current->sas_ss_sp < current->sas_ss_size);
  311. }
  312. static long
  313. force_sigsegv_info (int sig, void __user *addr)
  314. {
  315. unsigned long flags;
  316. struct siginfo si;
  317. if (sig == SIGSEGV) {
  318. /*
  319. * Acquiring siglock around the sa_handler-update is almost
  320. * certainly overkill, but this isn't a
  321. * performance-critical path and I'd rather play it safe
  322. * here than having to debug a nasty race if and when
  323. * something changes in kernel/signal.c that would make it
  324. * no longer safe to modify sa_handler without holding the
  325. * lock.
  326. */
  327. spin_lock_irqsave(&current->sighand->siglock, flags);
  328. current->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  329. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  330. }
  331. si.si_signo = SIGSEGV;
  332. si.si_errno = 0;
  333. si.si_code = SI_KERNEL;
  334. si.si_pid = current->pid;
  335. si.si_uid = current->uid;
  336. si.si_addr = addr;
  337. force_sig_info(SIGSEGV, &si, current);
  338. return 0;
  339. }
  340. static long
  341. setup_frame (int sig, struct k_sigaction *ka, siginfo_t *info, sigset_t *set,
  342. struct sigscratch *scr)
  343. {
  344. extern char __kernel_sigtramp[];
  345. unsigned long tramp_addr, new_rbs = 0;
  346. struct sigframe __user *frame;
  347. long err;
  348. frame = (void __user *) scr->pt.r12;
  349. tramp_addr = (unsigned long) __kernel_sigtramp;
  350. if ((ka->sa.sa_flags & SA_ONSTACK) && sas_ss_flags((unsigned long) frame) == 0) {
  351. frame = (void __user *) ((current->sas_ss_sp + current->sas_ss_size)
  352. & ~(STACK_ALIGN - 1));
  353. /*
  354. * We need to check for the register stack being on the signal stack
  355. * separately, because it's switched separately (memory stack is switched
  356. * in the kernel, register stack is switched in the signal trampoline).
  357. */
  358. if (!rbs_on_sig_stack(scr->pt.ar_bspstore))
  359. new_rbs = (current->sas_ss_sp + sizeof(long) - 1) & ~(sizeof(long) - 1);
  360. }
  361. frame = (void __user *) frame - ((sizeof(*frame) + STACK_ALIGN - 1) & ~(STACK_ALIGN - 1));
  362. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  363. return force_sigsegv_info(sig, frame);
  364. err = __put_user(sig, &frame->arg0);
  365. err |= __put_user(&frame->info, &frame->arg1);
  366. err |= __put_user(&frame->sc, &frame->arg2);
  367. err |= __put_user(new_rbs, &frame->sc.sc_rbs_base);
  368. err |= __put_user(0, &frame->sc.sc_loadrs); /* initialize to zero */
  369. err |= __put_user(ka->sa.sa_handler, &frame->handler);
  370. err |= copy_siginfo_to_user(&frame->info, info);
  371. err |= __put_user(current->sas_ss_sp, &frame->sc.sc_stack.ss_sp);
  372. err |= __put_user(current->sas_ss_size, &frame->sc.sc_stack.ss_size);
  373. err |= __put_user(sas_ss_flags(scr->pt.r12), &frame->sc.sc_stack.ss_flags);
  374. err |= setup_sigcontext(&frame->sc, set, scr);
  375. if (unlikely(err))
  376. return force_sigsegv_info(sig, frame);
  377. scr->pt.r12 = (unsigned long) frame - 16; /* new stack pointer */
  378. scr->pt.ar_fpsr = FPSR_DEFAULT; /* reset fpsr for signal handler */
  379. scr->pt.cr_iip = tramp_addr;
  380. ia64_psr(&scr->pt)->ri = 0; /* start executing in first slot */
  381. ia64_psr(&scr->pt)->be = 0; /* force little-endian byte-order */
  382. /*
  383. * Force the interruption function mask to zero. This has no effect when a
  384. * system-call got interrupted by a signal (since, in that case, scr->pt_cr_ifs is
  385. * ignored), but it has the desirable effect of making it possible to deliver a
  386. * signal with an incomplete register frame (which happens when a mandatory RSE
  387. * load faults). Furthermore, it has no negative effect on the getting the user's
  388. * dirty partition preserved, because that's governed by scr->pt.loadrs.
  389. */
  390. scr->pt.cr_ifs = (1UL << 63);
  391. /*
  392. * Note: this affects only the NaT bits of the scratch regs (the ones saved in
  393. * pt_regs), which is exactly what we want.
  394. */
  395. scr->scratch_unat = 0; /* ensure NaT bits of r12 is clear */
  396. #if DEBUG_SIG
  397. printk("SIG deliver (%s:%d): sig=%d sp=%lx ip=%lx handler=%p\n",
  398. current->comm, current->pid, sig, scr->pt.r12, frame->sc.sc_ip, frame->handler);
  399. #endif
  400. return 1;
  401. }
  402. static long
  403. handle_signal (unsigned long sig, struct k_sigaction *ka, siginfo_t *info, sigset_t *oldset,
  404. struct sigscratch *scr)
  405. {
  406. if (IS_IA32_PROCESS(&scr->pt)) {
  407. /* send signal to IA-32 process */
  408. if (!ia32_setup_frame1(sig, ka, info, oldset, &scr->pt))
  409. return 0;
  410. } else
  411. /* send signal to IA-64 process */
  412. if (!setup_frame(sig, ka, info, oldset, scr))
  413. return 0;
  414. spin_lock_irq(&current->sighand->siglock);
  415. sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
  416. if (!(ka->sa.sa_flags & SA_NODEFER))
  417. sigaddset(&current->blocked, sig);
  418. recalc_sigpending();
  419. spin_unlock_irq(&current->sighand->siglock);
  420. return 1;
  421. }
  422. /*
  423. * Note that `init' is a special process: it doesn't get signals it doesn't want to
  424. * handle. Thus you cannot kill init even with a SIGKILL even by mistake.
  425. */
  426. long
  427. ia64_do_signal (sigset_t *oldset, struct sigscratch *scr, long in_syscall)
  428. {
  429. struct k_sigaction ka;
  430. siginfo_t info;
  431. long restart = in_syscall;
  432. long errno = scr->pt.r8;
  433. # define ERR_CODE(c) (IS_IA32_PROCESS(&scr->pt) ? -(c) : (c))
  434. /*
  435. * In the ia64_leave_kernel code path, we want the common case to go fast, which
  436. * is why we may in certain cases get here from kernel mode. Just return without
  437. * doing anything if so.
  438. */
  439. if (!user_mode(&scr->pt))
  440. return 0;
  441. if (!oldset)
  442. oldset = &current->blocked;
  443. /*
  444. * This only loops in the rare cases of handle_signal() failing, in which case we
  445. * need to push through a forced SIGSEGV.
  446. */
  447. while (1) {
  448. int signr = get_signal_to_deliver(&info, &ka, &scr->pt, NULL);
  449. /*
  450. * get_signal_to_deliver() may have run a debugger (via notify_parent())
  451. * and the debugger may have modified the state (e.g., to arrange for an
  452. * inferior call), thus it's important to check for restarting _after_
  453. * get_signal_to_deliver().
  454. */
  455. if (IS_IA32_PROCESS(&scr->pt)) {
  456. if (in_syscall) {
  457. if (errno >= 0)
  458. restart = 0;
  459. else
  460. errno = -errno;
  461. }
  462. } else if ((long) scr->pt.r10 != -1)
  463. /*
  464. * A system calls has to be restarted only if one of the error codes
  465. * ERESTARTNOHAND, ERESTARTSYS, or ERESTARTNOINTR is returned. If r10
  466. * isn't -1 then r8 doesn't hold an error code and we don't need to
  467. * restart the syscall, so we can clear the "restart" flag here.
  468. */
  469. restart = 0;
  470. if (signr <= 0)
  471. break;
  472. if (unlikely(restart)) {
  473. switch (errno) {
  474. case ERESTART_RESTARTBLOCK:
  475. case ERESTARTNOHAND:
  476. scr->pt.r8 = ERR_CODE(EINTR);
  477. /* note: scr->pt.r10 is already -1 */
  478. break;
  479. case ERESTARTSYS:
  480. if ((ka.sa.sa_flags & SA_RESTART) == 0) {
  481. scr->pt.r8 = ERR_CODE(EINTR);
  482. /* note: scr->pt.r10 is already -1 */
  483. break;
  484. }
  485. case ERESTARTNOINTR:
  486. if (IS_IA32_PROCESS(&scr->pt)) {
  487. scr->pt.r8 = scr->pt.r1;
  488. scr->pt.cr_iip -= 2;
  489. } else
  490. ia64_decrement_ip(&scr->pt);
  491. restart = 0; /* don't restart twice if handle_signal() fails... */
  492. }
  493. }
  494. /*
  495. * Whee! Actually deliver the signal. If the delivery failed, we need to
  496. * continue to iterate in this loop so we can deliver the SIGSEGV...
  497. */
  498. if (handle_signal(signr, &ka, &info, oldset, scr))
  499. return 1;
  500. }
  501. /* Did we come from a system call? */
  502. if (restart) {
  503. /* Restart the system call - no handlers present */
  504. if (errno == ERESTARTNOHAND || errno == ERESTARTSYS || errno == ERESTARTNOINTR
  505. || errno == ERESTART_RESTARTBLOCK)
  506. {
  507. if (IS_IA32_PROCESS(&scr->pt)) {
  508. scr->pt.r8 = scr->pt.r1;
  509. scr->pt.cr_iip -= 2;
  510. if (errno == ERESTART_RESTARTBLOCK)
  511. scr->pt.r8 = 0; /* x86 version of __NR_restart_syscall */
  512. } else {
  513. /*
  514. * Note: the syscall number is in r15 which is saved in
  515. * pt_regs so all we need to do here is adjust ip so that
  516. * the "break" instruction gets re-executed.
  517. */
  518. ia64_decrement_ip(&scr->pt);
  519. if (errno == ERESTART_RESTARTBLOCK)
  520. scr->pt.r15 = __NR_restart_syscall;
  521. }
  522. }
  523. }
  524. return 0;
  525. }
  526. /* Set a delayed signal that was detected in MCA/INIT/NMI/PMI context where it
  527. * could not be delivered. It is important that the target process is not
  528. * allowed to do any more work in user space. Possible cases for the target
  529. * process:
  530. *
  531. * - It is sleeping and will wake up soon. Store the data in the current task,
  532. * the signal will be sent when the current task returns from the next
  533. * interrupt.
  534. *
  535. * - It is running in user context. Store the data in the current task, the
  536. * signal will be sent when the current task returns from the next interrupt.
  537. *
  538. * - It is running in kernel context on this or another cpu and will return to
  539. * user context. Store the data in the target task, the signal will be sent
  540. * to itself when the target task returns to user space.
  541. *
  542. * - It is running in kernel context on this cpu and will sleep before
  543. * returning to user context. Because this is also the current task, the
  544. * signal will not get delivered and the task could sleep indefinitely.
  545. * Store the data in the idle task for this cpu, the signal will be sent
  546. * after the idle task processes its next interrupt.
  547. *
  548. * To cover all cases, store the data in the target task, the current task and
  549. * the idle task on this cpu. Whatever happens, the signal will be delivered
  550. * to the target task before it can do any useful user space work. Multiple
  551. * deliveries have no unwanted side effects.
  552. *
  553. * Note: This code is executed in MCA/INIT/NMI/PMI context, with interrupts
  554. * disabled. It must not take any locks nor use kernel structures or services
  555. * that require locks.
  556. */
  557. /* To ensure that we get the right pid, check its start time. To avoid extra
  558. * include files in thread_info.h, convert the task start_time to unsigned long,
  559. * giving us a cycle time of > 580 years.
  560. */
  561. static inline unsigned long
  562. start_time_ul(const struct task_struct *t)
  563. {
  564. return t->start_time.tv_sec * NSEC_PER_SEC + t->start_time.tv_nsec;
  565. }
  566. void
  567. set_sigdelayed(pid_t pid, int signo, int code, void __user *addr)
  568. {
  569. struct task_struct *t;
  570. unsigned long start_time = 0;
  571. int i;
  572. for (i = 1; i <= 3; ++i) {
  573. switch (i) {
  574. case 1:
  575. t = find_task_by_pid(pid);
  576. if (t)
  577. start_time = start_time_ul(t);
  578. break;
  579. case 2:
  580. t = current;
  581. break;
  582. default:
  583. t = idle_task(smp_processor_id());
  584. break;
  585. }
  586. if (!t)
  587. return;
  588. t->thread_info->sigdelayed.signo = signo;
  589. t->thread_info->sigdelayed.code = code;
  590. t->thread_info->sigdelayed.addr = addr;
  591. t->thread_info->sigdelayed.start_time = start_time;
  592. t->thread_info->sigdelayed.pid = pid;
  593. wmb();
  594. set_tsk_thread_flag(t, TIF_SIGDELAYED);
  595. }
  596. }
  597. /* Called from entry.S when it detects TIF_SIGDELAYED, a delayed signal that
  598. * was detected in MCA/INIT/NMI/PMI context where it could not be delivered.
  599. */
  600. void
  601. do_sigdelayed(void)
  602. {
  603. struct siginfo siginfo;
  604. pid_t pid;
  605. struct task_struct *t;
  606. clear_thread_flag(TIF_SIGDELAYED);
  607. memset(&siginfo, 0, sizeof(siginfo));
  608. siginfo.si_signo = current_thread_info()->sigdelayed.signo;
  609. siginfo.si_code = current_thread_info()->sigdelayed.code;
  610. siginfo.si_addr = current_thread_info()->sigdelayed.addr;
  611. pid = current_thread_info()->sigdelayed.pid;
  612. t = find_task_by_pid(pid);
  613. if (!t)
  614. return;
  615. if (current_thread_info()->sigdelayed.start_time != start_time_ul(t))
  616. return;
  617. force_sig_info(siginfo.si_signo, &siginfo, t);
  618. }