signal_o32.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. * Copyright (C) 1994 - 2000, 2006 Ralf Baechle
  8. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  9. * Copyright (C) 2016, Imagination Technologies Ltd.
  10. */
  11. #include <linux/compiler.h>
  12. #include <linux/errno.h>
  13. #include <linux/signal.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/abi.h>
  16. #include <asm/compat-signal.h>
  17. #include <asm/dsp.h>
  18. #include <asm/sim.h>
  19. #include <asm/unistd.h>
  20. #include "signal-common.h"
  21. /*
  22. * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
  23. */
  24. #define __NR_O32_restart_syscall 4253
  25. struct sigframe32 {
  26. u32 sf_ass[4]; /* argument save space for o32 */
  27. u32 sf_pad[2]; /* Was: signal trampoline */
  28. struct sigcontext32 sf_sc;
  29. compat_sigset_t sf_mask;
  30. };
  31. struct ucontext32 {
  32. u32 uc_flags;
  33. s32 uc_link;
  34. compat_stack_t uc_stack;
  35. struct sigcontext32 uc_mcontext;
  36. compat_sigset_t uc_sigmask; /* mask last for extensibility */
  37. };
  38. struct rt_sigframe32 {
  39. u32 rs_ass[4]; /* argument save space for o32 */
  40. u32 rs_pad[2]; /* Was: signal trampoline */
  41. compat_siginfo_t rs_info;
  42. struct ucontext32 rs_uc;
  43. };
  44. static int setup_sigcontext32(struct pt_regs *regs,
  45. struct sigcontext32 __user *sc)
  46. {
  47. int err = 0;
  48. int i;
  49. err |= __put_user(regs->cp0_epc, &sc->sc_pc);
  50. err |= __put_user(0, &sc->sc_regs[0]);
  51. for (i = 1; i < 32; i++)
  52. err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
  53. err |= __put_user(regs->hi, &sc->sc_mdhi);
  54. err |= __put_user(regs->lo, &sc->sc_mdlo);
  55. if (cpu_has_dsp) {
  56. err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
  57. err |= __put_user(mfhi1(), &sc->sc_hi1);
  58. err |= __put_user(mflo1(), &sc->sc_lo1);
  59. err |= __put_user(mfhi2(), &sc->sc_hi2);
  60. err |= __put_user(mflo2(), &sc->sc_lo2);
  61. err |= __put_user(mfhi3(), &sc->sc_hi3);
  62. err |= __put_user(mflo3(), &sc->sc_lo3);
  63. }
  64. /*
  65. * Save FPU state to signal context. Signal handler
  66. * will "inherit" current FPU state.
  67. */
  68. err |= protected_save_fp_context(sc);
  69. return err;
  70. }
  71. static int restore_sigcontext32(struct pt_regs *regs,
  72. struct sigcontext32 __user *sc)
  73. {
  74. int err = 0;
  75. s32 treg;
  76. int i;
  77. /* Always make any pending restarted system calls return -EINTR */
  78. current->restart_block.fn = do_no_restart_syscall;
  79. err |= __get_user(regs->cp0_epc, &sc->sc_pc);
  80. err |= __get_user(regs->hi, &sc->sc_mdhi);
  81. err |= __get_user(regs->lo, &sc->sc_mdlo);
  82. if (cpu_has_dsp) {
  83. err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
  84. err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
  85. err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
  86. err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
  87. err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
  88. err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
  89. err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
  90. }
  91. for (i = 1; i < 32; i++)
  92. err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
  93. return err ?: protected_restore_fp_context(sc);
  94. }
  95. static int setup_frame_32(void *sig_return, struct ksignal *ksig,
  96. struct pt_regs *regs, sigset_t *set)
  97. {
  98. struct sigframe32 __user *frame;
  99. int err = 0;
  100. frame = get_sigframe(ksig, regs, sizeof(*frame));
  101. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  102. return -EFAULT;
  103. err |= setup_sigcontext32(regs, &frame->sf_sc);
  104. err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
  105. if (err)
  106. return -EFAULT;
  107. /*
  108. * Arguments to signal handler:
  109. *
  110. * a0 = signal number
  111. * a1 = 0 (should be cause)
  112. * a2 = pointer to struct sigcontext
  113. *
  114. * $25 and c0_epc point to the signal handler, $29 points to the
  115. * struct sigframe.
  116. */
  117. regs->regs[ 4] = ksig->sig;
  118. regs->regs[ 5] = 0;
  119. regs->regs[ 6] = (unsigned long) &frame->sf_sc;
  120. regs->regs[29] = (unsigned long) frame;
  121. regs->regs[31] = (unsigned long) sig_return;
  122. regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
  123. DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
  124. current->comm, current->pid,
  125. frame, regs->cp0_epc, regs->regs[31]);
  126. return 0;
  127. }
  128. asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
  129. {
  130. struct rt_sigframe32 __user *frame;
  131. sigset_t set;
  132. int sig;
  133. frame = (struct rt_sigframe32 __user *) regs.regs[29];
  134. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  135. goto badframe;
  136. if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
  137. goto badframe;
  138. set_current_blocked(&set);
  139. sig = restore_sigcontext32(&regs, &frame->rs_uc.uc_mcontext);
  140. if (sig < 0)
  141. goto badframe;
  142. else if (sig)
  143. force_sig(sig, current);
  144. if (compat_restore_altstack(&frame->rs_uc.uc_stack))
  145. goto badframe;
  146. /*
  147. * Don't let your children do this ...
  148. */
  149. __asm__ __volatile__(
  150. "move\t$29, %0\n\t"
  151. "j\tsyscall_exit"
  152. :/* no outputs */
  153. :"r" (&regs));
  154. /* Unreached */
  155. badframe:
  156. force_sig(SIGSEGV, current);
  157. }
  158. static int setup_rt_frame_32(void *sig_return, struct ksignal *ksig,
  159. struct pt_regs *regs, sigset_t *set)
  160. {
  161. struct rt_sigframe32 __user *frame;
  162. int err = 0;
  163. frame = get_sigframe(ksig, regs, sizeof(*frame));
  164. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  165. return -EFAULT;
  166. /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
  167. err |= copy_siginfo_to_user32(&frame->rs_info, &ksig->info);
  168. /* Create the ucontext. */
  169. err |= __put_user(0, &frame->rs_uc.uc_flags);
  170. err |= __put_user(0, &frame->rs_uc.uc_link);
  171. err |= __compat_save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
  172. err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
  173. err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
  174. if (err)
  175. return -EFAULT;
  176. /*
  177. * Arguments to signal handler:
  178. *
  179. * a0 = signal number
  180. * a1 = 0 (should be cause)
  181. * a2 = pointer to ucontext
  182. *
  183. * $25 and c0_epc point to the signal handler, $29 points to
  184. * the struct rt_sigframe32.
  185. */
  186. regs->regs[ 4] = ksig->sig;
  187. regs->regs[ 5] = (unsigned long) &frame->rs_info;
  188. regs->regs[ 6] = (unsigned long) &frame->rs_uc;
  189. regs->regs[29] = (unsigned long) frame;
  190. regs->regs[31] = (unsigned long) sig_return;
  191. regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
  192. DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
  193. current->comm, current->pid,
  194. frame, regs->cp0_epc, regs->regs[31]);
  195. return 0;
  196. }
  197. /*
  198. * o32 compatibility on 64-bit kernels, without DSP ASE
  199. */
  200. struct mips_abi mips_abi_32 = {
  201. .setup_frame = setup_frame_32,
  202. .setup_rt_frame = setup_rt_frame_32,
  203. .restart = __NR_O32_restart_syscall,
  204. .off_sc_fpregs = offsetof(struct sigcontext32, sc_fpregs),
  205. .off_sc_fpc_csr = offsetof(struct sigcontext32, sc_fpc_csr),
  206. .off_sc_used_math = offsetof(struct sigcontext32, sc_used_math),
  207. .vdso = &vdso_image_o32,
  208. };
  209. asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
  210. {
  211. struct sigframe32 __user *frame;
  212. sigset_t blocked;
  213. int sig;
  214. frame = (struct sigframe32 __user *) regs.regs[29];
  215. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  216. goto badframe;
  217. if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
  218. goto badframe;
  219. set_current_blocked(&blocked);
  220. sig = restore_sigcontext32(&regs, &frame->sf_sc);
  221. if (sig < 0)
  222. goto badframe;
  223. else if (sig)
  224. force_sig(sig, current);
  225. /*
  226. * Don't let your children do this ...
  227. */
  228. __asm__ __volatile__(
  229. "move\t$29, %0\n\t"
  230. "j\tsyscall_exit"
  231. :/* no outputs */
  232. :"r" (&regs));
  233. /* Unreached */
  234. badframe:
  235. force_sig(SIGSEGV, current);
  236. }