signal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /* signal.c: FRV specific bits of signal handling
  2. *
  3. * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from arch/m68k/kernel/signal.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/kernel.h>
  16. #include <linux/signal.h>
  17. #include <linux/errno.h>
  18. #include <linux/wait.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/unistd.h>
  21. #include <linux/personality.h>
  22. #include <linux/freezer.h>
  23. #include <linux/tracehook.h>
  24. #include <asm/ucontext.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/cacheflush.h>
  27. #define DEBUG_SIG 0
  28. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  29. struct fdpic_func_descriptor {
  30. unsigned long text;
  31. unsigned long GOT;
  32. };
  33. /*
  34. * Atomically swap in the new signal mask, and wait for a signal.
  35. */
  36. asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  37. {
  38. mask &= _BLOCKABLE;
  39. spin_lock_irq(&current->sighand->siglock);
  40. current->saved_sigmask = current->blocked;
  41. siginitset(&current->blocked, mask);
  42. recalc_sigpending();
  43. spin_unlock_irq(&current->sighand->siglock);
  44. current->state = TASK_INTERRUPTIBLE;
  45. schedule();
  46. set_thread_flag(TIF_RESTORE_SIGMASK);
  47. return -ERESTARTNOHAND;
  48. }
  49. asmlinkage int sys_sigaction(int sig,
  50. const struct old_sigaction __user *act,
  51. struct old_sigaction __user *oact)
  52. {
  53. struct k_sigaction new_ka, old_ka;
  54. int ret;
  55. if (act) {
  56. old_sigset_t mask;
  57. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  58. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  59. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  60. return -EFAULT;
  61. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  62. __get_user(mask, &act->sa_mask);
  63. siginitset(&new_ka.sa.sa_mask, mask);
  64. }
  65. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  66. if (!ret && oact) {
  67. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  68. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  69. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  70. return -EFAULT;
  71. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  72. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  73. }
  74. return ret;
  75. }
  76. asmlinkage
  77. int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  78. {
  79. return do_sigaltstack(uss, uoss, __frame->sp);
  80. }
  81. /*
  82. * Do a signal return; undo the signal stack.
  83. */
  84. struct sigframe
  85. {
  86. __sigrestore_t pretcode;
  87. int sig;
  88. struct sigcontext sc;
  89. unsigned long extramask[_NSIG_WORDS-1];
  90. uint32_t retcode[2];
  91. };
  92. struct rt_sigframe
  93. {
  94. __sigrestore_t pretcode;
  95. int sig;
  96. struct siginfo __user *pinfo;
  97. void __user *puc;
  98. struct siginfo info;
  99. struct ucontext uc;
  100. uint32_t retcode[2];
  101. };
  102. static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
  103. {
  104. struct user_context *user = current->thread.user;
  105. unsigned long tbr, psr;
  106. /* Always make any pending restarted system calls return -EINTR */
  107. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  108. tbr = user->i.tbr;
  109. psr = user->i.psr;
  110. if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
  111. goto badframe;
  112. user->i.tbr = tbr;
  113. user->i.psr = psr;
  114. restore_user_regs(user);
  115. user->i.syscallno = -1; /* disable syscall checks */
  116. *_gr8 = user->i.gr[8];
  117. return 0;
  118. badframe:
  119. return 1;
  120. }
  121. asmlinkage int sys_sigreturn(void)
  122. {
  123. struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
  124. sigset_t set;
  125. int gr8;
  126. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  127. goto badframe;
  128. if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
  129. goto badframe;
  130. if (_NSIG_WORDS > 1 &&
  131. __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
  132. goto badframe;
  133. sigdelsetmask(&set, ~_BLOCKABLE);
  134. spin_lock_irq(&current->sighand->siglock);
  135. current->blocked = set;
  136. recalc_sigpending();
  137. spin_unlock_irq(&current->sighand->siglock);
  138. if (restore_sigcontext(&frame->sc, &gr8))
  139. goto badframe;
  140. return gr8;
  141. badframe:
  142. force_sig(SIGSEGV, current);
  143. return 0;
  144. }
  145. asmlinkage int sys_rt_sigreturn(void)
  146. {
  147. struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
  148. sigset_t set;
  149. int gr8;
  150. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  151. goto badframe;
  152. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  153. goto badframe;
  154. sigdelsetmask(&set, ~_BLOCKABLE);
  155. spin_lock_irq(&current->sighand->siglock);
  156. current->blocked = set;
  157. recalc_sigpending();
  158. spin_unlock_irq(&current->sighand->siglock);
  159. if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
  160. goto badframe;
  161. if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
  162. goto badframe;
  163. return gr8;
  164. badframe:
  165. force_sig(SIGSEGV, current);
  166. return 0;
  167. }
  168. /*
  169. * Set up a signal frame
  170. */
  171. static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
  172. {
  173. save_user_regs(current->thread.user);
  174. if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
  175. goto badframe;
  176. /* non-iBCS2 extensions.. */
  177. if (__put_user(mask, &sc->sc_oldmask) < 0)
  178. goto badframe;
  179. return 0;
  180. badframe:
  181. return 1;
  182. }
  183. /*****************************************************************************/
  184. /*
  185. * Determine which stack to use..
  186. */
  187. static inline void __user *get_sigframe(struct k_sigaction *ka,
  188. size_t frame_size)
  189. {
  190. unsigned long sp;
  191. /* Default to using normal stack */
  192. sp = __frame->sp;
  193. /* This is the X/Open sanctioned signal stack switching. */
  194. if (ka->sa.sa_flags & SA_ONSTACK) {
  195. if (! sas_ss_flags(sp))
  196. sp = current->sas_ss_sp + current->sas_ss_size;
  197. }
  198. return (void __user *) ((sp - frame_size) & ~7UL);
  199. } /* end get_sigframe() */
  200. /*****************************************************************************/
  201. /*
  202. *
  203. */
  204. static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
  205. {
  206. struct sigframe __user *frame;
  207. int rsig;
  208. frame = get_sigframe(ka, sizeof(*frame));
  209. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  210. goto give_sigsegv;
  211. rsig = sig;
  212. if (sig < 32 &&
  213. __current_thread_info->exec_domain &&
  214. __current_thread_info->exec_domain->signal_invmap)
  215. rsig = __current_thread_info->exec_domain->signal_invmap[sig];
  216. if (__put_user(rsig, &frame->sig) < 0)
  217. goto give_sigsegv;
  218. if (setup_sigcontext(&frame->sc, set->sig[0]))
  219. goto give_sigsegv;
  220. if (_NSIG_WORDS > 1) {
  221. if (__copy_to_user(frame->extramask, &set->sig[1],
  222. sizeof(frame->extramask)))
  223. goto give_sigsegv;
  224. }
  225. /* Set up to return from userspace. If provided, use a stub
  226. * already in userspace. */
  227. if (ka->sa.sa_flags & SA_RESTORER) {
  228. if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
  229. goto give_sigsegv;
  230. }
  231. else {
  232. /* Set up the following code on the stack:
  233. * setlos #__NR_sigreturn,gr7
  234. * tira gr0,0
  235. */
  236. if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
  237. __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
  238. __put_user(0xc0700000, &frame->retcode[1]))
  239. goto give_sigsegv;
  240. flush_icache_range((unsigned long) frame->retcode,
  241. (unsigned long) (frame->retcode + 2));
  242. }
  243. /* set up registers for signal handler */
  244. __frame->sp = (unsigned long) frame;
  245. __frame->lr = (unsigned long) &frame->retcode;
  246. __frame->gr8 = sig;
  247. if (current->personality & FDPIC_FUNCPTRS) {
  248. struct fdpic_func_descriptor __user *funcptr =
  249. (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
  250. __get_user(__frame->pc, &funcptr->text);
  251. __get_user(__frame->gr15, &funcptr->GOT);
  252. } else {
  253. __frame->pc = (unsigned long) ka->sa.sa_handler;
  254. __frame->gr15 = 0;
  255. }
  256. set_fs(USER_DS);
  257. /* the tracer may want to single-step inside the handler */
  258. if (test_thread_flag(TIF_SINGLESTEP))
  259. ptrace_notify(SIGTRAP);
  260. #if DEBUG_SIG
  261. printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  262. sig, current->comm, current->pid, frame, __frame->pc,
  263. frame->pretcode);
  264. #endif
  265. return 0;
  266. give_sigsegv:
  267. force_sig(SIGSEGV, current);
  268. return -EFAULT;
  269. } /* end setup_frame() */
  270. /*****************************************************************************/
  271. /*
  272. *
  273. */
  274. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  275. sigset_t *set)
  276. {
  277. struct rt_sigframe __user *frame;
  278. int rsig;
  279. frame = get_sigframe(ka, sizeof(*frame));
  280. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  281. goto give_sigsegv;
  282. rsig = sig;
  283. if (sig < 32 &&
  284. __current_thread_info->exec_domain &&
  285. __current_thread_info->exec_domain->signal_invmap)
  286. rsig = __current_thread_info->exec_domain->signal_invmap[sig];
  287. if (__put_user(rsig, &frame->sig) ||
  288. __put_user(&frame->info, &frame->pinfo) ||
  289. __put_user(&frame->uc, &frame->puc))
  290. goto give_sigsegv;
  291. if (copy_siginfo_to_user(&frame->info, info))
  292. goto give_sigsegv;
  293. /* Create the ucontext. */
  294. if (__put_user(0, &frame->uc.uc_flags) ||
  295. __put_user(NULL, &frame->uc.uc_link) ||
  296. __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
  297. __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
  298. __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
  299. goto give_sigsegv;
  300. if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
  301. goto give_sigsegv;
  302. if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
  303. goto give_sigsegv;
  304. /* Set up to return from userspace. If provided, use a stub
  305. * already in userspace. */
  306. if (ka->sa.sa_flags & SA_RESTORER) {
  307. if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
  308. goto give_sigsegv;
  309. }
  310. else {
  311. /* Set up the following code on the stack:
  312. * setlos #__NR_sigreturn,gr7
  313. * tira gr0,0
  314. */
  315. if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
  316. __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
  317. __put_user(0xc0700000, &frame->retcode[1]))
  318. goto give_sigsegv;
  319. flush_icache_range((unsigned long) frame->retcode,
  320. (unsigned long) (frame->retcode + 2));
  321. }
  322. /* Set up registers for signal handler */
  323. __frame->sp = (unsigned long) frame;
  324. __frame->lr = (unsigned long) &frame->retcode;
  325. __frame->gr8 = sig;
  326. __frame->gr9 = (unsigned long) &frame->info;
  327. if (current->personality & FDPIC_FUNCPTRS) {
  328. struct fdpic_func_descriptor __user *funcptr =
  329. (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
  330. __get_user(__frame->pc, &funcptr->text);
  331. __get_user(__frame->gr15, &funcptr->GOT);
  332. } else {
  333. __frame->pc = (unsigned long) ka->sa.sa_handler;
  334. __frame->gr15 = 0;
  335. }
  336. set_fs(USER_DS);
  337. /* the tracer may want to single-step inside the handler */
  338. if (test_thread_flag(TIF_SINGLESTEP))
  339. ptrace_notify(SIGTRAP);
  340. #if DEBUG_SIG
  341. printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  342. sig, current->comm, current->pid, frame, __frame->pc,
  343. frame->pretcode);
  344. #endif
  345. return 0;
  346. give_sigsegv:
  347. force_sig(SIGSEGV, current);
  348. return -EFAULT;
  349. } /* end setup_rt_frame() */
  350. /*****************************************************************************/
  351. /*
  352. * OK, we're invoking a handler
  353. */
  354. static int handle_signal(unsigned long sig, siginfo_t *info,
  355. struct k_sigaction *ka, sigset_t *oldset)
  356. {
  357. int ret;
  358. /* Are we from a system call? */
  359. if (in_syscall(__frame)) {
  360. /* If so, check system call restarting.. */
  361. switch (__frame->gr8) {
  362. case -ERESTART_RESTARTBLOCK:
  363. case -ERESTARTNOHAND:
  364. __frame->gr8 = -EINTR;
  365. break;
  366. case -ERESTARTSYS:
  367. if (!(ka->sa.sa_flags & SA_RESTART)) {
  368. __frame->gr8 = -EINTR;
  369. break;
  370. }
  371. /* fallthrough */
  372. case -ERESTARTNOINTR:
  373. __frame->gr8 = __frame->orig_gr8;
  374. __frame->pc -= 4;
  375. }
  376. }
  377. /* Set up the stack frame */
  378. if (ka->sa.sa_flags & SA_SIGINFO)
  379. ret = setup_rt_frame(sig, ka, info, oldset);
  380. else
  381. ret = setup_frame(sig, ka, oldset);
  382. if (ret == 0) {
  383. spin_lock_irq(&current->sighand->siglock);
  384. sigorsets(&current->blocked, &current->blocked,
  385. &ka->sa.sa_mask);
  386. if (!(ka->sa.sa_flags & SA_NODEFER))
  387. sigaddset(&current->blocked, sig);
  388. recalc_sigpending();
  389. spin_unlock_irq(&current->sighand->siglock);
  390. }
  391. return ret;
  392. } /* end handle_signal() */
  393. /*****************************************************************************/
  394. /*
  395. * Note that 'init' is a special process: it doesn't get signals it doesn't
  396. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  397. * mistake.
  398. */
  399. static void do_signal(void)
  400. {
  401. struct k_sigaction ka;
  402. siginfo_t info;
  403. sigset_t *oldset;
  404. int signr;
  405. /*
  406. * We want the common case to go fast, which
  407. * is why we may in certain cases get here from
  408. * kernel mode. Just return without doing anything
  409. * if so.
  410. */
  411. if (!user_mode(__frame))
  412. return;
  413. if (try_to_freeze())
  414. goto no_signal;
  415. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  416. oldset = &current->saved_sigmask;
  417. else
  418. oldset = &current->blocked;
  419. signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
  420. if (signr > 0) {
  421. if (handle_signal(signr, &info, &ka, oldset) == 0) {
  422. /* a signal was successfully delivered; the saved
  423. * sigmask will have been stored in the signal frame,
  424. * and will be restored by sigreturn, so we can simply
  425. * clear the TIF_RESTORE_SIGMASK flag */
  426. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  427. clear_thread_flag(TIF_RESTORE_SIGMASK);
  428. tracehook_signal_handler(signr, &info, &ka, __frame,
  429. test_thread_flag(TIF_SINGLESTEP));
  430. }
  431. return;
  432. }
  433. no_signal:
  434. /* Did we come from a system call? */
  435. if (__frame->syscallno != -1) {
  436. /* Restart the system call - no handlers present */
  437. switch (__frame->gr8) {
  438. case -ERESTARTNOHAND:
  439. case -ERESTARTSYS:
  440. case -ERESTARTNOINTR:
  441. __frame->gr8 = __frame->orig_gr8;
  442. __frame->pc -= 4;
  443. break;
  444. case -ERESTART_RESTARTBLOCK:
  445. __frame->gr8 = __NR_restart_syscall;
  446. __frame->pc -= 4;
  447. break;
  448. }
  449. }
  450. /* if there's no signal to deliver, we just put the saved sigmask
  451. * back */
  452. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  453. clear_thread_flag(TIF_RESTORE_SIGMASK);
  454. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  455. }
  456. } /* end do_signal() */
  457. /*****************************************************************************/
  458. /*
  459. * notification of userspace execution resumption
  460. * - triggered by the TIF_WORK_MASK flags
  461. */
  462. asmlinkage void do_notify_resume(__u32 thread_info_flags)
  463. {
  464. /* pending single-step? */
  465. if (thread_info_flags & _TIF_SINGLESTEP)
  466. clear_thread_flag(TIF_SINGLESTEP);
  467. /* deal with pending signal delivery */
  468. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
  469. do_signal();
  470. /* deal with notification on about to resume userspace execution */
  471. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  472. clear_thread_flag(TIF_NOTIFY_RESUME);
  473. tracehook_notify_resume(__frame);
  474. if (current->replacement_session_keyring)
  475. key_replace_session_keyring();
  476. }
  477. } /* end do_notify_resume() */