signal.c 13 KB

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