signal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/smp_lock.h>
  16. #include <linux/kernel.h>
  17. #include <linux/signal.h>
  18. #include <linux/errno.h>
  19. #include <linux/wait.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/unistd.h>
  22. #include <linux/personality.h>
  23. #include <linux/suspend.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. void (*pretcode)(void);
  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. void (*pretcode)(void);
  95. int sig;
  96. struct siginfo *pinfo;
  97. void *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. tbr = user->i.tbr;
  107. psr = user->i.psr;
  108. if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
  109. goto badframe;
  110. user->i.tbr = tbr;
  111. user->i.psr = psr;
  112. restore_user_regs(user);
  113. user->i.syscallno = -1; /* disable syscall checks */
  114. *_gr8 = user->i.gr[8];
  115. return 0;
  116. badframe:
  117. return 1;
  118. }
  119. asmlinkage int sys_sigreturn(void)
  120. {
  121. struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
  122. sigset_t set;
  123. int gr8;
  124. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  125. goto badframe;
  126. if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
  127. goto badframe;
  128. if (_NSIG_WORDS > 1 &&
  129. __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
  130. goto badframe;
  131. sigdelsetmask(&set, ~_BLOCKABLE);
  132. spin_lock_irq(&current->sighand->siglock);
  133. current->blocked = set;
  134. recalc_sigpending();
  135. spin_unlock_irq(&current->sighand->siglock);
  136. if (restore_sigcontext(&frame->sc, &gr8))
  137. goto badframe;
  138. return gr8;
  139. badframe:
  140. force_sig(SIGSEGV, current);
  141. return 0;
  142. }
  143. asmlinkage int sys_rt_sigreturn(void)
  144. {
  145. struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
  146. sigset_t set;
  147. int gr8;
  148. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  149. goto badframe;
  150. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  151. goto badframe;
  152. sigdelsetmask(&set, ~_BLOCKABLE);
  153. spin_lock_irq(&current->sighand->siglock);
  154. current->blocked = set;
  155. recalc_sigpending();
  156. spin_unlock_irq(&current->sighand->siglock);
  157. if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
  158. goto badframe;
  159. if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
  160. goto badframe;
  161. return gr8;
  162. badframe:
  163. force_sig(SIGSEGV, current);
  164. return 0;
  165. }
  166. /*
  167. * Set up a signal frame
  168. */
  169. static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
  170. {
  171. save_user_regs(current->thread.user);
  172. if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
  173. goto badframe;
  174. /* non-iBCS2 extensions.. */
  175. if (__put_user(mask, &sc->sc_oldmask) < 0)
  176. goto badframe;
  177. return 0;
  178. badframe:
  179. return 1;
  180. }
  181. /*****************************************************************************/
  182. /*
  183. * Determine which stack to use..
  184. */
  185. static inline void __user *get_sigframe(struct k_sigaction *ka,
  186. size_t frame_size)
  187. {
  188. unsigned long sp;
  189. /* Default to using normal stack */
  190. sp = __frame->sp;
  191. /* This is the X/Open sanctioned signal stack switching. */
  192. if (ka->sa.sa_flags & SA_ONSTACK) {
  193. if (! on_sig_stack(sp))
  194. sp = current->sas_ss_sp + current->sas_ss_size;
  195. }
  196. return (void __user *) ((sp - frame_size) & ~7UL);
  197. } /* end get_sigframe() */
  198. /*****************************************************************************/
  199. /*
  200. *
  201. */
  202. static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
  203. {
  204. struct sigframe __user *frame;
  205. int rsig;
  206. frame = get_sigframe(ka, sizeof(*frame));
  207. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  208. goto give_sigsegv;
  209. rsig = sig;
  210. if (sig < 32 &&
  211. __current_thread_info->exec_domain &&
  212. __current_thread_info->exec_domain->signal_invmap)
  213. rsig = __current_thread_info->exec_domain->signal_invmap[sig];
  214. if (__put_user(rsig, &frame->sig) < 0)
  215. goto give_sigsegv;
  216. if (setup_sigcontext(&frame->sc, set->sig[0]))
  217. goto give_sigsegv;
  218. if (_NSIG_WORDS > 1) {
  219. if (__copy_to_user(frame->extramask, &set->sig[1],
  220. sizeof(frame->extramask)))
  221. goto give_sigsegv;
  222. }
  223. /* Set up to return from userspace. If provided, use a stub
  224. * already in userspace. */
  225. if (ka->sa.sa_flags & SA_RESTORER) {
  226. if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
  227. goto give_sigsegv;
  228. }
  229. else {
  230. /* Set up the following code on the stack:
  231. * setlos #__NR_sigreturn,gr7
  232. * tira gr0,0
  233. */
  234. if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
  235. __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
  236. __put_user(0xc0700000, &frame->retcode[1]))
  237. goto give_sigsegv;
  238. flush_icache_range((unsigned long) frame->retcode,
  239. (unsigned long) (frame->retcode + 2));
  240. }
  241. /* set up registers for signal handler */
  242. __frame->sp = (unsigned long) frame;
  243. __frame->lr = (unsigned long) &frame->retcode;
  244. __frame->gr8 = sig;
  245. if (get_personality & FDPIC_FUNCPTRS) {
  246. struct fdpic_func_descriptor __user *funcptr =
  247. (struct fdpic_func_descriptor *) ka->sa.sa_handler;
  248. __get_user(__frame->pc, &funcptr->text);
  249. __get_user(__frame->gr15, &funcptr->GOT);
  250. } else {
  251. __frame->pc = (unsigned long) ka->sa.sa_handler;
  252. __frame->gr15 = 0;
  253. }
  254. set_fs(USER_DS);
  255. /* the tracer may want to single-step inside the handler */
  256. if (test_thread_flag(TIF_SINGLESTEP))
  257. ptrace_notify(SIGTRAP);
  258. #if DEBUG_SIG
  259. printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  260. sig, current->comm, current->pid, frame, __frame->pc,
  261. frame->pretcode);
  262. #endif
  263. return 0;
  264. give_sigsegv:
  265. force_sig(SIGSEGV, current);
  266. return -EFAULT;
  267. } /* end setup_frame() */
  268. /*****************************************************************************/
  269. /*
  270. *
  271. */
  272. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  273. sigset_t *set)
  274. {
  275. struct rt_sigframe __user *frame;
  276. int rsig;
  277. frame = get_sigframe(ka, sizeof(*frame));
  278. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  279. goto give_sigsegv;
  280. rsig = sig;
  281. if (sig < 32 &&
  282. __current_thread_info->exec_domain &&
  283. __current_thread_info->exec_domain->signal_invmap)
  284. rsig = __current_thread_info->exec_domain->signal_invmap[sig];
  285. if (__put_user(rsig, &frame->sig) ||
  286. __put_user(&frame->info, &frame->pinfo) ||
  287. __put_user(&frame->uc, &frame->puc))
  288. goto give_sigsegv;
  289. if (copy_siginfo_to_user(&frame->info, info))
  290. goto give_sigsegv;
  291. /* Create the ucontext. */
  292. if (__put_user(0, &frame->uc.uc_flags) ||
  293. __put_user(0, &frame->uc.uc_link) ||
  294. __put_user((void*)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
  295. __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
  296. __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
  297. goto give_sigsegv;
  298. if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
  299. goto give_sigsegv;
  300. if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
  301. goto give_sigsegv;
  302. /* Set up to return from userspace. If provided, use a stub
  303. * already in userspace. */
  304. if (ka->sa.sa_flags & SA_RESTORER) {
  305. if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
  306. goto give_sigsegv;
  307. }
  308. else {
  309. /* Set up the following code on the stack:
  310. * setlos #__NR_sigreturn,gr7
  311. * tira gr0,0
  312. */
  313. if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
  314. __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
  315. __put_user(0xc0700000, &frame->retcode[1]))
  316. goto give_sigsegv;
  317. flush_icache_range((unsigned long) frame->retcode,
  318. (unsigned long) (frame->retcode + 2));
  319. }
  320. /* Set up registers for signal handler */
  321. __frame->sp = (unsigned long) frame;
  322. __frame->lr = (unsigned long) &frame->retcode;
  323. __frame->gr8 = sig;
  324. __frame->gr9 = (unsigned long) &frame->info;
  325. if (get_personality & FDPIC_FUNCPTRS) {
  326. struct fdpic_func_descriptor *funcptr =
  327. (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
  328. __get_user(__frame->pc, &funcptr->text);
  329. __get_user(__frame->gr15, &funcptr->GOT);
  330. } else {
  331. __frame->pc = (unsigned long) ka->sa.sa_handler;
  332. __frame->gr15 = 0;
  333. }
  334. set_fs(USER_DS);
  335. /* the tracer may want to single-step inside the handler */
  336. if (test_thread_flag(TIF_SINGLESTEP))
  337. ptrace_notify(SIGTRAP);
  338. #if DEBUG_SIG
  339. printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  340. sig, current->comm, current->pid, frame, __frame->pc,
  341. frame->pretcode);
  342. #endif
  343. return 0;
  344. give_sigsegv:
  345. force_sig(SIGSEGV, current);
  346. return -EFAULT;
  347. } /* end setup_rt_frame() */
  348. /*****************************************************************************/
  349. /*
  350. * OK, we're invoking a handler
  351. */
  352. static int handle_signal(unsigned long sig, siginfo_t *info,
  353. struct k_sigaction *ka, sigset_t *oldset)
  354. {
  355. int ret;
  356. /* Are we from a system call? */
  357. if (in_syscall(__frame)) {
  358. /* If so, check system call restarting.. */
  359. switch (__frame->gr8) {
  360. case -ERESTART_RESTARTBLOCK:
  361. case -ERESTARTNOHAND:
  362. __frame->gr8 = -EINTR;
  363. break;
  364. case -ERESTARTSYS:
  365. if (!(ka->sa.sa_flags & SA_RESTART)) {
  366. __frame->gr8 = -EINTR;
  367. break;
  368. }
  369. /* fallthrough */
  370. case -ERESTARTNOINTR:
  371. __frame->gr8 = __frame->orig_gr8;
  372. __frame->pc -= 4;
  373. }
  374. }
  375. /* Set up the stack frame */
  376. if (ka->sa.sa_flags & SA_SIGINFO)
  377. ret = setup_rt_frame(sig, ka, info, oldset);
  378. else
  379. ret = setup_frame(sig, ka, oldset);
  380. if (ret == 0) {
  381. spin_lock_irq(&current->sighand->siglock);
  382. sigorsets(&current->blocked, &current->blocked,
  383. &ka->sa.sa_mask);
  384. if (!(ka->sa.sa_flags & SA_NODEFER))
  385. sigaddset(&current->blocked, sig);
  386. recalc_sigpending();
  387. spin_unlock_irq(&current->sighand->siglock);
  388. }
  389. return ret;
  390. } /* end handle_signal() */
  391. /*****************************************************************************/
  392. /*
  393. * Note that 'init' is a special process: it doesn't get signals it doesn't
  394. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  395. * mistake.
  396. */
  397. static void do_signal(void)
  398. {
  399. struct k_sigaction ka;
  400. siginfo_t info;
  401. sigset_t *oldset;
  402. int signr;
  403. /*
  404. * We want the common case to go fast, which
  405. * is why we may in certain cases get here from
  406. * kernel mode. Just return without doing anything
  407. * if so.
  408. */
  409. if (!user_mode(__frame))
  410. return;
  411. if (try_to_freeze())
  412. goto no_signal;
  413. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  414. oldset = &current->saved_sigmask;
  415. else
  416. oldset = &current->blocked;
  417. signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
  418. if (signr > 0) {
  419. if (handle_signal(signr, &info, &ka, oldset) == 0) {
  420. /* a signal was successfully delivered; the saved
  421. * sigmask will have been stored in the signal frame,
  422. * and will be restored by sigreturn, so we can simply
  423. * clear the TIF_RESTORE_SIGMASK flag */
  424. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  425. clear_thread_flag(TIF_RESTORE_SIGMASK);
  426. }
  427. return;
  428. }
  429. no_signal:
  430. /* Did we come from a system call? */
  431. if (__frame->syscallno >= 0) {
  432. /* Restart the system call - no handlers present */
  433. switch (__frame->gr8) {
  434. case -ERESTARTNOHAND:
  435. case -ERESTARTSYS:
  436. case -ERESTARTNOINTR:
  437. __frame->gr8 = __frame->orig_gr8;
  438. __frame->pc -= 4;
  439. break;
  440. case -ERESTART_RESTARTBLOCK:
  441. __frame->gr8 = __NR_restart_syscall;
  442. __frame->pc -= 4;
  443. break;
  444. }
  445. }
  446. /* if there's no signal to deliver, we just put the saved sigmask
  447. * back */
  448. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  449. clear_thread_flag(TIF_RESTORE_SIGMASK);
  450. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  451. }
  452. } /* end do_signal() */
  453. /*****************************************************************************/
  454. /*
  455. * notification of userspace execution resumption
  456. * - triggered by the TIF_WORK_MASK flags
  457. */
  458. asmlinkage void do_notify_resume(__u32 thread_info_flags)
  459. {
  460. /* pending single-step? */
  461. if (thread_info_flags & _TIF_SINGLESTEP)
  462. clear_thread_flag(TIF_SINGLESTEP);
  463. /* deal with pending signal delivery */
  464. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
  465. do_signal();
  466. } /* end do_notify_resume() */