signal.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * arch/score/kernel/signal.c
  3. *
  4. * Score Processor version.
  5. *
  6. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  7. * Chen Liqin <liqin.chen@sunplusct.com>
  8. * Lennox Wu <lennox.wu@sunplusct.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see the file COPYING, or write
  22. * to the Free Software Foundation, Inc.,
  23. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <linux/errno.h>
  26. #include <linux/signal.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/unistd.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/syscalls.h>
  32. #include <asm/ucontext.h>
  33. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  34. struct rt_sigframe {
  35. u32 rs_ass[4]; /* argument save space */
  36. u32 rs_code[2]; /* signal trampoline */
  37. struct siginfo rs_info;
  38. struct ucontext rs_uc;
  39. };
  40. static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  41. {
  42. int err = 0;
  43. unsigned long reg;
  44. reg = regs->cp0_epc; err |= __put_user(reg, &sc->sc_pc);
  45. err |= __put_user(regs->cp0_psr, &sc->sc_psr);
  46. err |= __put_user(regs->cp0_condition, &sc->sc_condition);
  47. #define save_gp_reg(i) { \
  48. reg = regs->regs[i]; \
  49. err |= __put_user(reg, &sc->sc_regs[i]); \
  50. } while (0)
  51. save_gp_reg(0); save_gp_reg(1); save_gp_reg(2);
  52. save_gp_reg(3); save_gp_reg(4); save_gp_reg(5);
  53. save_gp_reg(6); save_gp_reg(7); save_gp_reg(8);
  54. save_gp_reg(9); save_gp_reg(10); save_gp_reg(11);
  55. save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
  56. save_gp_reg(15); save_gp_reg(16); save_gp_reg(17);
  57. save_gp_reg(18); save_gp_reg(19); save_gp_reg(20);
  58. save_gp_reg(21); save_gp_reg(22); save_gp_reg(23);
  59. save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
  60. save_gp_reg(27); save_gp_reg(28); save_gp_reg(29);
  61. #undef save_gp_reg
  62. reg = regs->ceh; err |= __put_user(reg, &sc->sc_mdceh);
  63. reg = regs->cel; err |= __put_user(reg, &sc->sc_mdcel);
  64. err |= __put_user(regs->cp0_ecr, &sc->sc_ecr);
  65. err |= __put_user(regs->cp0_ema, &sc->sc_ema);
  66. return err;
  67. }
  68. static int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  69. {
  70. int err = 0;
  71. u32 reg;
  72. err |= __get_user(regs->cp0_epc, &sc->sc_pc);
  73. err |= __get_user(regs->cp0_condition, &sc->sc_condition);
  74. err |= __get_user(reg, &sc->sc_mdceh);
  75. regs->ceh = (int) reg;
  76. err |= __get_user(reg, &sc->sc_mdcel);
  77. regs->cel = (int) reg;
  78. err |= __get_user(reg, &sc->sc_psr);
  79. regs->cp0_psr = (int) reg;
  80. err |= __get_user(reg, &sc->sc_ecr);
  81. regs->cp0_ecr = (int) reg;
  82. err |= __get_user(reg, &sc->sc_ema);
  83. regs->cp0_ema = (int) reg;
  84. #define restore_gp_reg(i) do { \
  85. err |= __get_user(reg, &sc->sc_regs[i]); \
  86. regs->regs[i] = reg; \
  87. } while (0)
  88. restore_gp_reg(0); restore_gp_reg(1); restore_gp_reg(2);
  89. restore_gp_reg(3); restore_gp_reg(4); restore_gp_reg(5);
  90. restore_gp_reg(6); restore_gp_reg(7); restore_gp_reg(8);
  91. restore_gp_reg(9); restore_gp_reg(10); restore_gp_reg(11);
  92. restore_gp_reg(12); restore_gp_reg(13); restore_gp_reg(14);
  93. restore_gp_reg(15); restore_gp_reg(16); restore_gp_reg(17);
  94. restore_gp_reg(18); restore_gp_reg(19); restore_gp_reg(20);
  95. restore_gp_reg(21); restore_gp_reg(22); restore_gp_reg(23);
  96. restore_gp_reg(24); restore_gp_reg(25); restore_gp_reg(26);
  97. restore_gp_reg(27); restore_gp_reg(28); restore_gp_reg(29);
  98. #undef restore_gp_reg
  99. return err;
  100. }
  101. /*
  102. * Determine which stack to use..
  103. */
  104. static void __user *get_sigframe(struct k_sigaction *ka,
  105. struct pt_regs *regs, size_t frame_size)
  106. {
  107. unsigned long sp;
  108. /* Default to using normal stack */
  109. sp = regs->regs[0];
  110. sp -= 32;
  111. /* This is the X/Open sanctioned signal stack switching. */
  112. if ((ka->sa.sa_flags & SA_ONSTACK) && (!on_sig_stack(sp)))
  113. sp = current->sas_ss_sp + current->sas_ss_size;
  114. return (void __user*)((sp - frame_size) & ~7);
  115. }
  116. asmlinkage long
  117. score_sigaltstack(struct pt_regs *regs)
  118. {
  119. const stack_t __user *uss = (const stack_t __user *) regs->regs[4];
  120. stack_t __user *uoss = (stack_t __user *) regs->regs[5];
  121. unsigned long usp = regs->regs[0];
  122. return do_sigaltstack(uss, uoss, usp);
  123. }
  124. asmlinkage long
  125. score_rt_sigreturn(struct pt_regs *regs)
  126. {
  127. struct rt_sigframe __user *frame;
  128. sigset_t set;
  129. stack_t st;
  130. int sig;
  131. frame = (struct rt_sigframe __user *) regs->regs[0];
  132. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  133. goto badframe;
  134. if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
  135. goto badframe;
  136. sigdelsetmask(&set, ~_BLOCKABLE);
  137. spin_lock_irq(&current->sighand->siglock);
  138. current->blocked = set;
  139. recalc_sigpending();
  140. spin_unlock_irq(&current->sighand->siglock);
  141. sig = restore_sigcontext(regs, &frame->rs_uc.uc_mcontext);
  142. if (sig < 0)
  143. goto badframe;
  144. else if (sig)
  145. force_sig(sig, current);
  146. if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
  147. goto badframe;
  148. /* It is more difficult to avoid calling this function than to
  149. call it and ignore errors. */
  150. do_sigaltstack((stack_t __user *)&st, NULL, regs->regs[0]);
  151. __asm__ __volatile__(
  152. "mv\tr0, %0\n\t"
  153. "la\tr8, syscall_exit\n\t"
  154. "br\tr8\n\t"
  155. : : "r" (regs) : "r8");
  156. badframe:
  157. force_sig(SIGSEGV, current);
  158. return 0;
  159. }
  160. static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
  161. int signr, sigset_t *set, siginfo_t *info)
  162. {
  163. struct rt_sigframe __user *frame;
  164. int err = 0;
  165. frame = get_sigframe(ka, regs, sizeof(*frame));
  166. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  167. goto give_sigsegv;
  168. /*
  169. * Set up the return code ...
  170. *
  171. * li v0, __NR_rt_sigreturn
  172. * syscall
  173. */
  174. err |= __put_user(0x87788000 + __NR_rt_sigreturn*2,
  175. frame->rs_code + 0);
  176. err |= __put_user(0x80008002, frame->rs_code + 1);
  177. flush_cache_sigtramp((unsigned long) frame->rs_code);
  178. err |= copy_siginfo_to_user(&frame->rs_info, info);
  179. err |= __put_user(0, &frame->rs_uc.uc_flags);
  180. err |= __put_user(NULL, &frame->rs_uc.uc_link);
  181. err |= __put_user((void __user *)current->sas_ss_sp,
  182. &frame->rs_uc.uc_stack.ss_sp);
  183. err |= __put_user(sas_ss_flags(regs->regs[0]),
  184. &frame->rs_uc.uc_stack.ss_flags);
  185. err |= __put_user(current->sas_ss_size,
  186. &frame->rs_uc.uc_stack.ss_size);
  187. err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
  188. err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
  189. if (err)
  190. goto give_sigsegv;
  191. regs->regs[0] = (unsigned long) frame;
  192. regs->regs[3] = (unsigned long) frame->rs_code;
  193. regs->regs[4] = signr;
  194. regs->regs[5] = (unsigned long) &frame->rs_info;
  195. regs->regs[6] = (unsigned long) &frame->rs_uc;
  196. regs->regs[29] = (unsigned long) ka->sa.sa_handler;
  197. regs->cp0_epc = (unsigned long) ka->sa.sa_handler;
  198. return 0;
  199. give_sigsegv:
  200. if (signr == SIGSEGV)
  201. ka->sa.sa_handler = SIG_DFL;
  202. force_sig(SIGSEGV, current);
  203. return -EFAULT;
  204. }
  205. static int handle_signal(unsigned long sig, siginfo_t *info,
  206. struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
  207. {
  208. int ret;
  209. if (regs->is_syscall) {
  210. switch (regs->regs[4]) {
  211. case ERESTART_RESTARTBLOCK:
  212. case ERESTARTNOHAND:
  213. regs->regs[4] = EINTR;
  214. break;
  215. case ERESTARTSYS:
  216. if (!(ka->sa.sa_flags & SA_RESTART)) {
  217. regs->regs[4] = EINTR;
  218. break;
  219. }
  220. case ERESTARTNOINTR:
  221. regs->regs[4] = regs->orig_r4;
  222. regs->regs[7] = regs->orig_r7;
  223. regs->cp0_epc -= 8;
  224. }
  225. regs->is_syscall = 0;
  226. }
  227. /*
  228. * Set up the stack frame
  229. */
  230. ret = setup_rt_frame(ka, regs, sig, oldset, info);
  231. spin_lock_irq(&current->sighand->siglock);
  232. sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
  233. if (!(ka->sa.sa_flags & SA_NODEFER))
  234. sigaddset(&current->blocked, sig);
  235. recalc_sigpending();
  236. spin_unlock_irq(&current->sighand->siglock);
  237. return ret;
  238. }
  239. static void do_signal(struct pt_regs *regs)
  240. {
  241. struct k_sigaction ka;
  242. sigset_t *oldset;
  243. siginfo_t info;
  244. int signr;
  245. /*
  246. * We want the common case to go fast, which is why we may in certain
  247. * cases get here from kernel mode. Just return without doing anything
  248. * if so.
  249. */
  250. if (!user_mode(regs))
  251. return;
  252. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  253. oldset = &current->saved_sigmask;
  254. else
  255. oldset = &current->blocked;
  256. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  257. if (signr > 0) {
  258. /* Actually deliver the signal. */
  259. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  260. /*
  261. * A signal was successfully delivered; the saved
  262. * sigmask will have been stored in the signal frame,
  263. * and will be restored by sigreturn, so we can simply
  264. * clear the TIF_RESTORE_SIGMASK flag.
  265. */
  266. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  267. clear_thread_flag(TIF_RESTORE_SIGMASK);
  268. }
  269. return;
  270. }
  271. if (regs->is_syscall) {
  272. if (regs->regs[4] == ERESTARTNOHAND ||
  273. regs->regs[4] == ERESTARTSYS ||
  274. regs->regs[4] == ERESTARTNOINTR) {
  275. regs->regs[4] = regs->orig_r4;
  276. regs->regs[7] = regs->orig_r7;
  277. regs->cp0_epc -= 8;
  278. }
  279. if (regs->regs[4] == ERESTART_RESTARTBLOCK) {
  280. regs->regs[27] = __NR_restart_syscall;
  281. regs->regs[4] = regs->orig_r4;
  282. regs->regs[7] = regs->orig_r7;
  283. regs->cp0_epc -= 8;
  284. }
  285. regs->is_syscall = 0; /* Don't deal with this again. */
  286. }
  287. /*
  288. * If there's no signal to deliver, we just put the saved sigmask
  289. * back
  290. */
  291. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  292. clear_thread_flag(TIF_RESTORE_SIGMASK);
  293. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  294. }
  295. }
  296. /*
  297. * notification of userspace execution resumption
  298. * - triggered by the TIF_WORK_MASK flags
  299. */
  300. asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
  301. __u32 thread_info_flags)
  302. {
  303. /* deal with pending signal delivery */
  304. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
  305. do_signal(regs);
  306. }