signal32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. */
  10. #include <linux/cache.h>
  11. #include <linux/compat.h>
  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/syscalls.h>
  18. #include <linux/errno.h>
  19. #include <linux/wait.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/suspend.h>
  22. #include <linux/compiler.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/abi.h>
  25. #include <asm/asm.h>
  26. #include <asm/compat-signal.h>
  27. #include <linux/bitops.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/sim.h>
  30. #include <asm/ucontext.h>
  31. #include <asm/fpu.h>
  32. #include <asm/war.h>
  33. #include <asm/dsp.h>
  34. #include "signal-common.h"
  35. /*
  36. * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
  37. */
  38. #define __NR_O32_restart_syscall 4253
  39. /* 32-bit compatibility types */
  40. typedef unsigned int __sighandler32_t;
  41. typedef void (*vfptr_t)(void);
  42. struct ucontext32 {
  43. u32 uc_flags;
  44. s32 uc_link;
  45. compat_stack_t uc_stack;
  46. struct sigcontext32 uc_mcontext;
  47. compat_sigset_t uc_sigmask; /* mask last for extensibility */
  48. };
  49. struct sigframe32 {
  50. u32 sf_ass[4]; /* argument save space for o32 */
  51. u32 sf_pad[2]; /* Was: signal trampoline */
  52. struct sigcontext32 sf_sc;
  53. compat_sigset_t sf_mask;
  54. };
  55. struct rt_sigframe32 {
  56. u32 rs_ass[4]; /* argument save space for o32 */
  57. u32 rs_pad[2]; /* Was: signal trampoline */
  58. compat_siginfo_t rs_info;
  59. struct ucontext32 rs_uc;
  60. };
  61. static int setup_sigcontext32(struct pt_regs *regs,
  62. struct sigcontext32 __user *sc)
  63. {
  64. int err = 0;
  65. int i;
  66. err |= __put_user(regs->cp0_epc, &sc->sc_pc);
  67. err |= __put_user(0, &sc->sc_regs[0]);
  68. for (i = 1; i < 32; i++)
  69. err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
  70. err |= __put_user(regs->hi, &sc->sc_mdhi);
  71. err |= __put_user(regs->lo, &sc->sc_mdlo);
  72. if (cpu_has_dsp) {
  73. err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
  74. err |= __put_user(mfhi1(), &sc->sc_hi1);
  75. err |= __put_user(mflo1(), &sc->sc_lo1);
  76. err |= __put_user(mfhi2(), &sc->sc_hi2);
  77. err |= __put_user(mflo2(), &sc->sc_lo2);
  78. err |= __put_user(mfhi3(), &sc->sc_hi3);
  79. err |= __put_user(mflo3(), &sc->sc_lo3);
  80. }
  81. /*
  82. * Save FPU state to signal context. Signal handler
  83. * will "inherit" current FPU state.
  84. */
  85. err |= protected_save_fp_context(sc);
  86. return err;
  87. }
  88. static int restore_sigcontext32(struct pt_regs *regs,
  89. struct sigcontext32 __user *sc)
  90. {
  91. int err = 0;
  92. s32 treg;
  93. int i;
  94. /* Always make any pending restarted system calls return -EINTR */
  95. current->restart_block.fn = do_no_restart_syscall;
  96. err |= __get_user(regs->cp0_epc, &sc->sc_pc);
  97. err |= __get_user(regs->hi, &sc->sc_mdhi);
  98. err |= __get_user(regs->lo, &sc->sc_mdlo);
  99. if (cpu_has_dsp) {
  100. err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
  101. err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
  102. err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
  103. err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
  104. err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
  105. err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
  106. err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
  107. }
  108. for (i = 1; i < 32; i++)
  109. err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
  110. return err ?: protected_restore_fp_context(sc);
  111. }
  112. /*
  113. * Atomically swap in the new signal mask, and wait for a signal.
  114. */
  115. asmlinkage int sys32_sigsuspend(compat_sigset_t __user *uset)
  116. {
  117. return compat_sys_rt_sigsuspend(uset, sizeof(compat_sigset_t));
  118. }
  119. SYSCALL_DEFINE3(32_sigaction, long, sig, const struct compat_sigaction __user *, act,
  120. struct compat_sigaction __user *, oact)
  121. {
  122. struct k_sigaction new_ka, old_ka;
  123. int ret;
  124. int err = 0;
  125. if (act) {
  126. old_sigset_t mask;
  127. s32 handler;
  128. if (!access_ok(VERIFY_READ, act, sizeof(*act)))
  129. return -EFAULT;
  130. err |= __get_user(handler, &act->sa_handler);
  131. new_ka.sa.sa_handler = (void __user *)(s64)handler;
  132. err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  133. err |= __get_user(mask, &act->sa_mask.sig[0]);
  134. if (err)
  135. return -EFAULT;
  136. siginitset(&new_ka.sa.sa_mask, mask);
  137. }
  138. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  139. if (!ret && oact) {
  140. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
  141. return -EFAULT;
  142. err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  143. err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
  144. &oact->sa_handler);
  145. err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
  146. err |= __put_user(0, &oact->sa_mask.sig[1]);
  147. err |= __put_user(0, &oact->sa_mask.sig[2]);
  148. err |= __put_user(0, &oact->sa_mask.sig[3]);
  149. if (err)
  150. return -EFAULT;
  151. }
  152. return ret;
  153. }
  154. int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
  155. {
  156. int err;
  157. if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
  158. return -EFAULT;
  159. /* If you change siginfo_t structure, please be sure
  160. this code is fixed accordingly.
  161. It should never copy any pad contained in the structure
  162. to avoid security leaks, but must copy the generic
  163. 3 ints plus the relevant union member.
  164. This routine must convert siginfo from 64bit to 32bit as well
  165. at the same time. */
  166. err = __put_user(from->si_signo, &to->si_signo);
  167. err |= __put_user(from->si_errno, &to->si_errno);
  168. err |= __put_user((short)from->si_code, &to->si_code);
  169. if (from->si_code < 0)
  170. err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
  171. else {
  172. switch (from->si_code >> 16) {
  173. case __SI_TIMER >> 16:
  174. err |= __put_user(from->si_tid, &to->si_tid);
  175. err |= __put_user(from->si_overrun, &to->si_overrun);
  176. err |= __put_user(from->si_int, &to->si_int);
  177. break;
  178. case __SI_CHLD >> 16:
  179. err |= __put_user(from->si_utime, &to->si_utime);
  180. err |= __put_user(from->si_stime, &to->si_stime);
  181. err |= __put_user(from->si_status, &to->si_status);
  182. default:
  183. err |= __put_user(from->si_pid, &to->si_pid);
  184. err |= __put_user(from->si_uid, &to->si_uid);
  185. break;
  186. case __SI_FAULT >> 16:
  187. err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
  188. break;
  189. case __SI_POLL >> 16:
  190. err |= __put_user(from->si_band, &to->si_band);
  191. err |= __put_user(from->si_fd, &to->si_fd);
  192. break;
  193. case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
  194. case __SI_MESGQ >> 16:
  195. err |= __put_user(from->si_pid, &to->si_pid);
  196. err |= __put_user(from->si_uid, &to->si_uid);
  197. err |= __put_user(from->si_int, &to->si_int);
  198. break;
  199. }
  200. }
  201. return err;
  202. }
  203. int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
  204. {
  205. if (copy_from_user(to, from, 3*sizeof(int)) ||
  206. copy_from_user(to->_sifields._pad,
  207. from->_sifields._pad, SI_PAD_SIZE32))
  208. return -EFAULT;
  209. return 0;
  210. }
  211. asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
  212. {
  213. struct sigframe32 __user *frame;
  214. sigset_t blocked;
  215. int sig;
  216. frame = (struct sigframe32 __user *) regs.regs[29];
  217. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  218. goto badframe;
  219. if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
  220. goto badframe;
  221. set_current_blocked(&blocked);
  222. sig = restore_sigcontext32(&regs, &frame->sf_sc);
  223. if (sig < 0)
  224. goto badframe;
  225. else if (sig)
  226. force_sig(sig, current);
  227. /*
  228. * Don't let your children do this ...
  229. */
  230. __asm__ __volatile__(
  231. "move\t$29, %0\n\t"
  232. "j\tsyscall_exit"
  233. :/* no outputs */
  234. :"r" (&regs));
  235. /* Unreached */
  236. badframe:
  237. force_sig(SIGSEGV, current);
  238. }
  239. asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
  240. {
  241. struct rt_sigframe32 __user *frame;
  242. sigset_t set;
  243. int sig;
  244. frame = (struct rt_sigframe32 __user *) regs.regs[29];
  245. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  246. goto badframe;
  247. if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
  248. goto badframe;
  249. set_current_blocked(&set);
  250. sig = restore_sigcontext32(&regs, &frame->rs_uc.uc_mcontext);
  251. if (sig < 0)
  252. goto badframe;
  253. else if (sig)
  254. force_sig(sig, current);
  255. if (compat_restore_altstack(&frame->rs_uc.uc_stack))
  256. goto badframe;
  257. /*
  258. * Don't let your children do this ...
  259. */
  260. __asm__ __volatile__(
  261. "move\t$29, %0\n\t"
  262. "j\tsyscall_exit"
  263. :/* no outputs */
  264. :"r" (&regs));
  265. /* Unreached */
  266. badframe:
  267. force_sig(SIGSEGV, current);
  268. }
  269. static int setup_frame_32(void *sig_return, struct ksignal *ksig,
  270. struct pt_regs *regs, sigset_t *set)
  271. {
  272. struct sigframe32 __user *frame;
  273. int err = 0;
  274. frame = get_sigframe(ksig, regs, sizeof(*frame));
  275. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  276. return -EFAULT;
  277. err |= setup_sigcontext32(regs, &frame->sf_sc);
  278. err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
  279. if (err)
  280. return -EFAULT;
  281. /*
  282. * Arguments to signal handler:
  283. *
  284. * a0 = signal number
  285. * a1 = 0 (should be cause)
  286. * a2 = pointer to struct sigcontext
  287. *
  288. * $25 and c0_epc point to the signal handler, $29 points to the
  289. * struct sigframe.
  290. */
  291. regs->regs[ 4] = ksig->sig;
  292. regs->regs[ 5] = 0;
  293. regs->regs[ 6] = (unsigned long) &frame->sf_sc;
  294. regs->regs[29] = (unsigned long) frame;
  295. regs->regs[31] = (unsigned long) sig_return;
  296. regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
  297. DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
  298. current->comm, current->pid,
  299. frame, regs->cp0_epc, regs->regs[31]);
  300. return 0;
  301. }
  302. static int setup_rt_frame_32(void *sig_return, struct ksignal *ksig,
  303. struct pt_regs *regs, sigset_t *set)
  304. {
  305. struct rt_sigframe32 __user *frame;
  306. int err = 0;
  307. frame = get_sigframe(ksig, regs, sizeof(*frame));
  308. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  309. return -EFAULT;
  310. /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
  311. err |= copy_siginfo_to_user32(&frame->rs_info, &ksig->info);
  312. /* Create the ucontext. */
  313. err |= __put_user(0, &frame->rs_uc.uc_flags);
  314. err |= __put_user(0, &frame->rs_uc.uc_link);
  315. err |= __compat_save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
  316. err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
  317. err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
  318. if (err)
  319. return -EFAULT;
  320. /*
  321. * Arguments to signal handler:
  322. *
  323. * a0 = signal number
  324. * a1 = 0 (should be cause)
  325. * a2 = pointer to ucontext
  326. *
  327. * $25 and c0_epc point to the signal handler, $29 points to
  328. * the struct rt_sigframe32.
  329. */
  330. regs->regs[ 4] = ksig->sig;
  331. regs->regs[ 5] = (unsigned long) &frame->rs_info;
  332. regs->regs[ 6] = (unsigned long) &frame->rs_uc;
  333. regs->regs[29] = (unsigned long) frame;
  334. regs->regs[31] = (unsigned long) sig_return;
  335. regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
  336. DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
  337. current->comm, current->pid,
  338. frame, regs->cp0_epc, regs->regs[31]);
  339. return 0;
  340. }
  341. /*
  342. * o32 compatibility on 64-bit kernels, without DSP ASE
  343. */
  344. struct mips_abi mips_abi_32 = {
  345. .setup_frame = setup_frame_32,
  346. .setup_rt_frame = setup_rt_frame_32,
  347. .restart = __NR_O32_restart_syscall,
  348. .off_sc_fpregs = offsetof(struct sigcontext32, sc_fpregs),
  349. .off_sc_fpc_csr = offsetof(struct sigcontext32, sc_fpc_csr),
  350. .off_sc_used_math = offsetof(struct sigcontext32, sc_used_math),
  351. .vdso = &vdso_image_o32,
  352. };