signal32.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /* arch/sparc64/kernel/signal32.c
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
  6. * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
  7. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/signal.h>
  12. #include <linux/errno.h>
  13. #include <linux/wait.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/unistd.h>
  16. #include <linux/mm.h>
  17. #include <linux/tty.h>
  18. #include <linux/binfmts.h>
  19. #include <linux/compat.h>
  20. #include <linux/bitops.h>
  21. #include <linux/tracehook.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/ptrace.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/psrcompat.h>
  26. #include <asm/fpumacro.h>
  27. #include <asm/visasm.h>
  28. #include <asm/compat_signal.h>
  29. #include <asm/switch_to.h>
  30. #include "sigutil.h"
  31. #include "kernel.h"
  32. /* This magic should be in g_upper[0] for all upper parts
  33. * to be valid.
  34. */
  35. #define SIGINFO_EXTRA_V8PLUS_MAGIC 0x130e269
  36. typedef struct {
  37. unsigned int g_upper[8];
  38. unsigned int o_upper[8];
  39. unsigned int asi;
  40. } siginfo_extra_v8plus_t;
  41. struct signal_frame32 {
  42. struct sparc_stackf32 ss;
  43. __siginfo32_t info;
  44. /* __siginfo_fpu_t * */ u32 fpu_save;
  45. unsigned int insns[2];
  46. unsigned int extramask[_COMPAT_NSIG_WORDS - 1];
  47. unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
  48. /* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
  49. siginfo_extra_v8plus_t v8plus;
  50. /* __siginfo_rwin_t * */u32 rwin_save;
  51. } __attribute__((aligned(8)));
  52. struct rt_signal_frame32 {
  53. struct sparc_stackf32 ss;
  54. compat_siginfo_t info;
  55. struct pt_regs32 regs;
  56. compat_sigset_t mask;
  57. /* __siginfo_fpu_t * */ u32 fpu_save;
  58. unsigned int insns[2];
  59. compat_stack_t stack;
  60. unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
  61. /* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
  62. siginfo_extra_v8plus_t v8plus;
  63. /* __siginfo_rwin_t * */u32 rwin_save;
  64. } __attribute__((aligned(8)));
  65. int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
  66. {
  67. int err;
  68. if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
  69. return -EFAULT;
  70. /* If you change siginfo_t structure, please be sure
  71. this code is fixed accordingly.
  72. It should never copy any pad contained in the structure
  73. to avoid security leaks, but must copy the generic
  74. 3 ints plus the relevant union member.
  75. This routine must convert siginfo from 64bit to 32bit as well
  76. at the same time. */
  77. err = __put_user(from->si_signo, &to->si_signo);
  78. err |= __put_user(from->si_errno, &to->si_errno);
  79. err |= __put_user((short)from->si_code, &to->si_code);
  80. if (from->si_code < 0)
  81. err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
  82. else {
  83. switch (from->si_code >> 16) {
  84. case __SI_TIMER >> 16:
  85. err |= __put_user(from->si_tid, &to->si_tid);
  86. err |= __put_user(from->si_overrun, &to->si_overrun);
  87. err |= __put_user(from->si_int, &to->si_int);
  88. break;
  89. case __SI_CHLD >> 16:
  90. err |= __put_user(from->si_utime, &to->si_utime);
  91. err |= __put_user(from->si_stime, &to->si_stime);
  92. err |= __put_user(from->si_status, &to->si_status);
  93. default:
  94. err |= __put_user(from->si_pid, &to->si_pid);
  95. err |= __put_user(from->si_uid, &to->si_uid);
  96. break;
  97. case __SI_FAULT >> 16:
  98. err |= __put_user(from->si_trapno, &to->si_trapno);
  99. err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
  100. break;
  101. case __SI_POLL >> 16:
  102. err |= __put_user(from->si_band, &to->si_band);
  103. err |= __put_user(from->si_fd, &to->si_fd);
  104. break;
  105. case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
  106. case __SI_MESGQ >> 16:
  107. err |= __put_user(from->si_pid, &to->si_pid);
  108. err |= __put_user(from->si_uid, &to->si_uid);
  109. err |= __put_user(from->si_int, &to->si_int);
  110. break;
  111. }
  112. }
  113. return err;
  114. }
  115. /* CAUTION: This is just a very minimalist implementation for the
  116. * sake of compat_sys_rt_sigqueueinfo()
  117. */
  118. int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
  119. {
  120. if (!access_ok(VERIFY_WRITE, from, sizeof(compat_siginfo_t)))
  121. return -EFAULT;
  122. if (copy_from_user(to, from, 3*sizeof(int)) ||
  123. copy_from_user(to->_sifields._pad, from->_sifields._pad,
  124. SI_PAD_SIZE))
  125. return -EFAULT;
  126. return 0;
  127. }
  128. /* Checks if the fp is valid. We always build signal frames which are
  129. * 16-byte aligned, therefore we can always enforce that the restore
  130. * frame has that property as well.
  131. */
  132. static bool invalid_frame_pointer(void __user *fp, int fplen)
  133. {
  134. if ((((unsigned long) fp) & 15) ||
  135. ((unsigned long)fp) > 0x100000000ULL - fplen)
  136. return true;
  137. return false;
  138. }
  139. void do_sigreturn32(struct pt_regs *regs)
  140. {
  141. struct signal_frame32 __user *sf;
  142. compat_uptr_t fpu_save;
  143. compat_uptr_t rwin_save;
  144. unsigned int psr, ufp;
  145. unsigned int pc, npc;
  146. sigset_t set;
  147. compat_sigset_t seta;
  148. int err, i;
  149. /* Always make any pending restarted system calls return -EINTR */
  150. current->restart_block.fn = do_no_restart_syscall;
  151. synchronize_user_stack();
  152. regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
  153. sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
  154. /* 1. Make sure we are not getting garbage from the user */
  155. if (invalid_frame_pointer(sf, sizeof(*sf)))
  156. goto segv;
  157. if (get_user(ufp, &sf->info.si_regs.u_regs[UREG_FP]))
  158. goto segv;
  159. if (ufp & 0x7)
  160. goto segv;
  161. if (__get_user(pc, &sf->info.si_regs.pc) ||
  162. __get_user(npc, &sf->info.si_regs.npc))
  163. goto segv;
  164. if ((pc | npc) & 3)
  165. goto segv;
  166. if (test_thread_flag(TIF_32BIT)) {
  167. pc &= 0xffffffff;
  168. npc &= 0xffffffff;
  169. }
  170. regs->tpc = pc;
  171. regs->tnpc = npc;
  172. /* 2. Restore the state */
  173. err = __get_user(regs->y, &sf->info.si_regs.y);
  174. err |= __get_user(psr, &sf->info.si_regs.psr);
  175. for (i = UREG_G1; i <= UREG_I7; i++)
  176. err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
  177. if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
  178. err |= __get_user(i, &sf->v8plus.g_upper[0]);
  179. if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
  180. unsigned long asi;
  181. for (i = UREG_G1; i <= UREG_I7; i++)
  182. err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
  183. err |= __get_user(asi, &sf->v8plus.asi);
  184. regs->tstate &= ~TSTATE_ASI;
  185. regs->tstate |= ((asi & 0xffUL) << 24UL);
  186. }
  187. }
  188. /* User can only change condition codes in %tstate. */
  189. regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
  190. regs->tstate |= psr_to_tstate_icc(psr);
  191. /* Prevent syscall restart. */
  192. pt_regs_clear_syscall(regs);
  193. err |= __get_user(fpu_save, &sf->fpu_save);
  194. if (!err && fpu_save)
  195. err |= restore_fpu_state(regs, compat_ptr(fpu_save));
  196. err |= __get_user(rwin_save, &sf->rwin_save);
  197. if (!err && rwin_save) {
  198. if (restore_rwin_state(compat_ptr(rwin_save)))
  199. goto segv;
  200. }
  201. err |= __get_user(seta.sig[0], &sf->info.si_mask);
  202. err |= copy_from_user(&seta.sig[1], &sf->extramask,
  203. (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
  204. if (err)
  205. goto segv;
  206. set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
  207. set_current_blocked(&set);
  208. return;
  209. segv:
  210. force_sig(SIGSEGV, current);
  211. }
  212. asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
  213. {
  214. struct rt_signal_frame32 __user *sf;
  215. unsigned int psr, pc, npc, ufp;
  216. compat_uptr_t fpu_save;
  217. compat_uptr_t rwin_save;
  218. sigset_t set;
  219. compat_sigset_t seta;
  220. int err, i;
  221. /* Always make any pending restarted system calls return -EINTR */
  222. current->restart_block.fn = do_no_restart_syscall;
  223. synchronize_user_stack();
  224. regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
  225. sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
  226. /* 1. Make sure we are not getting garbage from the user */
  227. if (invalid_frame_pointer(sf, sizeof(*sf)))
  228. goto segv;
  229. if (get_user(ufp, &sf->regs.u_regs[UREG_FP]))
  230. goto segv;
  231. if (ufp & 0x7)
  232. goto segv;
  233. if (__get_user(pc, &sf->regs.pc) ||
  234. __get_user(npc, &sf->regs.npc))
  235. goto segv;
  236. if ((pc | npc) & 3)
  237. goto segv;
  238. if (test_thread_flag(TIF_32BIT)) {
  239. pc &= 0xffffffff;
  240. npc &= 0xffffffff;
  241. }
  242. regs->tpc = pc;
  243. regs->tnpc = npc;
  244. /* 2. Restore the state */
  245. err = __get_user(regs->y, &sf->regs.y);
  246. err |= __get_user(psr, &sf->regs.psr);
  247. for (i = UREG_G1; i <= UREG_I7; i++)
  248. err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
  249. if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
  250. err |= __get_user(i, &sf->v8plus.g_upper[0]);
  251. if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
  252. unsigned long asi;
  253. for (i = UREG_G1; i <= UREG_I7; i++)
  254. err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
  255. err |= __get_user(asi, &sf->v8plus.asi);
  256. regs->tstate &= ~TSTATE_ASI;
  257. regs->tstate |= ((asi & 0xffUL) << 24UL);
  258. }
  259. }
  260. /* User can only change condition codes in %tstate. */
  261. regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
  262. regs->tstate |= psr_to_tstate_icc(psr);
  263. /* Prevent syscall restart. */
  264. pt_regs_clear_syscall(regs);
  265. err |= __get_user(fpu_save, &sf->fpu_save);
  266. if (!err && fpu_save)
  267. err |= restore_fpu_state(regs, compat_ptr(fpu_save));
  268. err |= copy_from_user(&seta, &sf->mask, sizeof(compat_sigset_t));
  269. err |= compat_restore_altstack(&sf->stack);
  270. if (err)
  271. goto segv;
  272. err |= __get_user(rwin_save, &sf->rwin_save);
  273. if (!err && rwin_save) {
  274. if (restore_rwin_state(compat_ptr(rwin_save)))
  275. goto segv;
  276. }
  277. set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
  278. set_current_blocked(&set);
  279. return;
  280. segv:
  281. force_sig(SIGSEGV, current);
  282. }
  283. static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
  284. {
  285. unsigned long sp;
  286. regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
  287. sp = regs->u_regs[UREG_FP];
  288. /*
  289. * If we are on the alternate signal stack and would overflow it, don't.
  290. * Return an always-bogus address instead so we will die with SIGSEGV.
  291. */
  292. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  293. return (void __user *) -1L;
  294. /* This is the X/Open sanctioned signal stack switching. */
  295. sp = sigsp(sp, ksig) - framesize;
  296. /* Always align the stack frame. This handles two cases. First,
  297. * sigaltstack need not be mindful of platform specific stack
  298. * alignment. Second, if we took this signal because the stack
  299. * is not aligned properly, we'd like to take the signal cleanly
  300. * and report that.
  301. */
  302. sp &= ~15UL;
  303. return (void __user *) sp;
  304. }
  305. /* The I-cache flush instruction only works in the primary ASI, which
  306. * right now is the nucleus, aka. kernel space.
  307. *
  308. * Therefore we have to kick the instructions out using the kernel
  309. * side linear mapping of the physical address backing the user
  310. * instructions.
  311. */
  312. static void flush_signal_insns(unsigned long address)
  313. {
  314. unsigned long pstate, paddr;
  315. pte_t *ptep, pte;
  316. pgd_t *pgdp;
  317. pud_t *pudp;
  318. pmd_t *pmdp;
  319. /* Commit all stores of the instructions we are about to flush. */
  320. wmb();
  321. /* Disable cross-call reception. In this way even a very wide
  322. * munmap() on another cpu can't tear down the page table
  323. * hierarchy from underneath us, since that can't complete
  324. * until the IPI tlb flush returns.
  325. */
  326. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  327. __asm__ __volatile__("wrpr %0, %1, %%pstate"
  328. : : "r" (pstate), "i" (PSTATE_IE));
  329. pgdp = pgd_offset(current->mm, address);
  330. if (pgd_none(*pgdp))
  331. goto out_irqs_on;
  332. pudp = pud_offset(pgdp, address);
  333. if (pud_none(*pudp))
  334. goto out_irqs_on;
  335. pmdp = pmd_offset(pudp, address);
  336. if (pmd_none(*pmdp))
  337. goto out_irqs_on;
  338. ptep = pte_offset_map(pmdp, address);
  339. pte = *ptep;
  340. if (!pte_present(pte))
  341. goto out_unmap;
  342. paddr = (unsigned long) page_address(pte_page(pte));
  343. __asm__ __volatile__("flush %0 + %1"
  344. : /* no outputs */
  345. : "r" (paddr),
  346. "r" (address & (PAGE_SIZE - 1))
  347. : "memory");
  348. out_unmap:
  349. pte_unmap(ptep);
  350. out_irqs_on:
  351. __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
  352. }
  353. static int setup_frame32(struct ksignal *ksig, struct pt_regs *regs,
  354. sigset_t *oldset)
  355. {
  356. struct signal_frame32 __user *sf;
  357. int i, err, wsaved;
  358. void __user *tail;
  359. int sigframe_size;
  360. u32 psr;
  361. compat_sigset_t seta;
  362. /* 1. Make sure everything is clean */
  363. synchronize_user_stack();
  364. save_and_clear_fpu();
  365. wsaved = get_thread_wsaved();
  366. sigframe_size = sizeof(*sf);
  367. if (current_thread_info()->fpsaved[0] & FPRS_FEF)
  368. sigframe_size += sizeof(__siginfo_fpu_t);
  369. if (wsaved)
  370. sigframe_size += sizeof(__siginfo_rwin_t);
  371. sf = (struct signal_frame32 __user *)
  372. get_sigframe(ksig, regs, sigframe_size);
  373. if (invalid_frame_pointer(sf, sigframe_size)) {
  374. do_exit(SIGILL);
  375. return -EINVAL;
  376. }
  377. tail = (sf + 1);
  378. /* 2. Save the current process state */
  379. if (test_thread_flag(TIF_32BIT)) {
  380. regs->tpc &= 0xffffffff;
  381. regs->tnpc &= 0xffffffff;
  382. }
  383. err = put_user(regs->tpc, &sf->info.si_regs.pc);
  384. err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
  385. err |= __put_user(regs->y, &sf->info.si_regs.y);
  386. psr = tstate_to_psr(regs->tstate);
  387. if (current_thread_info()->fpsaved[0] & FPRS_FEF)
  388. psr |= PSR_EF;
  389. err |= __put_user(psr, &sf->info.si_regs.psr);
  390. for (i = 0; i < 16; i++)
  391. err |= __put_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
  392. err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
  393. err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
  394. for (i = 1; i < 16; i++)
  395. err |= __put_user(((u32 *)regs->u_regs)[2*i],
  396. &sf->v8plus.g_upper[i]);
  397. err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
  398. &sf->v8plus.asi);
  399. if (psr & PSR_EF) {
  400. __siginfo_fpu_t __user *fp = tail;
  401. tail += sizeof(*fp);
  402. err |= save_fpu_state(regs, fp);
  403. err |= __put_user((u64)fp, &sf->fpu_save);
  404. } else {
  405. err |= __put_user(0, &sf->fpu_save);
  406. }
  407. if (wsaved) {
  408. __siginfo_rwin_t __user *rwp = tail;
  409. tail += sizeof(*rwp);
  410. err |= save_rwin_state(wsaved, rwp);
  411. err |= __put_user((u64)rwp, &sf->rwin_save);
  412. set_thread_wsaved(0);
  413. } else {
  414. err |= __put_user(0, &sf->rwin_save);
  415. }
  416. /* If these change we need to know - assignments to seta relies on these sizes */
  417. BUILD_BUG_ON(_NSIG_WORDS != 1);
  418. BUILD_BUG_ON(_COMPAT_NSIG_WORDS != 2);
  419. seta.sig[1] = (oldset->sig[0] >> 32);
  420. seta.sig[0] = oldset->sig[0];
  421. err |= __put_user(seta.sig[0], &sf->info.si_mask);
  422. err |= __copy_to_user(sf->extramask, &seta.sig[1],
  423. (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
  424. if (!wsaved) {
  425. err |= copy_in_user((u32 __user *)sf,
  426. (u32 __user *)(regs->u_regs[UREG_FP]),
  427. sizeof(struct reg_window32));
  428. } else {
  429. struct reg_window *rp;
  430. rp = &current_thread_info()->reg_window[wsaved - 1];
  431. for (i = 0; i < 8; i++)
  432. err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
  433. for (i = 0; i < 6; i++)
  434. err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
  435. err |= __put_user(rp->ins[6], &sf->ss.fp);
  436. err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
  437. }
  438. if (err)
  439. return err;
  440. /* 3. signal handler back-trampoline and parameters */
  441. regs->u_regs[UREG_FP] = (unsigned long) sf;
  442. regs->u_regs[UREG_I0] = ksig->sig;
  443. regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
  444. regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
  445. /* 4. signal handler */
  446. regs->tpc = (unsigned long) ksig->ka.sa.sa_handler;
  447. regs->tnpc = (regs->tpc + 4);
  448. if (test_thread_flag(TIF_32BIT)) {
  449. regs->tpc &= 0xffffffff;
  450. regs->tnpc &= 0xffffffff;
  451. }
  452. /* 5. return to kernel instructions */
  453. if (ksig->ka.ka_restorer) {
  454. regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
  455. } else {
  456. unsigned long address = ((unsigned long)&(sf->insns[0]));
  457. regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
  458. err = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
  459. err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
  460. if (err)
  461. return err;
  462. flush_signal_insns(address);
  463. }
  464. return 0;
  465. }
  466. static int setup_rt_frame32(struct ksignal *ksig, struct pt_regs *regs,
  467. sigset_t *oldset)
  468. {
  469. struct rt_signal_frame32 __user *sf;
  470. int i, err, wsaved;
  471. void __user *tail;
  472. int sigframe_size;
  473. u32 psr;
  474. compat_sigset_t seta;
  475. /* 1. Make sure everything is clean */
  476. synchronize_user_stack();
  477. save_and_clear_fpu();
  478. wsaved = get_thread_wsaved();
  479. sigframe_size = sizeof(*sf);
  480. if (current_thread_info()->fpsaved[0] & FPRS_FEF)
  481. sigframe_size += sizeof(__siginfo_fpu_t);
  482. if (wsaved)
  483. sigframe_size += sizeof(__siginfo_rwin_t);
  484. sf = (struct rt_signal_frame32 __user *)
  485. get_sigframe(ksig, regs, sigframe_size);
  486. if (invalid_frame_pointer(sf, sigframe_size)) {
  487. do_exit(SIGILL);
  488. return -EINVAL;
  489. }
  490. tail = (sf + 1);
  491. /* 2. Save the current process state */
  492. if (test_thread_flag(TIF_32BIT)) {
  493. regs->tpc &= 0xffffffff;
  494. regs->tnpc &= 0xffffffff;
  495. }
  496. err = put_user(regs->tpc, &sf->regs.pc);
  497. err |= __put_user(regs->tnpc, &sf->regs.npc);
  498. err |= __put_user(regs->y, &sf->regs.y);
  499. psr = tstate_to_psr(regs->tstate);
  500. if (current_thread_info()->fpsaved[0] & FPRS_FEF)
  501. psr |= PSR_EF;
  502. err |= __put_user(psr, &sf->regs.psr);
  503. for (i = 0; i < 16; i++)
  504. err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
  505. err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
  506. err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
  507. for (i = 1; i < 16; i++)
  508. err |= __put_user(((u32 *)regs->u_regs)[2*i],
  509. &sf->v8plus.g_upper[i]);
  510. err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
  511. &sf->v8plus.asi);
  512. if (psr & PSR_EF) {
  513. __siginfo_fpu_t __user *fp = tail;
  514. tail += sizeof(*fp);
  515. err |= save_fpu_state(regs, fp);
  516. err |= __put_user((u64)fp, &sf->fpu_save);
  517. } else {
  518. err |= __put_user(0, &sf->fpu_save);
  519. }
  520. if (wsaved) {
  521. __siginfo_rwin_t __user *rwp = tail;
  522. tail += sizeof(*rwp);
  523. err |= save_rwin_state(wsaved, rwp);
  524. err |= __put_user((u64)rwp, &sf->rwin_save);
  525. set_thread_wsaved(0);
  526. } else {
  527. err |= __put_user(0, &sf->rwin_save);
  528. }
  529. /* Update the siginfo structure. */
  530. err |= copy_siginfo_to_user32(&sf->info, &ksig->info);
  531. /* Setup sigaltstack */
  532. err |= __compat_save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
  533. seta.sig[1] = (oldset->sig[0] >> 32);
  534. seta.sig[0] = oldset->sig[0];
  535. err |= __copy_to_user(&sf->mask, &seta, sizeof(compat_sigset_t));
  536. if (!wsaved) {
  537. err |= copy_in_user((u32 __user *)sf,
  538. (u32 __user *)(regs->u_regs[UREG_FP]),
  539. sizeof(struct reg_window32));
  540. } else {
  541. struct reg_window *rp;
  542. rp = &current_thread_info()->reg_window[wsaved - 1];
  543. for (i = 0; i < 8; i++)
  544. err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
  545. for (i = 0; i < 6; i++)
  546. err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
  547. err |= __put_user(rp->ins[6], &sf->ss.fp);
  548. err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
  549. }
  550. if (err)
  551. return err;
  552. /* 3. signal handler back-trampoline and parameters */
  553. regs->u_regs[UREG_FP] = (unsigned long) sf;
  554. regs->u_regs[UREG_I0] = ksig->sig;
  555. regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
  556. regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
  557. /* 4. signal handler */
  558. regs->tpc = (unsigned long) ksig->ka.sa.sa_handler;
  559. regs->tnpc = (regs->tpc + 4);
  560. if (test_thread_flag(TIF_32BIT)) {
  561. regs->tpc &= 0xffffffff;
  562. regs->tnpc &= 0xffffffff;
  563. }
  564. /* 5. return to kernel instructions */
  565. if (ksig->ka.ka_restorer)
  566. regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
  567. else {
  568. unsigned long address = ((unsigned long)&(sf->insns[0]));
  569. regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
  570. /* mov __NR_rt_sigreturn, %g1 */
  571. err |= __put_user(0x82102065, &sf->insns[0]);
  572. /* t 0x10 */
  573. err |= __put_user(0x91d02010, &sf->insns[1]);
  574. if (err)
  575. return err;
  576. flush_signal_insns(address);
  577. }
  578. return 0;
  579. }
  580. static inline void handle_signal32(struct ksignal *ksig,
  581. struct pt_regs *regs)
  582. {
  583. sigset_t *oldset = sigmask_to_save();
  584. int err;
  585. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  586. err = setup_rt_frame32(ksig, regs, oldset);
  587. else
  588. err = setup_frame32(ksig, regs, oldset);
  589. signal_setup_done(err, ksig, 0);
  590. }
  591. static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
  592. struct sigaction *sa)
  593. {
  594. switch (regs->u_regs[UREG_I0]) {
  595. case ERESTART_RESTARTBLOCK:
  596. case ERESTARTNOHAND:
  597. no_system_call_restart:
  598. regs->u_regs[UREG_I0] = EINTR;
  599. regs->tstate |= TSTATE_ICARRY;
  600. break;
  601. case ERESTARTSYS:
  602. if (!(sa->sa_flags & SA_RESTART))
  603. goto no_system_call_restart;
  604. /* fallthrough */
  605. case ERESTARTNOINTR:
  606. regs->u_regs[UREG_I0] = orig_i0;
  607. regs->tpc -= 4;
  608. regs->tnpc -= 4;
  609. }
  610. }
  611. /* Note that 'init' is a special process: it doesn't get signals it doesn't
  612. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  613. * mistake.
  614. */
  615. void do_signal32(struct pt_regs * regs)
  616. {
  617. struct ksignal ksig;
  618. unsigned long orig_i0 = 0;
  619. int restart_syscall = 0;
  620. bool has_handler = get_signal(&ksig);
  621. if (pt_regs_is_syscall(regs) &&
  622. (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
  623. restart_syscall = 1;
  624. orig_i0 = regs->u_regs[UREG_G6];
  625. }
  626. if (has_handler) {
  627. if (restart_syscall)
  628. syscall_restart32(orig_i0, regs, &ksig.ka.sa);
  629. handle_signal32(&ksig, regs);
  630. } else {
  631. if (restart_syscall) {
  632. switch (regs->u_regs[UREG_I0]) {
  633. case ERESTARTNOHAND:
  634. case ERESTARTSYS:
  635. case ERESTARTNOINTR:
  636. /* replay the system call when we are done */
  637. regs->u_regs[UREG_I0] = orig_i0;
  638. regs->tpc -= 4;
  639. regs->tnpc -= 4;
  640. pt_regs_clear_syscall(regs);
  641. case ERESTART_RESTARTBLOCK:
  642. regs->u_regs[UREG_G1] = __NR_restart_syscall;
  643. regs->tpc -= 4;
  644. regs->tnpc -= 4;
  645. pt_regs_clear_syscall(regs);
  646. }
  647. }
  648. restore_saved_sigmask();
  649. }
  650. }
  651. struct sigstack32 {
  652. u32 the_stack;
  653. int cur_status;
  654. };
  655. asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
  656. {
  657. struct sigstack32 __user *ssptr =
  658. (struct sigstack32 __user *)((unsigned long)(u_ssptr));
  659. struct sigstack32 __user *ossptr =
  660. (struct sigstack32 __user *)((unsigned long)(u_ossptr));
  661. int ret = -EFAULT;
  662. /* First see if old state is wanted. */
  663. if (ossptr) {
  664. if (put_user(current->sas_ss_sp + current->sas_ss_size,
  665. &ossptr->the_stack) ||
  666. __put_user(on_sig_stack(sp), &ossptr->cur_status))
  667. goto out;
  668. }
  669. /* Now see if we want to update the new state. */
  670. if (ssptr) {
  671. u32 ss_sp;
  672. if (get_user(ss_sp, &ssptr->the_stack))
  673. goto out;
  674. /* If the current stack was set with sigaltstack, don't
  675. * swap stacks while we are on it.
  676. */
  677. ret = -EPERM;
  678. if (current->sas_ss_sp && on_sig_stack(sp))
  679. goto out;
  680. /* Since we don't know the extent of the stack, and we don't
  681. * track onstack-ness, but rather calculate it, we must
  682. * presume a size. Ho hum this interface is lossy.
  683. */
  684. current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
  685. current->sas_ss_size = SIGSTKSZ;
  686. }
  687. ret = 0;
  688. out:
  689. return ret;
  690. }