signal.c 12 KB

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