signal.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2013-2014 Altera Corporation
  3. * Copyright (C) 2011-2012 Tobias Klauser <tklauser@distanz.ch>
  4. * Copyright (C) 2004 Microtronix Datacom Ltd
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/signal.h>
  12. #include <linux/errno.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/unistd.h>
  16. #include <linux/personality.h>
  17. #include <linux/tracehook.h>
  18. #include <asm/ucontext.h>
  19. #include <asm/cacheflush.h>
  20. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  21. /*
  22. * Do a signal return; undo the signal stack.
  23. *
  24. * Keep the return code on the stack quadword aligned!
  25. * That makes the cache flush below easier.
  26. */
  27. struct rt_sigframe {
  28. struct siginfo info;
  29. struct ucontext uc;
  30. };
  31. static inline int rt_restore_ucontext(struct pt_regs *regs,
  32. struct switch_stack *sw,
  33. struct ucontext *uc, int *pr2)
  34. {
  35. int temp;
  36. unsigned long *gregs = uc->uc_mcontext.gregs;
  37. int err;
  38. /* Always make any pending restarted system calls return -EINTR */
  39. current->restart_block.fn = do_no_restart_syscall;
  40. err = __get_user(temp, &uc->uc_mcontext.version);
  41. if (temp != MCONTEXT_VERSION)
  42. goto badframe;
  43. /* restore passed registers */
  44. err |= __get_user(regs->r1, &gregs[0]);
  45. err |= __get_user(regs->r2, &gregs[1]);
  46. err |= __get_user(regs->r3, &gregs[2]);
  47. err |= __get_user(regs->r4, &gregs[3]);
  48. err |= __get_user(regs->r5, &gregs[4]);
  49. err |= __get_user(regs->r6, &gregs[5]);
  50. err |= __get_user(regs->r7, &gregs[6]);
  51. err |= __get_user(regs->r8, &gregs[7]);
  52. err |= __get_user(regs->r9, &gregs[8]);
  53. err |= __get_user(regs->r10, &gregs[9]);
  54. err |= __get_user(regs->r11, &gregs[10]);
  55. err |= __get_user(regs->r12, &gregs[11]);
  56. err |= __get_user(regs->r13, &gregs[12]);
  57. err |= __get_user(regs->r14, &gregs[13]);
  58. err |= __get_user(regs->r15, &gregs[14]);
  59. err |= __get_user(sw->r16, &gregs[15]);
  60. err |= __get_user(sw->r17, &gregs[16]);
  61. err |= __get_user(sw->r18, &gregs[17]);
  62. err |= __get_user(sw->r19, &gregs[18]);
  63. err |= __get_user(sw->r20, &gregs[19]);
  64. err |= __get_user(sw->r21, &gregs[20]);
  65. err |= __get_user(sw->r22, &gregs[21]);
  66. err |= __get_user(sw->r23, &gregs[22]);
  67. /* gregs[23] is handled below */
  68. err |= __get_user(sw->fp, &gregs[24]); /* Verify, should this be
  69. settable */
  70. err |= __get_user(sw->gp, &gregs[25]); /* Verify, should this be
  71. settable */
  72. err |= __get_user(temp, &gregs[26]); /* Not really necessary no user
  73. settable bits */
  74. err |= __get_user(regs->ea, &gregs[27]);
  75. err |= __get_user(regs->ra, &gregs[23]);
  76. err |= __get_user(regs->sp, &gregs[28]);
  77. regs->orig_r2 = -1; /* disable syscall checks */
  78. err |= restore_altstack(&uc->uc_stack);
  79. if (err)
  80. goto badframe;
  81. *pr2 = regs->r2;
  82. return err;
  83. badframe:
  84. return 1;
  85. }
  86. asmlinkage int do_rt_sigreturn(struct switch_stack *sw)
  87. {
  88. struct pt_regs *regs = (struct pt_regs *)(sw + 1);
  89. /* Verify, can we follow the stack back */
  90. struct rt_sigframe *frame = (struct rt_sigframe *) regs->sp;
  91. sigset_t set;
  92. int rval;
  93. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  94. goto badframe;
  95. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  96. goto badframe;
  97. set_current_blocked(&set);
  98. if (rt_restore_ucontext(regs, sw, &frame->uc, &rval))
  99. goto badframe;
  100. return rval;
  101. badframe:
  102. force_sig(SIGSEGV, current);
  103. return 0;
  104. }
  105. static inline int rt_setup_ucontext(struct ucontext *uc, struct pt_regs *regs)
  106. {
  107. struct switch_stack *sw = (struct switch_stack *)regs - 1;
  108. unsigned long *gregs = uc->uc_mcontext.gregs;
  109. int err = 0;
  110. err |= __put_user(MCONTEXT_VERSION, &uc->uc_mcontext.version);
  111. err |= __put_user(regs->r1, &gregs[0]);
  112. err |= __put_user(regs->r2, &gregs[1]);
  113. err |= __put_user(regs->r3, &gregs[2]);
  114. err |= __put_user(regs->r4, &gregs[3]);
  115. err |= __put_user(regs->r5, &gregs[4]);
  116. err |= __put_user(regs->r6, &gregs[5]);
  117. err |= __put_user(regs->r7, &gregs[6]);
  118. err |= __put_user(regs->r8, &gregs[7]);
  119. err |= __put_user(regs->r9, &gregs[8]);
  120. err |= __put_user(regs->r10, &gregs[9]);
  121. err |= __put_user(regs->r11, &gregs[10]);
  122. err |= __put_user(regs->r12, &gregs[11]);
  123. err |= __put_user(regs->r13, &gregs[12]);
  124. err |= __put_user(regs->r14, &gregs[13]);
  125. err |= __put_user(regs->r15, &gregs[14]);
  126. err |= __put_user(sw->r16, &gregs[15]);
  127. err |= __put_user(sw->r17, &gregs[16]);
  128. err |= __put_user(sw->r18, &gregs[17]);
  129. err |= __put_user(sw->r19, &gregs[18]);
  130. err |= __put_user(sw->r20, &gregs[19]);
  131. err |= __put_user(sw->r21, &gregs[20]);
  132. err |= __put_user(sw->r22, &gregs[21]);
  133. err |= __put_user(sw->r23, &gregs[22]);
  134. err |= __put_user(regs->ra, &gregs[23]);
  135. err |= __put_user(sw->fp, &gregs[24]);
  136. err |= __put_user(sw->gp, &gregs[25]);
  137. err |= __put_user(regs->ea, &gregs[27]);
  138. err |= __put_user(regs->sp, &gregs[28]);
  139. return err;
  140. }
  141. static inline void *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
  142. size_t frame_size)
  143. {
  144. unsigned long usp;
  145. /* Default to using normal stack. */
  146. usp = regs->sp;
  147. /* This is the X/Open sanctioned signal stack switching. */
  148. usp = sigsp(usp, ksig);
  149. /* Verify, is it 32 or 64 bit aligned */
  150. return (void *)((usp - frame_size) & -8UL);
  151. }
  152. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  153. struct pt_regs *regs)
  154. {
  155. struct rt_sigframe *frame;
  156. int err = 0;
  157. frame = get_sigframe(ksig, regs, sizeof(*frame));
  158. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  159. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  160. /* Create the ucontext. */
  161. err |= __put_user(0, &frame->uc.uc_flags);
  162. err |= __put_user(0, &frame->uc.uc_link);
  163. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  164. err |= rt_setup_ucontext(&frame->uc, regs);
  165. err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  166. if (err)
  167. goto give_sigsegv;
  168. /* Set up to return from userspace; jump to fixed address sigreturn
  169. trampoline on kuser page. */
  170. regs->ra = (unsigned long) (0x1044);
  171. /* Set up registers for signal handler */
  172. regs->sp = (unsigned long) frame;
  173. regs->r4 = (unsigned long) ksig->sig;
  174. regs->r5 = (unsigned long) &frame->info;
  175. regs->r6 = (unsigned long) &frame->uc;
  176. regs->ea = (unsigned long) ksig->ka.sa.sa_handler;
  177. return 0;
  178. give_sigsegv:
  179. force_sigsegv(ksig->sig, current);
  180. return -EFAULT;
  181. }
  182. /*
  183. * OK, we're invoking a handler
  184. */
  185. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  186. {
  187. int ret;
  188. sigset_t *oldset = sigmask_to_save();
  189. /* set up the stack frame */
  190. ret = setup_rt_frame(ksig, oldset, regs);
  191. signal_setup_done(ret, ksig, 0);
  192. }
  193. static int do_signal(struct pt_regs *regs)
  194. {
  195. unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
  196. int restart = 0;
  197. struct ksignal ksig;
  198. current->thread.kregs = regs;
  199. /*
  200. * If we were from a system call, check for system call restarting...
  201. */
  202. if (regs->orig_r2 >= 0) {
  203. continue_addr = regs->ea;
  204. restart_addr = continue_addr - 4;
  205. retval = regs->r2;
  206. /*
  207. * Prepare for system call restart. We do this here so that a
  208. * debugger will see the already changed PC.
  209. */
  210. switch (retval) {
  211. case ERESTART_RESTARTBLOCK:
  212. restart = -2;
  213. case ERESTARTNOHAND:
  214. case ERESTARTSYS:
  215. case ERESTARTNOINTR:
  216. restart++;
  217. regs->r2 = regs->orig_r2;
  218. regs->r7 = regs->orig_r7;
  219. regs->ea = restart_addr;
  220. break;
  221. }
  222. }
  223. if (get_signal(&ksig)) {
  224. /* handler */
  225. if (unlikely(restart && regs->ea == restart_addr)) {
  226. if (retval == ERESTARTNOHAND ||
  227. retval == ERESTART_RESTARTBLOCK ||
  228. (retval == ERESTARTSYS
  229. && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
  230. regs->r2 = EINTR;
  231. regs->r7 = 1;
  232. regs->ea = continue_addr;
  233. }
  234. }
  235. handle_signal(&ksig, regs);
  236. return 0;
  237. }
  238. /*
  239. * No handler present
  240. */
  241. if (unlikely(restart) && regs->ea == restart_addr) {
  242. regs->ea = continue_addr;
  243. regs->r2 = __NR_restart_syscall;
  244. }
  245. /*
  246. * If there's no signal to deliver, we just put the saved sigmask back.
  247. */
  248. restore_saved_sigmask();
  249. return restart;
  250. }
  251. asmlinkage int do_notify_resume(struct pt_regs *regs)
  252. {
  253. /*
  254. * We want the common case to go fast, which is why we may in certain
  255. * cases get here from kernel mode. Just return without doing anything
  256. * if so.
  257. */
  258. if (!user_mode(regs))
  259. return 0;
  260. if (test_thread_flag(TIF_SIGPENDING)) {
  261. int restart = do_signal(regs);
  262. if (unlikely(restart)) {
  263. /*
  264. * Restart without handlers.
  265. * Deal with it without leaving
  266. * the kernel space.
  267. */
  268. return restart;
  269. }
  270. } else if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
  271. tracehook_notify_resume(regs);
  272. return 0;
  273. }