signal.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 Ralf Baechle
  8. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  9. * Copyright (C) 2014, Imagination Technologies Ltd.
  10. */
  11. #include <linux/cache.h>
  12. #include <linux/context_tracking.h>
  13. #include <linux/irqflags.h>
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/personality.h>
  17. #include <linux/smp.h>
  18. #include <linux/kernel.h>
  19. #include <linux/signal.h>
  20. #include <linux/errno.h>
  21. #include <linux/wait.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/unistd.h>
  24. #include <linux/uprobes.h>
  25. #include <linux/compiler.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/resume_user_mode.h>
  29. #include <asm/abi.h>
  30. #include <asm/asm.h>
  31. #include <linux/bitops.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/fpu.h>
  34. #include <asm/sim.h>
  35. #include <asm/ucontext.h>
  36. #include <asm/cpu-features.h>
  37. #include <asm/dsp.h>
  38. #include <asm/inst.h>
  39. #include <asm/msa.h>
  40. #include <asm/syscalls.h>
  41. #include "signal-common.h"
  42. static int (*save_fp_context)(void __user *sc);
  43. static int (*restore_fp_context)(void __user *sc);
  44. struct sigframe {
  45. u32 sf_ass[4]; /* argument save space for o32 */
  46. u32 sf_pad[2]; /* Was: signal trampoline */
  47. /* Matches struct ucontext from its uc_mcontext field onwards */
  48. struct sigcontext sf_sc;
  49. sigset_t sf_mask;
  50. unsigned long long sf_extcontext[];
  51. };
  52. struct rt_sigframe {
  53. u32 rs_ass[4]; /* argument save space for o32 */
  54. u32 rs_pad[2]; /* Was: signal trampoline */
  55. struct siginfo rs_info;
  56. struct ucontext rs_uc;
  57. };
  58. #ifdef CONFIG_MIPS_FP_SUPPORT
  59. /*
  60. * Thread saved context copy to/from a signal context presumed to be on the
  61. * user stack, and therefore accessed with appropriate macros from uaccess.h.
  62. */
  63. static int copy_fp_to_sigcontext(void __user *sc)
  64. {
  65. struct mips_abi *abi = current->thread.abi;
  66. uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
  67. uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
  68. int i;
  69. int err = 0;
  70. int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
  71. for (i = 0; i < NUM_FPU_REGS; i += inc) {
  72. err |=
  73. __put_user(get_fpr64(&current->thread.fpu.fpr[i], 0),
  74. &fpregs[i]);
  75. }
  76. err |= __put_user(current->thread.fpu.fcr31, csr);
  77. return err;
  78. }
  79. static int copy_fp_from_sigcontext(void __user *sc)
  80. {
  81. struct mips_abi *abi = current->thread.abi;
  82. uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
  83. uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
  84. int i;
  85. int err = 0;
  86. int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
  87. u64 fpr_val;
  88. for (i = 0; i < NUM_FPU_REGS; i += inc) {
  89. err |= __get_user(fpr_val, &fpregs[i]);
  90. set_fpr64(&current->thread.fpu.fpr[i], 0, fpr_val);
  91. }
  92. err |= __get_user(current->thread.fpu.fcr31, csr);
  93. return err;
  94. }
  95. #else /* !CONFIG_MIPS_FP_SUPPORT */
  96. static int copy_fp_to_sigcontext(void __user *sc)
  97. {
  98. return 0;
  99. }
  100. static int copy_fp_from_sigcontext(void __user *sc)
  101. {
  102. return 0;
  103. }
  104. #endif /* !CONFIG_MIPS_FP_SUPPORT */
  105. /*
  106. * Wrappers for the assembly _{save,restore}_fp_context functions.
  107. */
  108. static int save_hw_fp_context(void __user *sc)
  109. {
  110. struct mips_abi *abi = current->thread.abi;
  111. uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
  112. uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
  113. return _save_fp_context(fpregs, csr);
  114. }
  115. static int restore_hw_fp_context(void __user *sc)
  116. {
  117. struct mips_abi *abi = current->thread.abi;
  118. uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
  119. uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
  120. return _restore_fp_context(fpregs, csr);
  121. }
  122. /*
  123. * Extended context handling.
  124. */
  125. static inline void __user *sc_to_extcontext(void __user *sc)
  126. {
  127. struct ucontext __user *uc;
  128. /*
  129. * We can just pretend the sigcontext is always embedded in a struct
  130. * ucontext here, because the offset from sigcontext to extended
  131. * context is the same in the struct sigframe case.
  132. */
  133. uc = container_of(sc, struct ucontext, uc_mcontext);
  134. return &uc->uc_extcontext;
  135. }
  136. #ifdef CONFIG_CPU_HAS_MSA
  137. static int save_msa_extcontext(void __user *buf)
  138. {
  139. struct msa_extcontext __user *msa = buf;
  140. uint64_t val;
  141. int i, err;
  142. if (!thread_msa_context_live())
  143. return 0;
  144. /*
  145. * Ensure that we can't lose the live MSA context between checking
  146. * for it & writing it to memory.
  147. */
  148. preempt_disable();
  149. if (is_msa_enabled()) {
  150. /*
  151. * There are no EVA versions of the vector register load/store
  152. * instructions, so MSA context has to be saved to kernel memory
  153. * and then copied to user memory. The save to kernel memory
  154. * should already have been done when handling scalar FP
  155. * context.
  156. */
  157. BUG_ON(IS_ENABLED(CONFIG_EVA));
  158. err = __put_user(read_msa_csr(), &msa->csr);
  159. err |= _save_msa_all_upper(&msa->wr);
  160. preempt_enable();
  161. } else {
  162. preempt_enable();
  163. err = __put_user(current->thread.fpu.msacsr, &msa->csr);
  164. for (i = 0; i < NUM_FPU_REGS; i++) {
  165. val = get_fpr64(&current->thread.fpu.fpr[i], 1);
  166. err |= __put_user(val, &msa->wr[i]);
  167. }
  168. }
  169. err |= __put_user(MSA_EXTCONTEXT_MAGIC, &msa->ext.magic);
  170. err |= __put_user(sizeof(*msa), &msa->ext.size);
  171. return err ? -EFAULT : sizeof(*msa);
  172. }
  173. static int restore_msa_extcontext(void __user *buf, unsigned int size)
  174. {
  175. struct msa_extcontext __user *msa = buf;
  176. unsigned long long val;
  177. unsigned int csr;
  178. int i, err;
  179. if (size != sizeof(*msa))
  180. return -EINVAL;
  181. err = get_user(csr, &msa->csr);
  182. if (err)
  183. return err;
  184. preempt_disable();
  185. if (is_msa_enabled()) {
  186. /*
  187. * There are no EVA versions of the vector register load/store
  188. * instructions, so MSA context has to be copied to kernel
  189. * memory and later loaded to registers. The same is true of
  190. * scalar FP context, so FPU & MSA should have already been
  191. * disabled whilst handling scalar FP context.
  192. */
  193. BUG_ON(IS_ENABLED(CONFIG_EVA));
  194. write_msa_csr(csr);
  195. err |= _restore_msa_all_upper(&msa->wr);
  196. preempt_enable();
  197. } else {
  198. preempt_enable();
  199. current->thread.fpu.msacsr = csr;
  200. for (i = 0; i < NUM_FPU_REGS; i++) {
  201. err |= __get_user(val, &msa->wr[i]);
  202. set_fpr64(&current->thread.fpu.fpr[i], 1, val);
  203. }
  204. }
  205. return err;
  206. }
  207. #else /* !CONFIG_CPU_HAS_MSA */
  208. static int save_msa_extcontext(void __user *buf)
  209. {
  210. return 0;
  211. }
  212. static int restore_msa_extcontext(void __user *buf, unsigned int size)
  213. {
  214. return SIGSYS;
  215. }
  216. #endif /* !CONFIG_CPU_HAS_MSA */
  217. static int save_extcontext(void __user *buf)
  218. {
  219. int sz;
  220. sz = save_msa_extcontext(buf);
  221. if (sz < 0)
  222. return sz;
  223. buf += sz;
  224. /* If no context was saved then trivially return */
  225. if (!sz)
  226. return 0;
  227. /* Write the end marker */
  228. if (__put_user(END_EXTCONTEXT_MAGIC, (u32 *)buf))
  229. return -EFAULT;
  230. sz += sizeof(((struct extcontext *)NULL)->magic);
  231. return sz;
  232. }
  233. static int restore_extcontext(void __user *buf)
  234. {
  235. struct extcontext ext;
  236. int err;
  237. while (1) {
  238. err = __get_user(ext.magic, (unsigned int *)buf);
  239. if (err)
  240. return err;
  241. if (ext.magic == END_EXTCONTEXT_MAGIC)
  242. return 0;
  243. err = __get_user(ext.size, (unsigned int *)(buf
  244. + offsetof(struct extcontext, size)));
  245. if (err)
  246. return err;
  247. switch (ext.magic) {
  248. case MSA_EXTCONTEXT_MAGIC:
  249. err = restore_msa_extcontext(buf, ext.size);
  250. break;
  251. default:
  252. err = -EINVAL;
  253. break;
  254. }
  255. if (err)
  256. return err;
  257. buf += ext.size;
  258. }
  259. }
  260. /*
  261. * Helper routines
  262. */
  263. int protected_save_fp_context(void __user *sc)
  264. {
  265. struct mips_abi *abi = current->thread.abi;
  266. uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
  267. uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
  268. uint32_t __user *used_math = sc + abi->off_sc_used_math;
  269. unsigned int used, ext_sz;
  270. int err;
  271. used = used_math() ? USED_FP : 0;
  272. if (!used)
  273. goto fp_done;
  274. if (!test_thread_flag(TIF_32BIT_FPREGS))
  275. used |= USED_FR1;
  276. if (test_thread_flag(TIF_HYBRID_FPREGS))
  277. used |= USED_HYBRID_FPRS;
  278. /*
  279. * EVA does not have userland equivalents of ldc1 or sdc1, so
  280. * save to the kernel FP context & copy that to userland below.
  281. */
  282. if (IS_ENABLED(CONFIG_EVA))
  283. lose_fpu(1);
  284. while (1) {
  285. lock_fpu_owner();
  286. if (is_fpu_owner()) {
  287. err = save_fp_context(sc);
  288. unlock_fpu_owner();
  289. } else {
  290. unlock_fpu_owner();
  291. err = copy_fp_to_sigcontext(sc);
  292. }
  293. if (likely(!err))
  294. break;
  295. /* touch the sigcontext and try again */
  296. err = __put_user(0, &fpregs[0]) |
  297. __put_user(0, &fpregs[31]) |
  298. __put_user(0, csr);
  299. if (err)
  300. return err; /* really bad sigcontext */
  301. }
  302. fp_done:
  303. ext_sz = err = save_extcontext(sc_to_extcontext(sc));
  304. if (err < 0)
  305. return err;
  306. used |= ext_sz ? USED_EXTCONTEXT : 0;
  307. return __put_user(used, used_math);
  308. }
  309. int protected_restore_fp_context(void __user *sc)
  310. {
  311. struct mips_abi *abi = current->thread.abi;
  312. uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
  313. uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
  314. uint32_t __user *used_math = sc + abi->off_sc_used_math;
  315. unsigned int used;
  316. int err, sig = 0, tmp __maybe_unused;
  317. err = __get_user(used, used_math);
  318. conditional_used_math(used & USED_FP);
  319. /*
  320. * The signal handler may have used FPU; give it up if the program
  321. * doesn't want it following sigreturn.
  322. */
  323. if (err || !(used & USED_FP))
  324. lose_fpu(0);
  325. if (err)
  326. return err;
  327. if (!(used & USED_FP))
  328. goto fp_done;
  329. err = sig = fpcsr_pending(csr);
  330. if (err < 0)
  331. return err;
  332. /*
  333. * EVA does not have userland equivalents of ldc1 or sdc1, so we
  334. * disable the FPU here such that the code below simply copies to
  335. * the kernel FP context.
  336. */
  337. if (IS_ENABLED(CONFIG_EVA))
  338. lose_fpu(0);
  339. while (1) {
  340. lock_fpu_owner();
  341. if (is_fpu_owner()) {
  342. err = restore_fp_context(sc);
  343. unlock_fpu_owner();
  344. } else {
  345. unlock_fpu_owner();
  346. err = copy_fp_from_sigcontext(sc);
  347. }
  348. if (likely(!err))
  349. break;
  350. /* touch the sigcontext and try again */
  351. err = __get_user(tmp, &fpregs[0]) |
  352. __get_user(tmp, &fpregs[31]) |
  353. __get_user(tmp, csr);
  354. if (err)
  355. break; /* really bad sigcontext */
  356. }
  357. fp_done:
  358. if (!err && (used & USED_EXTCONTEXT))
  359. err = restore_extcontext(sc_to_extcontext(sc));
  360. return err ?: sig;
  361. }
  362. int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  363. {
  364. int err = 0;
  365. int i;
  366. err |= __put_user(regs->cp0_epc, &sc->sc_pc);
  367. err |= __put_user(0, &sc->sc_regs[0]);
  368. for (i = 1; i < 32; i++)
  369. err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
  370. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  371. err |= __put_user(regs->acx, &sc->sc_acx);
  372. #endif
  373. err |= __put_user(regs->hi, &sc->sc_mdhi);
  374. err |= __put_user(regs->lo, &sc->sc_mdlo);
  375. if (cpu_has_dsp) {
  376. err |= __put_user(mfhi1(), &sc->sc_hi1);
  377. err |= __put_user(mflo1(), &sc->sc_lo1);
  378. err |= __put_user(mfhi2(), &sc->sc_hi2);
  379. err |= __put_user(mflo2(), &sc->sc_lo2);
  380. err |= __put_user(mfhi3(), &sc->sc_hi3);
  381. err |= __put_user(mflo3(), &sc->sc_lo3);
  382. err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
  383. }
  384. /*
  385. * Save FPU state to signal context. Signal handler
  386. * will "inherit" current FPU state.
  387. */
  388. err |= protected_save_fp_context(sc);
  389. return err;
  390. }
  391. static size_t extcontext_max_size(void)
  392. {
  393. size_t sz = 0;
  394. /*
  395. * The assumption here is that between this point & the point at which
  396. * the extended context is saved the size of the context should only
  397. * ever be able to shrink (if the task is preempted), but never grow.
  398. * That is, what this function returns is an upper bound on the size of
  399. * the extended context for the current task at the current time.
  400. */
  401. if (thread_msa_context_live())
  402. sz += sizeof(struct msa_extcontext);
  403. /* If any context is saved then we'll append the end marker */
  404. if (sz)
  405. sz += sizeof(((struct extcontext *)NULL)->magic);
  406. return sz;
  407. }
  408. int fpcsr_pending(unsigned int __user *fpcsr)
  409. {
  410. int err, sig = 0;
  411. unsigned int csr, enabled;
  412. err = __get_user(csr, fpcsr);
  413. enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
  414. /*
  415. * If the signal handler set some FPU exceptions, clear it and
  416. * send SIGFPE.
  417. */
  418. if (csr & enabled) {
  419. csr &= ~enabled;
  420. err |= __put_user(csr, fpcsr);
  421. sig = SIGFPE;
  422. }
  423. return err ?: sig;
  424. }
  425. int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  426. {
  427. unsigned long treg;
  428. int err = 0;
  429. int i;
  430. /* Always make any pending restarted system calls return -EINTR */
  431. current->restart_block.fn = do_no_restart_syscall;
  432. err |= __get_user(regs->cp0_epc, &sc->sc_pc);
  433. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  434. err |= __get_user(regs->acx, &sc->sc_acx);
  435. #endif
  436. err |= __get_user(regs->hi, &sc->sc_mdhi);
  437. err |= __get_user(regs->lo, &sc->sc_mdlo);
  438. if (cpu_has_dsp) {
  439. err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
  440. err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
  441. err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
  442. err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
  443. err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
  444. err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
  445. err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
  446. }
  447. for (i = 1; i < 32; i++)
  448. err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
  449. return err ?: protected_restore_fp_context(sc);
  450. }
  451. #ifdef CONFIG_WAR_ICACHE_REFILLS
  452. #define SIGMASK ~(cpu_icache_line_size()-1)
  453. #else
  454. #define SIGMASK ALMASK
  455. #endif
  456. void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
  457. size_t frame_size)
  458. {
  459. unsigned long sp;
  460. /* Leave space for potential extended context */
  461. frame_size += extcontext_max_size();
  462. /* Default to using normal stack */
  463. sp = regs->regs[29];
  464. /*
  465. * If we are on the alternate signal stack and would overflow it, don't.
  466. * Return an always-bogus address instead so we will die with SIGSEGV.
  467. */
  468. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
  469. return (void __user __force *)(-1UL);
  470. /*
  471. * FPU emulator may have its own trampoline active just
  472. * above the user stack, 16-bytes before the next lowest
  473. * 16 byte boundary. Try to avoid trashing it.
  474. */
  475. sp -= 32;
  476. sp = sigsp(sp, ksig);
  477. return (void __user *)((sp - frame_size) & SIGMASK);
  478. }
  479. /*
  480. * Atomically swap in the new signal mask, and wait for a signal.
  481. */
  482. #ifdef CONFIG_TRAD_SIGNALS
  483. SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
  484. {
  485. return sys_rt_sigsuspend(uset, sizeof(sigset_t));
  486. }
  487. #endif
  488. #ifdef CONFIG_TRAD_SIGNALS
  489. SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
  490. struct sigaction __user *, oact)
  491. {
  492. struct k_sigaction new_ka, old_ka;
  493. int ret;
  494. int err = 0;
  495. if (act) {
  496. old_sigset_t mask;
  497. if (!access_ok(act, sizeof(*act)))
  498. return -EFAULT;
  499. err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
  500. err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  501. err |= __get_user(mask, &act->sa_mask.sig[0]);
  502. if (err)
  503. return -EFAULT;
  504. siginitset(&new_ka.sa.sa_mask, mask);
  505. }
  506. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  507. if (!ret && oact) {
  508. if (!access_ok(oact, sizeof(*oact)))
  509. return -EFAULT;
  510. err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  511. err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
  512. err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
  513. err |= __put_user(0, &oact->sa_mask.sig[1]);
  514. err |= __put_user(0, &oact->sa_mask.sig[2]);
  515. err |= __put_user(0, &oact->sa_mask.sig[3]);
  516. if (err)
  517. return -EFAULT;
  518. }
  519. return ret;
  520. }
  521. #endif
  522. #ifdef CONFIG_TRAD_SIGNALS
  523. asmlinkage void sys_sigreturn(void)
  524. {
  525. struct sigframe __user *frame;
  526. struct pt_regs *regs;
  527. sigset_t blocked;
  528. int sig;
  529. regs = current_pt_regs();
  530. frame = (struct sigframe __user *)regs->regs[29];
  531. if (!access_ok(frame, sizeof(*frame)))
  532. goto badframe;
  533. if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
  534. goto badframe;
  535. set_current_blocked(&blocked);
  536. sig = restore_sigcontext(regs, &frame->sf_sc);
  537. if (sig < 0)
  538. goto badframe;
  539. else if (sig)
  540. force_sig(sig);
  541. /*
  542. * Don't let your children do this ...
  543. */
  544. __asm__ __volatile__(
  545. "move\t$29, %0\n\t"
  546. "j\tsyscall_exit"
  547. : /* no outputs */
  548. : "r" (regs));
  549. /* Unreached */
  550. badframe:
  551. force_sig(SIGSEGV);
  552. }
  553. #endif /* CONFIG_TRAD_SIGNALS */
  554. asmlinkage void sys_rt_sigreturn(void)
  555. {
  556. struct rt_sigframe __user *frame;
  557. struct pt_regs *regs;
  558. sigset_t set;
  559. int sig;
  560. regs = current_pt_regs();
  561. frame = (struct rt_sigframe __user *)regs->regs[29];
  562. if (!access_ok(frame, sizeof(*frame)))
  563. goto badframe;
  564. if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
  565. goto badframe;
  566. set_current_blocked(&set);
  567. sig = restore_sigcontext(regs, &frame->rs_uc.uc_mcontext);
  568. if (sig < 0)
  569. goto badframe;
  570. else if (sig)
  571. force_sig(sig);
  572. if (restore_altstack(&frame->rs_uc.uc_stack))
  573. goto badframe;
  574. /*
  575. * Don't let your children do this ...
  576. */
  577. __asm__ __volatile__(
  578. "move\t$29, %0\n\t"
  579. "j\tsyscall_exit"
  580. : /* no outputs */
  581. : "r" (regs));
  582. /* Unreached */
  583. badframe:
  584. force_sig(SIGSEGV);
  585. }
  586. #ifdef CONFIG_TRAD_SIGNALS
  587. static int setup_frame(void *sig_return, struct ksignal *ksig,
  588. struct pt_regs *regs, sigset_t *set)
  589. {
  590. struct sigframe __user *frame;
  591. int err = 0;
  592. frame = get_sigframe(ksig, regs, sizeof(*frame));
  593. if (!access_ok(frame, sizeof (*frame)))
  594. return -EFAULT;
  595. err |= setup_sigcontext(regs, &frame->sf_sc);
  596. err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
  597. if (err)
  598. return -EFAULT;
  599. /*
  600. * Arguments to signal handler:
  601. *
  602. * a0 = signal number
  603. * a1 = 0 (should be cause)
  604. * a2 = pointer to struct sigcontext
  605. *
  606. * $25 and c0_epc point to the signal handler, $29 points to the
  607. * struct sigframe.
  608. */
  609. regs->regs[ 4] = ksig->sig;
  610. regs->regs[ 5] = 0;
  611. regs->regs[ 6] = (unsigned long) &frame->sf_sc;
  612. regs->regs[29] = (unsigned long) frame;
  613. regs->regs[31] = (unsigned long) sig_return;
  614. regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
  615. DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
  616. current->comm, current->pid,
  617. frame, regs->cp0_epc, regs->regs[31]);
  618. return 0;
  619. }
  620. #endif
  621. static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
  622. struct pt_regs *regs, sigset_t *set)
  623. {
  624. struct rt_sigframe __user *frame;
  625. frame = get_sigframe(ksig, regs, sizeof(*frame));
  626. if (!access_ok(frame, sizeof (*frame)))
  627. return -EFAULT;
  628. /* Create siginfo. */
  629. if (copy_siginfo_to_user(&frame->rs_info, &ksig->info))
  630. return -EFAULT;
  631. /* Create the ucontext. */
  632. if (__put_user(0, &frame->rs_uc.uc_flags))
  633. return -EFAULT;
  634. if (__put_user(NULL, &frame->rs_uc.uc_link))
  635. return -EFAULT;
  636. if (__save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]))
  637. return -EFAULT;
  638. if (setup_sigcontext(regs, &frame->rs_uc.uc_mcontext))
  639. return -EFAULT;
  640. if (__copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set)))
  641. return -EFAULT;
  642. /*
  643. * Arguments to signal handler:
  644. *
  645. * a0 = signal number
  646. * a1 = 0 (should be cause)
  647. * a2 = pointer to ucontext
  648. *
  649. * $25 and c0_epc point to the signal handler, $29 points to
  650. * the struct rt_sigframe.
  651. */
  652. regs->regs[ 4] = ksig->sig;
  653. regs->regs[ 5] = (unsigned long) &frame->rs_info;
  654. regs->regs[ 6] = (unsigned long) &frame->rs_uc;
  655. regs->regs[29] = (unsigned long) frame;
  656. regs->regs[31] = (unsigned long) sig_return;
  657. regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
  658. DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
  659. current->comm, current->pid,
  660. frame, regs->cp0_epc, regs->regs[31]);
  661. return 0;
  662. }
  663. struct mips_abi mips_abi = {
  664. #ifdef CONFIG_TRAD_SIGNALS
  665. .setup_frame = setup_frame,
  666. #endif
  667. .setup_rt_frame = setup_rt_frame,
  668. .restart = __NR_restart_syscall,
  669. .off_sc_fpregs = offsetof(struct sigcontext, sc_fpregs),
  670. .off_sc_fpc_csr = offsetof(struct sigcontext, sc_fpc_csr),
  671. .off_sc_used_math = offsetof(struct sigcontext, sc_used_math),
  672. .vdso = &vdso_image,
  673. };
  674. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  675. {
  676. sigset_t *oldset = sigmask_to_save();
  677. int ret;
  678. struct mips_abi *abi = current->thread.abi;
  679. void *vdso = current->mm->context.vdso;
  680. /*
  681. * If we were emulating a delay slot instruction, exit that frame such
  682. * that addresses in the sigframe are as expected for userland and we
  683. * don't have a problem if we reuse the thread's frame for an
  684. * instruction within the signal handler.
  685. */
  686. dsemul_thread_rollback(regs);
  687. if (regs->regs[0]) {
  688. switch(regs->regs[2]) {
  689. case ERESTART_RESTARTBLOCK:
  690. case ERESTARTNOHAND:
  691. regs->regs[2] = EINTR;
  692. break;
  693. case ERESTARTSYS:
  694. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  695. regs->regs[2] = EINTR;
  696. break;
  697. }
  698. fallthrough;
  699. case ERESTARTNOINTR:
  700. regs->regs[7] = regs->regs[26];
  701. regs->regs[2] = regs->regs[0];
  702. regs->cp0_epc -= 4;
  703. }
  704. regs->regs[0] = 0; /* Don't deal with this again. */
  705. }
  706. rseq_signal_deliver(ksig, regs);
  707. if (sig_uses_siginfo(&ksig->ka, abi))
  708. ret = abi->setup_rt_frame(vdso + abi->vdso->off_rt_sigreturn,
  709. ksig, regs, oldset);
  710. else
  711. ret = abi->setup_frame(vdso + abi->vdso->off_sigreturn,
  712. ksig, regs, oldset);
  713. signal_setup_done(ret, ksig, 0);
  714. }
  715. static void do_signal(struct pt_regs *regs)
  716. {
  717. struct ksignal ksig;
  718. if (get_signal(&ksig)) {
  719. /* Whee! Actually deliver the signal. */
  720. handle_signal(&ksig, regs);
  721. return;
  722. }
  723. if (regs->regs[0]) {
  724. switch (regs->regs[2]) {
  725. case ERESTARTNOHAND:
  726. case ERESTARTSYS:
  727. case ERESTARTNOINTR:
  728. regs->regs[2] = regs->regs[0];
  729. regs->regs[7] = regs->regs[26];
  730. regs->cp0_epc -= 4;
  731. break;
  732. case ERESTART_RESTARTBLOCK:
  733. regs->regs[2] = current->thread.abi->restart;
  734. regs->regs[7] = regs->regs[26];
  735. regs->cp0_epc -= 4;
  736. break;
  737. }
  738. regs->regs[0] = 0; /* Don't deal with this again. */
  739. }
  740. /*
  741. * If there's no signal to deliver, we just put the saved sigmask
  742. * back
  743. */
  744. restore_saved_sigmask();
  745. }
  746. /*
  747. * notification of userspace execution resumption
  748. * - triggered by the TIF_WORK_MASK flags
  749. */
  750. asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
  751. __u32 thread_info_flags)
  752. {
  753. local_irq_enable();
  754. user_exit();
  755. if (thread_info_flags & _TIF_UPROBE)
  756. uprobe_notify_resume(regs);
  757. /* deal with pending signal delivery */
  758. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
  759. do_signal(regs);
  760. if (thread_info_flags & _TIF_NOTIFY_RESUME)
  761. resume_user_mode_work(regs);
  762. user_enter();
  763. }
  764. #if defined(CONFIG_SMP) && defined(CONFIG_MIPS_FP_SUPPORT)
  765. static int smp_save_fp_context(void __user *sc)
  766. {
  767. return raw_cpu_has_fpu
  768. ? save_hw_fp_context(sc)
  769. : copy_fp_to_sigcontext(sc);
  770. }
  771. static int smp_restore_fp_context(void __user *sc)
  772. {
  773. return raw_cpu_has_fpu
  774. ? restore_hw_fp_context(sc)
  775. : copy_fp_from_sigcontext(sc);
  776. }
  777. #endif
  778. static int signal_setup(void)
  779. {
  780. /*
  781. * The offset from sigcontext to extended context should be the same
  782. * regardless of the type of signal, such that userland can always know
  783. * where to look if it wishes to find the extended context structures.
  784. */
  785. BUILD_BUG_ON((offsetof(struct sigframe, sf_extcontext) -
  786. offsetof(struct sigframe, sf_sc)) !=
  787. (offsetof(struct rt_sigframe, rs_uc.uc_extcontext) -
  788. offsetof(struct rt_sigframe, rs_uc.uc_mcontext)));
  789. #if defined(CONFIG_SMP) && defined(CONFIG_MIPS_FP_SUPPORT)
  790. /* For now just do the cpu_has_fpu check when the functions are invoked */
  791. save_fp_context = smp_save_fp_context;
  792. restore_fp_context = smp_restore_fp_context;
  793. #else
  794. if (cpu_has_fpu) {
  795. save_fp_context = save_hw_fp_context;
  796. restore_fp_context = restore_hw_fp_context;
  797. } else {
  798. save_fp_context = copy_fp_to_sigcontext;
  799. restore_fp_context = copy_fp_from_sigcontext;
  800. }
  801. #endif /* CONFIG_SMP */
  802. return 0;
  803. }
  804. arch_initcall(signal_setup);