signal32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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/vdso.h>
  34. #include <asm/dsp.h>
  35. #include "signal-common.h"
  36. static int (*save_fp_context32)(struct sigcontext32 __user *sc);
  37. static int (*restore_fp_context32)(struct sigcontext32 __user *sc);
  38. extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
  39. extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
  40. /*
  41. * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
  42. */
  43. #define __NR_O32_restart_syscall 4253
  44. /* 32-bit compatibility types */
  45. typedef unsigned int __sighandler32_t;
  46. typedef void (*vfptr_t)(void);
  47. struct ucontext32 {
  48. u32 uc_flags;
  49. s32 uc_link;
  50. compat_stack_t uc_stack;
  51. struct sigcontext32 uc_mcontext;
  52. compat_sigset_t uc_sigmask; /* mask last for extensibility */
  53. };
  54. struct sigframe32 {
  55. u32 sf_ass[4]; /* argument save space for o32 */
  56. u32 sf_pad[2]; /* Was: signal trampoline */
  57. struct sigcontext32 sf_sc;
  58. compat_sigset_t sf_mask;
  59. };
  60. struct rt_sigframe32 {
  61. u32 rs_ass[4]; /* argument save space for o32 */
  62. u32 rs_pad[2]; /* Was: signal trampoline */
  63. compat_siginfo_t rs_info;
  64. struct ucontext32 rs_uc;
  65. };
  66. /*
  67. * Thread saved context copy to/from a signal context presumed to be on the
  68. * user stack, and therefore accessed with appropriate macros from uaccess.h.
  69. */
  70. static int copy_fp_to_sigcontext32(struct sigcontext32 __user *sc)
  71. {
  72. int i;
  73. int err = 0;
  74. int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
  75. for (i = 0; i < NUM_FPU_REGS; i += inc) {
  76. err |=
  77. __put_user(get_fpr64(&current->thread.fpu.fpr[i], 0),
  78. &sc->sc_fpregs[i]);
  79. }
  80. err |= __put_user(current->thread.fpu.fcr31, &sc->sc_fpc_csr);
  81. return err;
  82. }
  83. static int copy_fp_from_sigcontext32(struct sigcontext32 __user *sc)
  84. {
  85. int i;
  86. int err = 0;
  87. int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
  88. u64 fpr_val;
  89. for (i = 0; i < NUM_FPU_REGS; i += inc) {
  90. err |= __get_user(fpr_val, &sc->sc_fpregs[i]);
  91. set_fpr64(&current->thread.fpu.fpr[i], 0, fpr_val);
  92. }
  93. err |= __get_user(current->thread.fpu.fcr31, &sc->sc_fpc_csr);
  94. return err;
  95. }
  96. /*
  97. * sigcontext handlers
  98. */
  99. static int protected_save_fp_context32(struct sigcontext32 __user *sc)
  100. {
  101. int err;
  102. while (1) {
  103. lock_fpu_owner();
  104. if (is_fpu_owner()) {
  105. err = save_fp_context32(sc);
  106. unlock_fpu_owner();
  107. } else {
  108. unlock_fpu_owner();
  109. err = copy_fp_to_sigcontext32(sc);
  110. }
  111. if (likely(!err))
  112. break;
  113. /* touch the sigcontext and try again */
  114. err = __put_user(0, &sc->sc_fpregs[0]) |
  115. __put_user(0, &sc->sc_fpregs[31]) |
  116. __put_user(0, &sc->sc_fpc_csr);
  117. if (err)
  118. break; /* really bad sigcontext */
  119. }
  120. return err;
  121. }
  122. static int protected_restore_fp_context32(struct sigcontext32 __user *sc)
  123. {
  124. int err, tmp __maybe_unused;
  125. while (1) {
  126. lock_fpu_owner();
  127. if (is_fpu_owner()) {
  128. err = restore_fp_context32(sc);
  129. unlock_fpu_owner();
  130. } else {
  131. unlock_fpu_owner();
  132. err = copy_fp_from_sigcontext32(sc);
  133. }
  134. if (likely(!err))
  135. break;
  136. /* touch the sigcontext and try again */
  137. err = __get_user(tmp, &sc->sc_fpregs[0]) |
  138. __get_user(tmp, &sc->sc_fpregs[31]) |
  139. __get_user(tmp, &sc->sc_fpc_csr);
  140. if (err)
  141. break; /* really bad sigcontext */
  142. }
  143. return err;
  144. }
  145. static int setup_sigcontext32(struct pt_regs *regs,
  146. struct sigcontext32 __user *sc)
  147. {
  148. int err = 0;
  149. int i;
  150. u32 used_math;
  151. err |= __put_user(regs->cp0_epc, &sc->sc_pc);
  152. err |= __put_user(0, &sc->sc_regs[0]);
  153. for (i = 1; i < 32; i++)
  154. err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
  155. err |= __put_user(regs->hi, &sc->sc_mdhi);
  156. err |= __put_user(regs->lo, &sc->sc_mdlo);
  157. if (cpu_has_dsp) {
  158. err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
  159. err |= __put_user(mfhi1(), &sc->sc_hi1);
  160. err |= __put_user(mflo1(), &sc->sc_lo1);
  161. err |= __put_user(mfhi2(), &sc->sc_hi2);
  162. err |= __put_user(mflo2(), &sc->sc_lo2);
  163. err |= __put_user(mfhi3(), &sc->sc_hi3);
  164. err |= __put_user(mflo3(), &sc->sc_lo3);
  165. }
  166. used_math = !!used_math();
  167. err |= __put_user(used_math, &sc->sc_used_math);
  168. if (used_math) {
  169. /*
  170. * Save FPU state to signal context. Signal handler
  171. * will "inherit" current FPU state.
  172. */
  173. err |= protected_save_fp_context32(sc);
  174. }
  175. return err;
  176. }
  177. static int
  178. check_and_restore_fp_context32(struct sigcontext32 __user *sc)
  179. {
  180. int err, sig;
  181. err = sig = fpcsr_pending(&sc->sc_fpc_csr);
  182. if (err > 0)
  183. err = 0;
  184. err |= protected_restore_fp_context32(sc);
  185. return err ?: sig;
  186. }
  187. static int restore_sigcontext32(struct pt_regs *regs,
  188. struct sigcontext32 __user *sc)
  189. {
  190. u32 used_math;
  191. int err = 0;
  192. s32 treg;
  193. int i;
  194. /* Always make any pending restarted system calls return -EINTR */
  195. current->restart_block.fn = do_no_restart_syscall;
  196. err |= __get_user(regs->cp0_epc, &sc->sc_pc);
  197. err |= __get_user(regs->hi, &sc->sc_mdhi);
  198. err |= __get_user(regs->lo, &sc->sc_mdlo);
  199. if (cpu_has_dsp) {
  200. err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
  201. err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
  202. err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
  203. err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
  204. err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
  205. err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
  206. err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
  207. }
  208. for (i = 1; i < 32; i++)
  209. err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
  210. err |= __get_user(used_math, &sc->sc_used_math);
  211. conditional_used_math(used_math);
  212. if (used_math) {
  213. /* restore fpu context if we have used it before */
  214. if (!err)
  215. err = check_and_restore_fp_context32(sc);
  216. } else {
  217. /* signal handler may have used FPU. Give it up. */
  218. lose_fpu(0);
  219. }
  220. return err;
  221. }
  222. /*
  223. *
  224. */
  225. extern void __put_sigset_unknown_nsig(void);
  226. extern void __get_sigset_unknown_nsig(void);
  227. static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t __user *ubuf)
  228. {
  229. int err = 0;
  230. if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
  231. return -EFAULT;
  232. switch (_NSIG_WORDS) {
  233. default:
  234. __put_sigset_unknown_nsig();
  235. case 2:
  236. err |= __put_user(kbuf->sig[1] >> 32, &ubuf->sig[3]);
  237. err |= __put_user(kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
  238. case 1:
  239. err |= __put_user(kbuf->sig[0] >> 32, &ubuf->sig[1]);
  240. err |= __put_user(kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
  241. }
  242. return err;
  243. }
  244. static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t __user *ubuf)
  245. {
  246. int err = 0;
  247. unsigned long sig[4];
  248. if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
  249. return -EFAULT;
  250. switch (_NSIG_WORDS) {
  251. default:
  252. __get_sigset_unknown_nsig();
  253. case 2:
  254. err |= __get_user(sig[3], &ubuf->sig[3]);
  255. err |= __get_user(sig[2], &ubuf->sig[2]);
  256. kbuf->sig[1] = sig[2] | (sig[3] << 32);
  257. case 1:
  258. err |= __get_user(sig[1], &ubuf->sig[1]);
  259. err |= __get_user(sig[0], &ubuf->sig[0]);
  260. kbuf->sig[0] = sig[0] | (sig[1] << 32);
  261. }
  262. return err;
  263. }
  264. /*
  265. * Atomically swap in the new signal mask, and wait for a signal.
  266. */
  267. asmlinkage int sys32_sigsuspend(compat_sigset_t __user *uset)
  268. {
  269. return compat_sys_rt_sigsuspend(uset, sizeof(compat_sigset_t));
  270. }
  271. SYSCALL_DEFINE3(32_sigaction, long, sig, const struct compat_sigaction __user *, act,
  272. struct compat_sigaction __user *, oact)
  273. {
  274. struct k_sigaction new_ka, old_ka;
  275. int ret;
  276. int err = 0;
  277. if (act) {
  278. old_sigset_t mask;
  279. s32 handler;
  280. if (!access_ok(VERIFY_READ, act, sizeof(*act)))
  281. return -EFAULT;
  282. err |= __get_user(handler, &act->sa_handler);
  283. new_ka.sa.sa_handler = (void __user *)(s64)handler;
  284. err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  285. err |= __get_user(mask, &act->sa_mask.sig[0]);
  286. if (err)
  287. return -EFAULT;
  288. siginitset(&new_ka.sa.sa_mask, mask);
  289. }
  290. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  291. if (!ret && oact) {
  292. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
  293. return -EFAULT;
  294. err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  295. err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
  296. &oact->sa_handler);
  297. err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
  298. err |= __put_user(0, &oact->sa_mask.sig[1]);
  299. err |= __put_user(0, &oact->sa_mask.sig[2]);
  300. err |= __put_user(0, &oact->sa_mask.sig[3]);
  301. if (err)
  302. return -EFAULT;
  303. }
  304. return ret;
  305. }
  306. int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
  307. {
  308. int err;
  309. if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
  310. return -EFAULT;
  311. /* If you change siginfo_t structure, please be sure
  312. this code is fixed accordingly.
  313. It should never copy any pad contained in the structure
  314. to avoid security leaks, but must copy the generic
  315. 3 ints plus the relevant union member.
  316. This routine must convert siginfo from 64bit to 32bit as well
  317. at the same time. */
  318. err = __put_user(from->si_signo, &to->si_signo);
  319. err |= __put_user(from->si_errno, &to->si_errno);
  320. err |= __put_user((short)from->si_code, &to->si_code);
  321. if (from->si_code < 0)
  322. err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
  323. else {
  324. switch (from->si_code >> 16) {
  325. case __SI_TIMER >> 16:
  326. err |= __put_user(from->si_tid, &to->si_tid);
  327. err |= __put_user(from->si_overrun, &to->si_overrun);
  328. err |= __put_user(from->si_int, &to->si_int);
  329. break;
  330. case __SI_CHLD >> 16:
  331. err |= __put_user(from->si_utime, &to->si_utime);
  332. err |= __put_user(from->si_stime, &to->si_stime);
  333. err |= __put_user(from->si_status, &to->si_status);
  334. default:
  335. err |= __put_user(from->si_pid, &to->si_pid);
  336. err |= __put_user(from->si_uid, &to->si_uid);
  337. break;
  338. case __SI_FAULT >> 16:
  339. err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
  340. break;
  341. case __SI_POLL >> 16:
  342. err |= __put_user(from->si_band, &to->si_band);
  343. err |= __put_user(from->si_fd, &to->si_fd);
  344. break;
  345. case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
  346. case __SI_MESGQ >> 16:
  347. err |= __put_user(from->si_pid, &to->si_pid);
  348. err |= __put_user(from->si_uid, &to->si_uid);
  349. err |= __put_user(from->si_int, &to->si_int);
  350. break;
  351. }
  352. }
  353. return err;
  354. }
  355. int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
  356. {
  357. memset(to, 0, sizeof *to);
  358. if (copy_from_user(to, from, 3*sizeof(int)) ||
  359. copy_from_user(to->_sifields._pad,
  360. from->_sifields._pad, SI_PAD_SIZE32))
  361. return -EFAULT;
  362. return 0;
  363. }
  364. asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
  365. {
  366. struct sigframe32 __user *frame;
  367. sigset_t blocked;
  368. int sig;
  369. frame = (struct sigframe32 __user *) regs.regs[29];
  370. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  371. goto badframe;
  372. if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
  373. goto badframe;
  374. set_current_blocked(&blocked);
  375. sig = restore_sigcontext32(&regs, &frame->sf_sc);
  376. if (sig < 0)
  377. goto badframe;
  378. else if (sig)
  379. force_sig(sig, current);
  380. /*
  381. * Don't let your children do this ...
  382. */
  383. __asm__ __volatile__(
  384. "move\t$29, %0\n\t"
  385. "j\tsyscall_exit"
  386. :/* no outputs */
  387. :"r" (&regs));
  388. /* Unreached */
  389. badframe:
  390. force_sig(SIGSEGV, current);
  391. }
  392. asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
  393. {
  394. struct rt_sigframe32 __user *frame;
  395. sigset_t set;
  396. int sig;
  397. frame = (struct rt_sigframe32 __user *) regs.regs[29];
  398. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  399. goto badframe;
  400. if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
  401. goto badframe;
  402. set_current_blocked(&set);
  403. sig = restore_sigcontext32(&regs, &frame->rs_uc.uc_mcontext);
  404. if (sig < 0)
  405. goto badframe;
  406. else if (sig)
  407. force_sig(sig, current);
  408. if (compat_restore_altstack(&frame->rs_uc.uc_stack))
  409. goto badframe;
  410. /*
  411. * Don't let your children do this ...
  412. */
  413. __asm__ __volatile__(
  414. "move\t$29, %0\n\t"
  415. "j\tsyscall_exit"
  416. :/* no outputs */
  417. :"r" (&regs));
  418. /* Unreached */
  419. badframe:
  420. force_sig(SIGSEGV, current);
  421. }
  422. static int setup_frame_32(void *sig_return, struct ksignal *ksig,
  423. struct pt_regs *regs, sigset_t *set)
  424. {
  425. struct sigframe32 __user *frame;
  426. int err = 0;
  427. frame = get_sigframe(ksig, regs, sizeof(*frame));
  428. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  429. return -EFAULT;
  430. err |= setup_sigcontext32(regs, &frame->sf_sc);
  431. err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
  432. if (err)
  433. return -EFAULT;
  434. /*
  435. * Arguments to signal handler:
  436. *
  437. * a0 = signal number
  438. * a1 = 0 (should be cause)
  439. * a2 = pointer to struct sigcontext
  440. *
  441. * $25 and c0_epc point to the signal handler, $29 points to the
  442. * struct sigframe.
  443. */
  444. regs->regs[ 4] = ksig->sig;
  445. regs->regs[ 5] = 0;
  446. regs->regs[ 6] = (unsigned long) &frame->sf_sc;
  447. regs->regs[29] = (unsigned long) frame;
  448. regs->regs[31] = (unsigned long) sig_return;
  449. regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
  450. DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
  451. current->comm, current->pid,
  452. frame, regs->cp0_epc, regs->regs[31]);
  453. return 0;
  454. }
  455. static int setup_rt_frame_32(void *sig_return, struct ksignal *ksig,
  456. struct pt_regs *regs, sigset_t *set)
  457. {
  458. struct rt_sigframe32 __user *frame;
  459. int err = 0;
  460. frame = get_sigframe(ksig, regs, sizeof(*frame));
  461. if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
  462. return -EFAULT;
  463. /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
  464. err |= copy_siginfo_to_user32(&frame->rs_info, &ksig->info);
  465. /* Create the ucontext. */
  466. err |= __put_user(0, &frame->rs_uc.uc_flags);
  467. err |= __put_user(0, &frame->rs_uc.uc_link);
  468. err |= __compat_save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
  469. err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
  470. err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
  471. if (err)
  472. return -EFAULT;
  473. /*
  474. * Arguments to signal handler:
  475. *
  476. * a0 = signal number
  477. * a1 = 0 (should be cause)
  478. * a2 = pointer to ucontext
  479. *
  480. * $25 and c0_epc point to the signal handler, $29 points to
  481. * the struct rt_sigframe32.
  482. */
  483. regs->regs[ 4] = ksig->sig;
  484. regs->regs[ 5] = (unsigned long) &frame->rs_info;
  485. regs->regs[ 6] = (unsigned long) &frame->rs_uc;
  486. regs->regs[29] = (unsigned long) frame;
  487. regs->regs[31] = (unsigned long) sig_return;
  488. regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
  489. DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
  490. current->comm, current->pid,
  491. frame, regs->cp0_epc, regs->regs[31]);
  492. return 0;
  493. }
  494. /*
  495. * o32 compatibility on 64-bit kernels, without DSP ASE
  496. */
  497. struct mips_abi mips_abi_32 = {
  498. .setup_frame = setup_frame_32,
  499. .signal_return_offset =
  500. offsetof(struct mips_vdso, o32_signal_trampoline),
  501. .setup_rt_frame = setup_rt_frame_32,
  502. .rt_signal_return_offset =
  503. offsetof(struct mips_vdso, o32_rt_signal_trampoline),
  504. .restart = __NR_O32_restart_syscall
  505. };
  506. static int signal32_init(void)
  507. {
  508. if (cpu_has_fpu) {
  509. save_fp_context32 = _save_fp_context32;
  510. restore_fp_context32 = _restore_fp_context32;
  511. } else {
  512. save_fp_context32 = copy_fp_to_sigcontext32;
  513. restore_fp_context32 = copy_fp_from_sigcontext32;
  514. }
  515. return 0;
  516. }
  517. arch_initcall(signal32_init);