signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * linux/arch/cris/kernel/signal.c
  3. *
  4. * Based on arch/i386/kernel/signal.c by
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson *
  7. *
  8. * Ideas also taken from arch/arm.
  9. *
  10. * Copyright (C) 2000-2007 Axis Communications AB
  11. *
  12. * Authors: Bjorn Wesen (bjornw@axis.com)
  13. *
  14. */
  15. #include <linux/sched.h>
  16. #include <linux/mm.h>
  17. #include <linux/smp.h>
  18. #include <linux/kernel.h>
  19. #include <linux/signal.h>
  20. #include <linux/errno.h>
  21. #include <linux/wait.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/unistd.h>
  24. #include <linux/stddef.h>
  25. #include <asm/processor.h>
  26. #include <asm/ucontext.h>
  27. #include <asm/uaccess.h>
  28. #include <arch/system.h>
  29. #define DEBUG_SIG 0
  30. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  31. /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
  32. /* manipulate regs so that upon return, it will be re-executed */
  33. /* We rely on that pc points to the instruction after "break 13", so the
  34. * library must never do strange things like putting it in a delay slot.
  35. */
  36. #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
  37. void do_signal(int canrestart, struct pt_regs *regs);
  38. /*
  39. * Atomically swap in the new signal mask, and wait for a signal. Define
  40. * dummy arguments to be able to reach the regs argument. (Note that this
  41. * arrangement relies on old_sigset_t occupying one register.)
  42. */
  43. int sys_sigsuspend(old_sigset_t mask)
  44. {
  45. sigset_t blocked;
  46. siginitset(&blocked, mask);
  47. return sigsuspend(&blocked);
  48. }
  49. int sys_sigaction(int sig, const struct old_sigaction __user *act,
  50. struct old_sigaction *oact)
  51. {
  52. struct k_sigaction new_ka, old_ka;
  53. int ret;
  54. if (act) {
  55. old_sigset_t mask;
  56. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  57. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  58. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
  59. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  60. __get_user(mask, &act->sa_mask))
  61. return -EFAULT;
  62. siginitset(&new_ka.sa.sa_mask, mask);
  63. }
  64. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  65. if (!ret && oact) {
  66. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  67. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  68. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
  69. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  70. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  71. return -EFAULT;
  72. }
  73. return ret;
  74. }
  75. int sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
  76. {
  77. return do_sigaltstack(uss, uoss, rdusp());
  78. }
  79. /*
  80. * Do a signal return; undo the signal stack.
  81. */
  82. struct sigframe {
  83. struct sigcontext sc;
  84. unsigned long extramask[_NSIG_WORDS-1];
  85. unsigned char retcode[8]; /* trampoline code */
  86. };
  87. struct rt_sigframe {
  88. struct siginfo *pinfo;
  89. void *puc;
  90. struct siginfo info;
  91. struct ucontext uc;
  92. unsigned char retcode[8]; /* trampoline code */
  93. };
  94. static int
  95. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  96. {
  97. unsigned int err = 0;
  98. unsigned long old_usp;
  99. /* Always make any pending restarted system calls return -EINTR */
  100. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  101. /* restore the regs from &sc->regs (same as sc, since regs is first)
  102. * (sc is already checked for VERIFY_READ since the sigframe was
  103. * checked in sys_sigreturn previously)
  104. */
  105. if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
  106. goto badframe;
  107. /* make sure the U-flag is set so user-mode cannot fool us */
  108. regs->dccr |= 1 << 8;
  109. /* restore the old USP as it was before we stacked the sc etc.
  110. * (we cannot just pop the sigcontext since we aligned the sp and
  111. * stuff after pushing it)
  112. */
  113. err |= __get_user(old_usp, &sc->usp);
  114. wrusp(old_usp);
  115. /* TODO: the other ports use regs->orig_XX to disable syscall checks
  116. * after this completes, but we don't use that mechanism. maybe we can
  117. * use it now ?
  118. */
  119. return err;
  120. badframe:
  121. return 1;
  122. }
  123. /* Define dummy arguments to be able to reach the regs argument. */
  124. asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
  125. long srp, struct pt_regs *regs)
  126. {
  127. struct sigframe __user *frame = (struct sigframe *)rdusp();
  128. sigset_t set;
  129. /*
  130. * Since we stacked the signal on a dword boundary,
  131. * then frame should be dword aligned here. If it's
  132. * not, then the user is trying to mess with us.
  133. */
  134. if (((long)frame) & 3)
  135. goto badframe;
  136. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  137. goto badframe;
  138. if (__get_user(set.sig[0], &frame->sc.oldmask)
  139. || (_NSIG_WORDS > 1
  140. && __copy_from_user(&set.sig[1], frame->extramask,
  141. sizeof(frame->extramask))))
  142. goto badframe;
  143. sigdelsetmask(&set, ~_BLOCKABLE);
  144. set_current_blocked(&set);
  145. if (restore_sigcontext(regs, &frame->sc))
  146. goto badframe;
  147. /* TODO: SIGTRAP when single-stepping as in arm ? */
  148. return regs->r10;
  149. badframe:
  150. force_sig(SIGSEGV, current);
  151. return 0;
  152. }
  153. /* Define dummy arguments to be able to reach the regs argument. */
  154. asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
  155. long mof, long srp, struct pt_regs *regs)
  156. {
  157. struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
  158. sigset_t set;
  159. /*
  160. * Since we stacked the signal on a dword boundary,
  161. * then frame should be dword aligned here. If it's
  162. * not, then the user is trying to mess with us.
  163. */
  164. if (((long)frame) & 3)
  165. goto badframe;
  166. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  167. goto badframe;
  168. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  169. goto badframe;
  170. sigdelsetmask(&set, ~_BLOCKABLE);
  171. set_current_blocked(&set);
  172. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  173. goto badframe;
  174. if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
  175. goto badframe;
  176. return regs->r10;
  177. badframe:
  178. force_sig(SIGSEGV, current);
  179. return 0;
  180. }
  181. /*
  182. * Set up a signal frame.
  183. */
  184. static int setup_sigcontext(struct sigcontext __user *sc,
  185. struct pt_regs *regs, unsigned long mask)
  186. {
  187. int err = 0;
  188. unsigned long usp = rdusp();
  189. /* copy the regs. they are first in sc so we can use sc directly */
  190. err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
  191. /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
  192. the signal handler. The frametype will be restored to its previous
  193. value in restore_sigcontext. */
  194. regs->frametype = CRIS_FRAME_NORMAL;
  195. /* then some other stuff */
  196. err |= __put_user(mask, &sc->oldmask);
  197. err |= __put_user(usp, &sc->usp);
  198. return err;
  199. }
  200. /* Figure out where we want to put the new signal frame
  201. * - usually on the stack. */
  202. static inline void __user *
  203. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
  204. {
  205. unsigned long sp = rdusp();
  206. /* This is the X/Open sanctioned signal stack switching. */
  207. if (ka->sa.sa_flags & SA_ONSTACK) {
  208. if (! on_sig_stack(sp))
  209. sp = current->sas_ss_sp + current->sas_ss_size;
  210. }
  211. /* make sure the frame is dword-aligned */
  212. sp &= ~3;
  213. return (void __user*)(sp - frame_size);
  214. }
  215. /* grab and setup a signal frame.
  216. *
  217. * basically we stack a lot of state info, and arrange for the
  218. * user-mode program to return to the kernel using either a
  219. * trampoline which performs the syscall sigreturn, or a provided
  220. * user-mode trampoline.
  221. */
  222. static int setup_frame(int sig, struct k_sigaction *ka,
  223. sigset_t *set, struct pt_regs *regs)
  224. {
  225. struct sigframe __user *frame;
  226. unsigned long return_ip;
  227. int err = 0;
  228. frame = get_sigframe(ka, regs, sizeof(*frame));
  229. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  230. goto give_sigsegv;
  231. err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
  232. if (err)
  233. goto give_sigsegv;
  234. if (_NSIG_WORDS > 1) {
  235. err |= __copy_to_user(frame->extramask, &set->sig[1],
  236. sizeof(frame->extramask));
  237. }
  238. if (err)
  239. goto give_sigsegv;
  240. /* Set up to return from userspace. If provided, use a stub
  241. already in userspace. */
  242. if (ka->sa.sa_flags & SA_RESTORER) {
  243. return_ip = (unsigned long)ka->sa.sa_restorer;
  244. } else {
  245. /* trampoline - the desired return ip is the retcode itself */
  246. return_ip = (unsigned long)&frame->retcode;
  247. /* This is movu.w __NR_sigreturn, r9; break 13; */
  248. err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
  249. err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
  250. err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
  251. }
  252. if (err)
  253. goto give_sigsegv;
  254. /* Set up registers for signal handler */
  255. regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
  256. regs->srp = return_ip; /* what we enter LATER */
  257. regs->r10 = sig; /* first argument is signo */
  258. /* actually move the usp to reflect the stacked frame */
  259. wrusp((unsigned long)frame);
  260. return 0;
  261. give_sigsegv:
  262. force_sigsegv(sig, current);
  263. return -EFAULT;
  264. }
  265. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  266. sigset_t *set, struct pt_regs *regs)
  267. {
  268. struct rt_sigframe __user *frame;
  269. unsigned long return_ip;
  270. int err = 0;
  271. frame = get_sigframe(ka, regs, sizeof(*frame));
  272. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  273. goto give_sigsegv;
  274. err |= __put_user(&frame->info, &frame->pinfo);
  275. err |= __put_user(&frame->uc, &frame->puc);
  276. err |= copy_siginfo_to_user(&frame->info, info);
  277. if (err)
  278. goto give_sigsegv;
  279. /* Clear all the bits of the ucontext we don't use. */
  280. err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
  281. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  282. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  283. if (err)
  284. goto give_sigsegv;
  285. /* Set up to return from userspace. If provided, use a stub
  286. already in userspace. */
  287. if (ka->sa.sa_flags & SA_RESTORER) {
  288. return_ip = (unsigned long)ka->sa.sa_restorer;
  289. } else {
  290. /* trampoline - the desired return ip is the retcode itself */
  291. return_ip = (unsigned long)&frame->retcode;
  292. /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
  293. err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
  294. err |= __put_user(__NR_rt_sigreturn,
  295. (short __user *)(frame->retcode+2));
  296. err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
  297. }
  298. if (err)
  299. goto give_sigsegv;
  300. /* TODO what is the current->exec_domain stuff and invmap ? */
  301. /* Set up registers for signal handler */
  302. /* What we enter NOW */
  303. regs->irp = (unsigned long) ka->sa.sa_handler;
  304. /* What we enter LATER */
  305. regs->srp = return_ip;
  306. /* First argument is signo */
  307. regs->r10 = sig;
  308. /* Second argument is (siginfo_t *) */
  309. regs->r11 = (unsigned long)&frame->info;
  310. /* Third argument is unused */
  311. regs->r12 = 0;
  312. /* Actually move the usp to reflect the stacked frame */
  313. wrusp((unsigned long)frame);
  314. return 0;
  315. give_sigsegv:
  316. force_sigsegv(sig, current);
  317. return -EFAULT;
  318. }
  319. /*
  320. * OK, we're invoking a handler
  321. */
  322. static inline void handle_signal(int canrestart, unsigned long sig,
  323. siginfo_t *info, struct k_sigaction *ka,
  324. struct pt_regs *regs)
  325. {
  326. sigset_t *oldset = sigmask_to_save();
  327. int ret;
  328. /* Are we from a system call? */
  329. if (canrestart) {
  330. /* If so, check system call restarting.. */
  331. switch (regs->r10) {
  332. case -ERESTART_RESTARTBLOCK:
  333. case -ERESTARTNOHAND:
  334. /* ERESTARTNOHAND means that the syscall should
  335. * only be restarted if there was no handler for
  336. * the signal, and since we only get here if there
  337. * is a handler, we don't restart */
  338. regs->r10 = -EINTR;
  339. break;
  340. case -ERESTARTSYS:
  341. /* ERESTARTSYS means to restart the syscall if
  342. * there is no handler or the handler was
  343. * registered with SA_RESTART */
  344. if (!(ka->sa.sa_flags & SA_RESTART)) {
  345. regs->r10 = -EINTR;
  346. break;
  347. }
  348. /* fallthrough */
  349. case -ERESTARTNOINTR:
  350. /* ERESTARTNOINTR means that the syscall should
  351. * be called again after the signal handler returns. */
  352. RESTART_CRIS_SYS(regs);
  353. }
  354. }
  355. /* Set up the stack frame */
  356. if (ka->sa.sa_flags & SA_SIGINFO)
  357. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  358. else
  359. ret = setup_frame(sig, ka, oldset, regs);
  360. if (ret == 0)
  361. block_sigmask(ka, sig);
  362. }
  363. /*
  364. * Note that 'init' is a special process: it doesn't get signals it doesn't
  365. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  366. * mistake.
  367. *
  368. * Also note that the regs structure given here as an argument, is the latest
  369. * pushed pt_regs. It may or may not be the same as the first pushed registers
  370. * when the initial usermode->kernelmode transition took place. Therefore
  371. * we can use user_mode(regs) to see if we came directly from kernel or user
  372. * mode below.
  373. */
  374. void do_signal(int canrestart, struct pt_regs *regs)
  375. {
  376. siginfo_t info;
  377. int signr;
  378. struct k_sigaction ka;
  379. /*
  380. * We want the common case to go fast, which
  381. * is why we may in certain cases get here from
  382. * kernel mode. Just return without doing anything
  383. * if so.
  384. */
  385. if (!user_mode(regs))
  386. return;
  387. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  388. if (signr > 0) {
  389. /* Whee! Actually deliver the signal. */
  390. handle_signal(canrestart, signr, &info, &ka, regs);
  391. return;
  392. }
  393. /* Did we come from a system call? */
  394. if (canrestart) {
  395. /* Restart the system call - no handlers present */
  396. if (regs->r10 == -ERESTARTNOHAND ||
  397. regs->r10 == -ERESTARTSYS ||
  398. regs->r10 == -ERESTARTNOINTR) {
  399. RESTART_CRIS_SYS(regs);
  400. }
  401. if (regs->r10 == -ERESTART_RESTARTBLOCK) {
  402. regs->r9 = __NR_restart_syscall;
  403. regs->irp -= 2;
  404. }
  405. }
  406. /* if there's no signal to deliver, we just put the saved sigmask
  407. * back */
  408. restore_saved_sigmask();
  409. }