signal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* signal.c: FRV specific bits of signal handling
  2. *
  3. * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from arch/m68k/kernel/signal.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/kernel.h>
  16. #include <linux/signal.h>
  17. #include <linux/errno.h>
  18. #include <linux/wait.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/unistd.h>
  21. #include <linux/personality.h>
  22. #include <linux/tracehook.h>
  23. #include <asm/ucontext.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/cacheflush.h>
  26. #define DEBUG_SIG 0
  27. struct fdpic_func_descriptor {
  28. unsigned long text;
  29. unsigned long GOT;
  30. };
  31. /*
  32. * Do a signal return; undo the signal stack.
  33. */
  34. struct sigframe
  35. {
  36. __sigrestore_t pretcode;
  37. int sig;
  38. struct sigcontext sc;
  39. unsigned long extramask[_NSIG_WORDS-1];
  40. uint32_t retcode[2];
  41. };
  42. struct rt_sigframe
  43. {
  44. __sigrestore_t pretcode;
  45. int sig;
  46. struct siginfo __user *pinfo;
  47. void __user *puc;
  48. struct siginfo info;
  49. struct ucontext uc;
  50. uint32_t retcode[2];
  51. };
  52. static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
  53. {
  54. struct user_context *user = current->thread.user;
  55. unsigned long tbr, psr;
  56. /* Always make any pending restarted system calls return -EINTR */
  57. current->restart_block.fn = do_no_restart_syscall;
  58. tbr = user->i.tbr;
  59. psr = user->i.psr;
  60. if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
  61. goto badframe;
  62. user->i.tbr = tbr;
  63. user->i.psr = psr;
  64. restore_user_regs(user);
  65. user->i.syscallno = -1; /* disable syscall checks */
  66. *_gr8 = user->i.gr[8];
  67. return 0;
  68. badframe:
  69. return 1;
  70. }
  71. asmlinkage int sys_sigreturn(void)
  72. {
  73. struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
  74. sigset_t set;
  75. int gr8;
  76. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  77. goto badframe;
  78. if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
  79. goto badframe;
  80. if (_NSIG_WORDS > 1 &&
  81. __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
  82. goto badframe;
  83. set_current_blocked(&set);
  84. if (restore_sigcontext(&frame->sc, &gr8))
  85. goto badframe;
  86. return gr8;
  87. badframe:
  88. force_sig(SIGSEGV, current);
  89. return 0;
  90. }
  91. asmlinkage int sys_rt_sigreturn(void)
  92. {
  93. struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
  94. sigset_t set;
  95. int gr8;
  96. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  97. goto badframe;
  98. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  99. goto badframe;
  100. set_current_blocked(&set);
  101. if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
  102. goto badframe;
  103. if (restore_altstack(&frame->uc.uc_stack))
  104. goto badframe;
  105. return gr8;
  106. badframe:
  107. force_sig(SIGSEGV, current);
  108. return 0;
  109. }
  110. /*
  111. * Set up a signal frame
  112. */
  113. static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
  114. {
  115. save_user_regs(current->thread.user);
  116. if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
  117. goto badframe;
  118. /* non-iBCS2 extensions.. */
  119. if (__put_user(mask, &sc->sc_oldmask) < 0)
  120. goto badframe;
  121. return 0;
  122. badframe:
  123. return 1;
  124. }
  125. /*****************************************************************************/
  126. /*
  127. * Determine which stack to use..
  128. */
  129. static inline void __user *get_sigframe(struct ksignal *ksig,
  130. size_t frame_size)
  131. {
  132. unsigned long sp = sigsp(__frame->sp, ksig);
  133. return (void __user *) ((sp - frame_size) & ~7UL);
  134. } /* end get_sigframe() */
  135. /*****************************************************************************/
  136. /*
  137. *
  138. */
  139. static int setup_frame(struct ksignal *ksig, sigset_t *set)
  140. {
  141. struct sigframe __user *frame;
  142. int sig = ksig->sig;
  143. frame = get_sigframe(ksig, sizeof(*frame));
  144. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  145. return -EFAULT;
  146. if (__put_user(sig, &frame->sig) < 0)
  147. return -EFAULT;
  148. if (setup_sigcontext(&frame->sc, set->sig[0]))
  149. return -EFAULT;
  150. if (_NSIG_WORDS > 1) {
  151. if (__copy_to_user(frame->extramask, &set->sig[1],
  152. sizeof(frame->extramask)))
  153. return -EFAULT;
  154. }
  155. /* Set up to return from userspace. If provided, use a stub
  156. * already in userspace. */
  157. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  158. if (__put_user(ksig->ka.sa.sa_restorer, &frame->pretcode) < 0)
  159. return -EFAULT;
  160. }
  161. else {
  162. /* Set up the following code on the stack:
  163. * setlos #__NR_sigreturn,gr7
  164. * tira gr0,0
  165. */
  166. if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
  167. __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
  168. __put_user(0xc0700000, &frame->retcode[1]))
  169. return -EFAULT;
  170. flush_icache_range((unsigned long) frame->retcode,
  171. (unsigned long) (frame->retcode + 2));
  172. }
  173. /* Set up registers for the signal handler */
  174. if (current->personality & FDPIC_FUNCPTRS) {
  175. struct fdpic_func_descriptor __user *funcptr =
  176. (struct fdpic_func_descriptor __user *) ksig->ka.sa.sa_handler;
  177. struct fdpic_func_descriptor desc;
  178. if (copy_from_user(&desc, funcptr, sizeof(desc)))
  179. return -EFAULT;
  180. __frame->pc = desc.text;
  181. __frame->gr15 = desc.GOT;
  182. } else {
  183. __frame->pc = (unsigned long) ksig->ka.sa.sa_handler;
  184. __frame->gr15 = 0;
  185. }
  186. __frame->sp = (unsigned long) frame;
  187. __frame->lr = (unsigned long) &frame->retcode;
  188. __frame->gr8 = sig;
  189. #if DEBUG_SIG
  190. printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  191. sig, current->comm, current->pid, frame, __frame->pc,
  192. frame->pretcode);
  193. #endif
  194. return 0;
  195. } /* end setup_frame() */
  196. /*****************************************************************************/
  197. /*
  198. *
  199. */
  200. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set)
  201. {
  202. struct rt_sigframe __user *frame;
  203. int sig = ksig->sig;
  204. frame = get_sigframe(ksig, sizeof(*frame));
  205. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  206. return -EFAULT;
  207. if (__put_user(sig, &frame->sig) ||
  208. __put_user(&frame->info, &frame->pinfo) ||
  209. __put_user(&frame->uc, &frame->puc))
  210. return -EFAULT;
  211. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  212. return -EFAULT;
  213. /* Create the ucontext. */
  214. if (__put_user(0, &frame->uc.uc_flags) ||
  215. __put_user(NULL, &frame->uc.uc_link) ||
  216. __save_altstack(&frame->uc.uc_stack, __frame->sp))
  217. return -EFAULT;
  218. if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
  219. return -EFAULT;
  220. if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
  221. return -EFAULT;
  222. /* Set up to return from userspace. If provided, use a stub
  223. * already in userspace. */
  224. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  225. if (__put_user(ksig->ka.sa.sa_restorer, &frame->pretcode))
  226. return -EFAULT;
  227. }
  228. else {
  229. /* Set up the following code on the stack:
  230. * setlos #__NR_sigreturn,gr7
  231. * tira gr0,0
  232. */
  233. if (__put_user((__sigrestore_t)frame->retcode, &frame->pretcode) ||
  234. __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
  235. __put_user(0xc0700000, &frame->retcode[1]))
  236. return -EFAULT;
  237. flush_icache_range((unsigned long) frame->retcode,
  238. (unsigned long) (frame->retcode + 2));
  239. }
  240. /* Set up registers for signal handler */
  241. if (current->personality & FDPIC_FUNCPTRS) {
  242. struct fdpic_func_descriptor __user *funcptr =
  243. (struct fdpic_func_descriptor __user *) ksig->ka.sa.sa_handler;
  244. struct fdpic_func_descriptor desc;
  245. if (copy_from_user(&desc, funcptr, sizeof(desc)))
  246. return -EFAULT;
  247. __frame->pc = desc.text;
  248. __frame->gr15 = desc.GOT;
  249. } else {
  250. __frame->pc = (unsigned long) ksig->ka.sa.sa_handler;
  251. __frame->gr15 = 0;
  252. }
  253. __frame->sp = (unsigned long) frame;
  254. __frame->lr = (unsigned long) &frame->retcode;
  255. __frame->gr8 = sig;
  256. __frame->gr9 = (unsigned long) &frame->info;
  257. #if DEBUG_SIG
  258. printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
  259. sig, current->comm, current->pid, frame, __frame->pc,
  260. frame->pretcode);
  261. #endif
  262. return 0;
  263. } /* end setup_rt_frame() */
  264. /*****************************************************************************/
  265. /*
  266. * OK, we're invoking a handler
  267. */
  268. static void handle_signal(struct ksignal *ksig)
  269. {
  270. sigset_t *oldset = sigmask_to_save();
  271. int ret;
  272. /* Are we from a system call? */
  273. if (__frame->syscallno != -1) {
  274. /* If so, check system call restarting.. */
  275. switch (__frame->gr8) {
  276. case -ERESTART_RESTARTBLOCK:
  277. case -ERESTARTNOHAND:
  278. __frame->gr8 = -EINTR;
  279. break;
  280. case -ERESTARTSYS:
  281. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  282. __frame->gr8 = -EINTR;
  283. break;
  284. }
  285. /* fallthrough */
  286. case -ERESTARTNOINTR:
  287. __frame->gr8 = __frame->orig_gr8;
  288. __frame->pc -= 4;
  289. }
  290. __frame->syscallno = -1;
  291. }
  292. /* Set up the stack frame */
  293. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  294. ret = setup_rt_frame(ksig, oldset);
  295. else
  296. ret = setup_frame(ksig, oldset);
  297. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  298. } /* end handle_signal() */
  299. /*****************************************************************************/
  300. /*
  301. * Note that 'init' is a special process: it doesn't get signals it doesn't
  302. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  303. * mistake.
  304. */
  305. static void do_signal(void)
  306. {
  307. struct ksignal ksig;
  308. if (get_signal(&ksig)) {
  309. handle_signal(&ksig);
  310. return;
  311. }
  312. /* Did we come from a system call? */
  313. if (__frame->syscallno != -1) {
  314. /* Restart the system call - no handlers present */
  315. switch (__frame->gr8) {
  316. case -ERESTARTNOHAND:
  317. case -ERESTARTSYS:
  318. case -ERESTARTNOINTR:
  319. __frame->gr8 = __frame->orig_gr8;
  320. __frame->pc -= 4;
  321. break;
  322. case -ERESTART_RESTARTBLOCK:
  323. __frame->gr7 = __NR_restart_syscall;
  324. __frame->pc -= 4;
  325. break;
  326. }
  327. __frame->syscallno = -1;
  328. }
  329. /* if there's no signal to deliver, we just put the saved sigmask
  330. * back */
  331. restore_saved_sigmask();
  332. } /* end do_signal() */
  333. /*****************************************************************************/
  334. /*
  335. * notification of userspace execution resumption
  336. * - triggered by the TIF_WORK_MASK flags
  337. */
  338. asmlinkage void do_notify_resume(__u32 thread_info_flags)
  339. {
  340. /* pending single-step? */
  341. if (thread_info_flags & _TIF_SINGLESTEP)
  342. clear_thread_flag(TIF_SINGLESTEP);
  343. /* deal with pending signal delivery */
  344. if (thread_info_flags & _TIF_SIGPENDING)
  345. do_signal();
  346. /* deal with notification on about to resume userspace execution */
  347. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  348. clear_thread_flag(TIF_NOTIFY_RESUME);
  349. tracehook_notify_resume(__frame);
  350. }
  351. } /* end do_notify_resume() */