signal.c 13 KB

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