signal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. static int do_signal(sigset_t *oldset);
  34. /*
  35. * Atomically swap in the new signal mask, and wait for a signal.
  36. */
  37. asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  38. {
  39. sigset_t saveset;
  40. mask &= _BLOCKABLE;
  41. spin_lock_irq(&current->sighand->siglock);
  42. saveset = current->blocked;
  43. siginitset(&current->blocked, mask);
  44. recalc_sigpending();
  45. spin_unlock_irq(&current->sighand->siglock);
  46. __frame->gr8 = -EINTR;
  47. while (1) {
  48. current->state = TASK_INTERRUPTIBLE;
  49. schedule();
  50. if (do_signal(&saveset))
  51. /* return the signal number as the return value of this function
  52. * - this is an utterly evil hack. syscalls should not invoke do_signal()
  53. * as entry.S sets regs->gr8 to the return value of the system call
  54. * - we can't just use sigpending() as we'd have to discard SIG_IGN signals
  55. * and call waitpid() if SIGCHLD needed discarding
  56. * - this only works on the i386 because it passes arguments to the signal
  57. * handler on the stack, and the return value in EAX is effectively
  58. * discarded
  59. */
  60. return __frame->gr8;
  61. }
  62. }
  63. asmlinkage int sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
  64. {
  65. sigset_t saveset, newset;
  66. /* XXX: Don't preclude handling different sized sigset_t's. */
  67. if (sigsetsize != sizeof(sigset_t))
  68. return -EINVAL;
  69. if (copy_from_user(&newset, unewset, sizeof(newset)))
  70. return -EFAULT;
  71. sigdelsetmask(&newset, ~_BLOCKABLE);
  72. spin_lock_irq(&current->sighand->siglock);
  73. saveset = current->blocked;
  74. current->blocked = newset;
  75. recalc_sigpending();
  76. spin_unlock_irq(&current->sighand->siglock);
  77. __frame->gr8 = -EINTR;
  78. while (1) {
  79. current->state = TASK_INTERRUPTIBLE;
  80. schedule();
  81. if (do_signal(&saveset))
  82. /* return the signal number as the return value of this function
  83. * - this is an utterly evil hack. syscalls should not invoke do_signal()
  84. * as entry.S sets regs->gr8 to the return value of the system call
  85. * - we can't just use sigpending() as we'd have to discard SIG_IGN signals
  86. * and call waitpid() if SIGCHLD needed discarding
  87. * - this only works on the i386 because it passes arguments to the signal
  88. * handler on the stack, and the return value in EAX is effectively
  89. * discarded
  90. */
  91. return __frame->gr8;
  92. }
  93. }
  94. asmlinkage int sys_sigaction(int sig,
  95. const struct old_sigaction __user *act,
  96. struct old_sigaction __user *oact)
  97. {
  98. struct k_sigaction new_ka, old_ka;
  99. int ret;
  100. if (act) {
  101. old_sigset_t mask;
  102. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  103. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  104. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  105. return -EFAULT;
  106. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  107. __get_user(mask, &act->sa_mask);
  108. siginitset(&new_ka.sa.sa_mask, mask);
  109. }
  110. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  111. if (!ret && oact) {
  112. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  113. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  114. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  115. return -EFAULT;
  116. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  117. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  118. }
  119. return ret;
  120. }
  121. asmlinkage
  122. int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  123. {
  124. return do_sigaltstack(uss, uoss, __frame->sp);
  125. }
  126. /*
  127. * Do a signal return; undo the signal stack.
  128. */
  129. struct sigframe
  130. {
  131. void (*pretcode)(void);
  132. int sig;
  133. struct sigcontext sc;
  134. unsigned long extramask[_NSIG_WORDS-1];
  135. uint32_t retcode[2];
  136. };
  137. struct rt_sigframe
  138. {
  139. void (*pretcode)(void);
  140. int sig;
  141. struct siginfo *pinfo;
  142. void *puc;
  143. struct siginfo info;
  144. struct ucontext uc;
  145. uint32_t retcode[2];
  146. };
  147. static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
  148. {
  149. struct user_context *user = current->thread.user;
  150. unsigned long tbr, psr;
  151. tbr = user->i.tbr;
  152. psr = user->i.psr;
  153. if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
  154. goto badframe;
  155. user->i.tbr = tbr;
  156. user->i.psr = psr;
  157. restore_user_regs(user);
  158. user->i.syscallno = -1; /* disable syscall checks */
  159. *_gr8 = user->i.gr[8];
  160. return 0;
  161. badframe:
  162. return 1;
  163. }
  164. asmlinkage int sys_sigreturn(void)
  165. {
  166. struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
  167. sigset_t set;
  168. int gr8;
  169. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  170. goto badframe;
  171. if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
  172. goto badframe;
  173. if (_NSIG_WORDS > 1 &&
  174. __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
  175. goto badframe;
  176. sigdelsetmask(&set, ~_BLOCKABLE);
  177. spin_lock_irq(&current->sighand->siglock);
  178. current->blocked = set;
  179. recalc_sigpending();
  180. spin_unlock_irq(&current->sighand->siglock);
  181. if (restore_sigcontext(&frame->sc, &gr8))
  182. goto badframe;
  183. return gr8;
  184. badframe:
  185. force_sig(SIGSEGV, current);
  186. return 0;
  187. }
  188. asmlinkage int sys_rt_sigreturn(void)
  189. {
  190. struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
  191. sigset_t set;
  192. int gr8;
  193. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  194. goto badframe;
  195. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  196. goto badframe;
  197. sigdelsetmask(&set, ~_BLOCKABLE);
  198. spin_lock_irq(&current->sighand->siglock);
  199. current->blocked = set;
  200. recalc_sigpending();
  201. spin_unlock_irq(&current->sighand->siglock);
  202. if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
  203. goto badframe;
  204. if (do_sigaltstack(&frame->uc.uc_stack, NULL, __frame->sp) == -EFAULT)
  205. goto badframe;
  206. return gr8;
  207. badframe:
  208. force_sig(SIGSEGV, current);
  209. return 0;
  210. }
  211. /*
  212. * Set up a signal frame
  213. */
  214. static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
  215. {
  216. save_user_regs(current->thread.user);
  217. if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
  218. goto badframe;
  219. /* non-iBCS2 extensions.. */
  220. if (__put_user(mask, &sc->sc_oldmask) < 0)
  221. goto badframe;
  222. return 0;
  223. badframe:
  224. return 1;
  225. }
  226. /*****************************************************************************/
  227. /*
  228. * Determine which stack to use..
  229. */
  230. static inline void __user *get_sigframe(struct k_sigaction *ka,
  231. size_t frame_size)
  232. {
  233. unsigned long sp;
  234. /* Default to using normal stack */
  235. sp = __frame->sp;
  236. /* This is the X/Open sanctioned signal stack switching. */
  237. if (ka->sa.sa_flags & SA_ONSTACK) {
  238. if (! on_sig_stack(sp))
  239. sp = current->sas_ss_sp + current->sas_ss_size;
  240. }
  241. return (void __user *) ((sp - frame_size) & ~7UL);
  242. } /* end get_sigframe() */
  243. /*****************************************************************************/
  244. /*
  245. *
  246. */
  247. static int setup_frame(int sig, struct k_sigaction *ka, sigset_t *set)
  248. {
  249. struct sigframe __user *frame;
  250. int rsig;
  251. frame = get_sigframe(ka, sizeof(*frame));
  252. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  253. goto give_sigsegv;
  254. rsig = sig;
  255. if (sig < 32 &&
  256. __current_thread_info->exec_domain &&
  257. __current_thread_info->exec_domain->signal_invmap)
  258. rsig = __current_thread_info->exec_domain->signal_invmap[sig];
  259. if (__put_user(rsig, &frame->sig) < 0)
  260. goto give_sigsegv;
  261. if (setup_sigcontext(&frame->sc, set->sig[0]))
  262. goto give_sigsegv;
  263. if (_NSIG_WORDS > 1) {
  264. if (__copy_to_user(frame->extramask, &set->sig[1],
  265. sizeof(frame->extramask)))
  266. goto give_sigsegv;
  267. }
  268. /* Set up to return from userspace. If provided, use a stub
  269. * already in userspace. */
  270. if (ka->sa.sa_flags & SA_RESTORER) {
  271. if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
  272. goto give_sigsegv;
  273. }
  274. else {
  275. /* Set up the following code on the stack:
  276. * setlos #__NR_sigreturn,gr7
  277. * tira gr0,0
  278. */
  279. if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
  280. __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
  281. __put_user(0xc0700000, &frame->retcode[1]))
  282. goto give_sigsegv;
  283. flush_icache_range((unsigned long) frame->retcode,
  284. (unsigned long) (frame->retcode + 2));
  285. }
  286. /* set up registers for signal handler */
  287. __frame->sp = (unsigned long) frame;
  288. __frame->lr = (unsigned long) &frame->retcode;
  289. __frame->gr8 = sig;
  290. if (get_personality & FDPIC_FUNCPTRS) {
  291. struct fdpic_func_descriptor __user *funcptr =
  292. (struct fdpic_func_descriptor *) ka->sa.sa_handler;
  293. __get_user(__frame->pc, &funcptr->text);
  294. __get_user(__frame->gr15, &funcptr->GOT);
  295. } else {
  296. __frame->pc = (unsigned long) ka->sa.sa_handler;
  297. __frame->gr15 = 0;
  298. }
  299. set_fs(USER_DS);
  300. /* the tracer may want to single-step inside the handler */
  301. if (test_thread_flag(TIF_SINGLESTEP))
  302. ptrace_notify(SIGTRAP);
  303. #if DEBUG_SIG
  304. printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  305. sig, current->comm, current->pid, frame, __frame->pc,
  306. frame->pretcode);
  307. #endif
  308. return 1;
  309. give_sigsegv:
  310. force_sig(SIGSEGV, current);
  311. return 0;
  312. } /* end setup_frame() */
  313. /*****************************************************************************/
  314. /*
  315. *
  316. */
  317. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  318. sigset_t *set)
  319. {
  320. struct rt_sigframe __user *frame;
  321. int rsig;
  322. frame = get_sigframe(ka, sizeof(*frame));
  323. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  324. goto give_sigsegv;
  325. rsig = sig;
  326. if (sig < 32 &&
  327. __current_thread_info->exec_domain &&
  328. __current_thread_info->exec_domain->signal_invmap)
  329. rsig = __current_thread_info->exec_domain->signal_invmap[sig];
  330. if (__put_user(rsig, &frame->sig) ||
  331. __put_user(&frame->info, &frame->pinfo) ||
  332. __put_user(&frame->uc, &frame->puc))
  333. goto give_sigsegv;
  334. if (copy_siginfo_to_user(&frame->info, info))
  335. goto give_sigsegv;
  336. /* Create the ucontext. */
  337. if (__put_user(0, &frame->uc.uc_flags) ||
  338. __put_user(0, &frame->uc.uc_link) ||
  339. __put_user((void*)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
  340. __put_user(sas_ss_flags(__frame->sp), &frame->uc.uc_stack.ss_flags) ||
  341. __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
  342. goto give_sigsegv;
  343. if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
  344. goto give_sigsegv;
  345. if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
  346. goto give_sigsegv;
  347. /* Set up to return from userspace. If provided, use a stub
  348. * already in userspace. */
  349. if (ka->sa.sa_flags & SA_RESTORER) {
  350. if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
  351. goto give_sigsegv;
  352. }
  353. else {
  354. /* Set up the following code on the stack:
  355. * setlos #__NR_sigreturn,gr7
  356. * tira gr0,0
  357. */
  358. if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
  359. __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
  360. __put_user(0xc0700000, &frame->retcode[1]))
  361. goto give_sigsegv;
  362. flush_icache_range((unsigned long) frame->retcode,
  363. (unsigned long) (frame->retcode + 2));
  364. }
  365. /* Set up registers for signal handler */
  366. __frame->sp = (unsigned long) frame;
  367. __frame->lr = (unsigned long) &frame->retcode;
  368. __frame->gr8 = sig;
  369. __frame->gr9 = (unsigned long) &frame->info;
  370. if (get_personality & FDPIC_FUNCPTRS) {
  371. struct fdpic_func_descriptor *funcptr =
  372. (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
  373. __get_user(__frame->pc, &funcptr->text);
  374. __get_user(__frame->gr15, &funcptr->GOT);
  375. } else {
  376. __frame->pc = (unsigned long) ka->sa.sa_handler;
  377. __frame->gr15 = 0;
  378. }
  379. set_fs(USER_DS);
  380. /* the tracer may want to single-step inside the handler */
  381. if (test_thread_flag(TIF_SINGLESTEP))
  382. ptrace_notify(SIGTRAP);
  383. #if DEBUG_SIG
  384. printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  385. sig, current->comm, current->pid, frame, __frame->pc,
  386. frame->pretcode);
  387. #endif
  388. return 1;
  389. give_sigsegv:
  390. force_sig(SIGSEGV, current);
  391. return 0;
  392. } /* end setup_rt_frame() */
  393. /*****************************************************************************/
  394. /*
  395. * OK, we're invoking a handler
  396. */
  397. static int handle_signal(unsigned long sig, siginfo_t *info,
  398. struct k_sigaction *ka, sigset_t *oldset)
  399. {
  400. int ret;
  401. /* Are we from a system call? */
  402. if (in_syscall(__frame)) {
  403. /* If so, check system call restarting.. */
  404. switch (__frame->gr8) {
  405. case -ERESTART_RESTARTBLOCK:
  406. case -ERESTARTNOHAND:
  407. __frame->gr8 = -EINTR;
  408. break;
  409. case -ERESTARTSYS:
  410. if (!(ka->sa.sa_flags & SA_RESTART)) {
  411. __frame->gr8 = -EINTR;
  412. break;
  413. }
  414. /* fallthrough */
  415. case -ERESTARTNOINTR:
  416. __frame->gr8 = __frame->orig_gr8;
  417. __frame->pc -= 4;
  418. }
  419. }
  420. /* Set up the stack frame */
  421. if (ka->sa.sa_flags & SA_SIGINFO)
  422. ret = setup_rt_frame(sig, ka, info, oldset);
  423. else
  424. ret = setup_frame(sig, ka, oldset);
  425. if (ret) {
  426. spin_lock_irq(&current->sighand->siglock);
  427. sigorsets(&current->blocked, &current->blocked,
  428. &ka->sa.sa_mask);
  429. if (!(ka->sa.sa_flags & SA_NODEFER))
  430. sigaddset(&current->blocked, sig);
  431. recalc_sigpending();
  432. spin_unlock_irq(&current->sighand->siglock);
  433. }
  434. return ret;
  435. } /* end handle_signal() */
  436. /*****************************************************************************/
  437. /*
  438. * Note that 'init' is a special process: it doesn't get signals it doesn't
  439. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  440. * mistake.
  441. */
  442. static int do_signal(sigset_t *oldset)
  443. {
  444. struct k_sigaction ka;
  445. siginfo_t info;
  446. int signr;
  447. /*
  448. * We want the common case to go fast, which
  449. * is why we may in certain cases get here from
  450. * kernel mode. Just return without doing anything
  451. * if so.
  452. */
  453. if (!user_mode(__frame))
  454. return 1;
  455. if (try_to_freeze())
  456. goto no_signal;
  457. if (!oldset)
  458. oldset = &current->blocked;
  459. signr = get_signal_to_deliver(&info, &ka, __frame, NULL);
  460. if (signr > 0)
  461. return handle_signal(signr, &info, &ka, oldset);
  462. no_signal:
  463. /* Did we come from a system call? */
  464. if (__frame->syscallno >= 0) {
  465. /* Restart the system call - no handlers present */
  466. if (__frame->gr8 == -ERESTARTNOHAND ||
  467. __frame->gr8 == -ERESTARTSYS ||
  468. __frame->gr8 == -ERESTARTNOINTR) {
  469. __frame->gr8 = __frame->orig_gr8;
  470. __frame->pc -= 4;
  471. }
  472. if (__frame->gr8 == -ERESTART_RESTARTBLOCK){
  473. __frame->gr8 = __NR_restart_syscall;
  474. __frame->pc -= 4;
  475. }
  476. }
  477. return 0;
  478. } /* end do_signal() */
  479. /*****************************************************************************/
  480. /*
  481. * notification of userspace execution resumption
  482. * - triggered by current->work.notify_resume
  483. */
  484. asmlinkage void do_notify_resume(__u32 thread_info_flags)
  485. {
  486. /* pending single-step? */
  487. if (thread_info_flags & _TIF_SINGLESTEP)
  488. clear_thread_flag(TIF_SINGLESTEP);
  489. /* deal with pending signal delivery */
  490. if (thread_info_flags & _TIF_SIGPENDING)
  491. do_signal(NULL);
  492. } /* end do_notify_resume() */