signal.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * OpenRISC signal.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/sched.h>
  18. #include <linux/mm.h>
  19. #include <linux/smp.h>
  20. #include <linux/kernel.h>
  21. #include <linux/signal.h>
  22. #include <linux/errno.h>
  23. #include <linux/wait.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/unistd.h>
  26. #include <linux/stddef.h>
  27. #include <linux/tracehook.h>
  28. #include <asm/processor.h>
  29. #include <asm/syscall.h>
  30. #include <asm/ucontext.h>
  31. #include <asm/uaccess.h>
  32. #define DEBUG_SIG 0
  33. struct rt_sigframe {
  34. struct siginfo info;
  35. struct ucontext uc;
  36. unsigned char retcode[16]; /* trampoline code */
  37. };
  38. static int restore_sigcontext(struct pt_regs *regs,
  39. struct sigcontext __user *sc)
  40. {
  41. int err = 0;
  42. /* Always make any pending restarted system calls return -EINTR */
  43. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  44. /*
  45. * Restore the regs from &sc->regs.
  46. * (sc is already checked for VERIFY_READ since the sigframe was
  47. * checked in sys_sigreturn previously)
  48. */
  49. err |= __copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long));
  50. err |= __copy_from_user(&regs->pc, &sc->regs.pc, sizeof(unsigned long));
  51. err |= __copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long));
  52. /* make sure the SM-bit is cleared so user-mode cannot fool us */
  53. regs->sr &= ~SPR_SR_SM;
  54. regs->orig_gpr11 = -1; /* Avoid syscall restart checks */
  55. /* TODO: the other ports use regs->orig_XX to disable syscall checks
  56. * after this completes, but we don't use that mechanism. maybe we can
  57. * use it now ?
  58. */
  59. return err;
  60. }
  61. asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs)
  62. {
  63. struct rt_sigframe *frame = (struct rt_sigframe __user *)regs->sp;
  64. sigset_t set;
  65. /*
  66. * Since we stacked the signal on a dword boundary,
  67. * then frame should be dword aligned here. If it's
  68. * not, then the user is trying to mess with us.
  69. */
  70. if (((long)frame) & 3)
  71. goto badframe;
  72. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  73. goto badframe;
  74. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  75. goto badframe;
  76. set_current_blocked(&set);
  77. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  78. goto badframe;
  79. if (restore_altstack(&frame->uc.uc_stack))
  80. goto badframe;
  81. return regs->gpr[11];
  82. badframe:
  83. force_sig(SIGSEGV, current);
  84. return 0;
  85. }
  86. /*
  87. * Set up a signal frame.
  88. */
  89. static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  90. {
  91. int err = 0;
  92. /* copy the regs */
  93. /* There should be no need to save callee-saved registers here...
  94. * ...but we save them anyway. Revisit this
  95. */
  96. err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long));
  97. err |= __copy_to_user(&sc->regs.pc, &regs->pc, sizeof(unsigned long));
  98. err |= __copy_to_user(&sc->regs.sr, &regs->sr, sizeof(unsigned long));
  99. return err;
  100. }
  101. static inline unsigned long align_sigframe(unsigned long sp)
  102. {
  103. return sp & ~3UL;
  104. }
  105. /*
  106. * Work out where the signal frame should go. It's either on the user stack
  107. * or the alternate stack.
  108. */
  109. static inline void __user *get_sigframe(struct k_sigaction *ka,
  110. struct pt_regs *regs, size_t frame_size)
  111. {
  112. unsigned long sp = regs->sp;
  113. int onsigstack = on_sig_stack(sp);
  114. /* redzone */
  115. sp -= STACK_FRAME_OVERHEAD;
  116. /* This is the X/Open sanctioned signal stack switching. */
  117. if ((ka->sa.sa_flags & SA_ONSTACK) && !onsigstack) {
  118. if (current->sas_ss_size)
  119. sp = current->sas_ss_sp + current->sas_ss_size;
  120. }
  121. sp = align_sigframe(sp - frame_size);
  122. /*
  123. * If we are on the alternate signal stack and would overflow it, don't.
  124. * Return an always-bogus address instead so we will die with SIGSEGV.
  125. */
  126. if (onsigstack && !likely(on_sig_stack(sp)))
  127. return (void __user *)-1L;
  128. return (void __user *)sp;
  129. }
  130. /* grab and setup a signal frame.
  131. *
  132. * basically we stack a lot of state info, and arrange for the
  133. * user-mode program to return to the kernel using either a
  134. * trampoline which performs the syscall sigreturn, or a provided
  135. * user-mode trampoline.
  136. */
  137. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  138. struct pt_regs *regs)
  139. {
  140. struct rt_sigframe *frame;
  141. unsigned long return_ip;
  142. int err = 0;
  143. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
  144. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  145. return -EFAULT;
  146. /* Create siginfo. */
  147. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  148. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  149. /* Create the ucontext. */
  150. err |= __put_user(0, &frame->uc.uc_flags);
  151. err |= __put_user(NULL, &frame->uc.uc_link);
  152. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  153. err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
  154. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  155. if (err)
  156. return -EFAULT;
  157. /* trampoline - the desired return ip is the retcode itself */
  158. return_ip = (unsigned long)&frame->retcode;
  159. /* This is:
  160. l.ori r11,r0,__NR_sigreturn
  161. l.sys 1
  162. */
  163. err |= __put_user(0xa960, (short *)(frame->retcode + 0));
  164. err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode + 2));
  165. err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
  166. err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
  167. if (err)
  168. return -EFAULT;
  169. /* TODO what is the current->exec_domain stuff and invmap ? */
  170. /* Set up registers for signal handler */
  171. regs->pc = (unsigned long)ksig->ka.sa.sa_handler; /* what we enter NOW */
  172. regs->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
  173. regs->gpr[3] = (unsigned long)ksig->sig; /* arg 1: signo */
  174. regs->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
  175. regs->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
  176. /* actually move the usp to reflect the stacked frame */
  177. regs->sp = (unsigned long)frame;
  178. return 0;
  179. }
  180. static inline void
  181. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  182. {
  183. int ret;
  184. ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
  185. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  186. }
  187. /*
  188. * Note that 'init' is a special process: it doesn't get signals it doesn't
  189. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  190. * mistake.
  191. *
  192. * Also note that the regs structure given here as an argument, is the latest
  193. * pushed pt_regs. It may or may not be the same as the first pushed registers
  194. * when the initial usermode->kernelmode transition took place. Therefore
  195. * we can use user_mode(regs) to see if we came directly from kernel or user
  196. * mode below.
  197. */
  198. int do_signal(struct pt_regs *regs, int syscall)
  199. {
  200. struct ksignal ksig;
  201. unsigned long continue_addr = 0;
  202. unsigned long restart_addr = 0;
  203. unsigned long retval = 0;
  204. int restart = 0;
  205. if (syscall) {
  206. continue_addr = regs->pc;
  207. restart_addr = continue_addr - 4;
  208. retval = regs->gpr[11];
  209. /*
  210. * Setup syscall restart here so that a debugger will
  211. * see the already changed PC.
  212. */
  213. switch (retval) {
  214. case -ERESTART_RESTARTBLOCK:
  215. restart = -2;
  216. /* Fall through */
  217. case -ERESTARTNOHAND:
  218. case -ERESTARTSYS:
  219. case -ERESTARTNOINTR:
  220. restart++;
  221. regs->gpr[11] = regs->orig_gpr11;
  222. regs->pc = restart_addr;
  223. break;
  224. }
  225. }
  226. /*
  227. * Get the signal to deliver. During the call to get_signal the
  228. * debugger may change all our registers so we may need to revert
  229. * the decision to restart the syscall; specifically, if the PC is
  230. * changed, don't restart the syscall.
  231. */
  232. if (get_signal(&ksig)) {
  233. if (unlikely(restart) && regs->pc == restart_addr) {
  234. if (retval == -ERESTARTNOHAND ||
  235. retval == -ERESTART_RESTARTBLOCK
  236. || (retval == -ERESTARTSYS
  237. && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
  238. /* No automatic restart */
  239. regs->gpr[11] = -EINTR;
  240. regs->pc = continue_addr;
  241. }
  242. }
  243. handle_signal(&ksig, regs);
  244. } else {
  245. /* no handler */
  246. restore_saved_sigmask();
  247. /*
  248. * Restore pt_regs PC as syscall restart will be handled by
  249. * kernel without return to userspace
  250. */
  251. if (unlikely(restart) && regs->pc == restart_addr) {
  252. regs->pc = continue_addr;
  253. return restart;
  254. }
  255. }
  256. return 0;
  257. }
  258. asmlinkage int
  259. do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  260. {
  261. do {
  262. if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  263. schedule();
  264. } else {
  265. if (unlikely(!user_mode(regs)))
  266. return 0;
  267. local_irq_enable();
  268. if (thread_flags & _TIF_SIGPENDING) {
  269. int restart = do_signal(regs, syscall);
  270. if (unlikely(restart)) {
  271. /*
  272. * Restart without handlers.
  273. * Deal with it without leaving
  274. * the kernel space.
  275. */
  276. return restart;
  277. }
  278. syscall = 0;
  279. } else {
  280. clear_thread_flag(TIF_NOTIFY_RESUME);
  281. tracehook_notify_resume(regs);
  282. }
  283. }
  284. local_irq_disable();
  285. thread_flags = current_thread_info()->flags;
  286. } while (thread_flags & _TIF_WORK_MASK);
  287. return 0;
  288. }