signal_32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /* linux/arch/sparc/kernel/signal.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. */
  8. #include <linux/sched.h>
  9. #include <linux/kernel.h>
  10. #include <linux/signal.h>
  11. #include <linux/errno.h>
  12. #include <linux/wait.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/unistd.h>
  15. #include <linux/mm.h>
  16. #include <linux/tty.h>
  17. #include <linux/smp.h>
  18. #include <linux/binfmts.h> /* do_coredum */
  19. #include <linux/bitops.h>
  20. #include <linux/tracehook.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/cacheflush.h> /* flush_sig_insns */
  26. #include <asm/switch_to.h>
  27. #include "sigutil.h"
  28. #include "kernel.h"
  29. extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
  30. void *fpqueue, unsigned long *fpqdepth);
  31. extern void fpload(unsigned long *fpregs, unsigned long *fsr);
  32. struct signal_frame {
  33. struct sparc_stackf ss;
  34. __siginfo32_t info;
  35. __siginfo_fpu_t __user *fpu_save;
  36. unsigned long insns[2] __attribute__ ((aligned (8)));
  37. unsigned int extramask[_NSIG_WORDS - 1];
  38. unsigned int extra_size; /* Should be 0 */
  39. __siginfo_rwin_t __user *rwin_save;
  40. } __attribute__((aligned(8)));
  41. struct rt_signal_frame {
  42. struct sparc_stackf ss;
  43. siginfo_t info;
  44. struct pt_regs regs;
  45. sigset_t mask;
  46. __siginfo_fpu_t __user *fpu_save;
  47. unsigned int insns[2];
  48. stack_t stack;
  49. unsigned int extra_size; /* Should be 0 */
  50. __siginfo_rwin_t __user *rwin_save;
  51. } __attribute__((aligned(8)));
  52. /* Align macros */
  53. #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7)))
  54. #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7)))
  55. /* Checks if the fp is valid. We always build signal frames which are
  56. * 16-byte aligned, therefore we can always enforce that the restore
  57. * frame has that property as well.
  58. */
  59. static inline bool invalid_frame_pointer(void __user *fp, int fplen)
  60. {
  61. if ((((unsigned long) fp) & 15) || !__access_ok((unsigned long)fp, fplen))
  62. return true;
  63. return false;
  64. }
  65. asmlinkage void do_sigreturn(struct pt_regs *regs)
  66. {
  67. unsigned long up_psr, pc, npc, ufp;
  68. struct signal_frame __user *sf;
  69. sigset_t set;
  70. __siginfo_fpu_t __user *fpu_save;
  71. __siginfo_rwin_t __user *rwin_save;
  72. int err;
  73. /* Always make any pending restarted system calls return -EINTR */
  74. current->restart_block.fn = do_no_restart_syscall;
  75. synchronize_user_stack();
  76. sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
  77. /* 1. Make sure we are not getting garbage from the user */
  78. if (invalid_frame_pointer(sf, sizeof(*sf)))
  79. goto segv_and_exit;
  80. if (get_user(ufp, &sf->info.si_regs.u_regs[UREG_FP]))
  81. goto segv_and_exit;
  82. if (ufp & 0x7)
  83. goto segv_and_exit;
  84. err = __get_user(pc, &sf->info.si_regs.pc);
  85. err |= __get_user(npc, &sf->info.si_regs.npc);
  86. if ((pc | npc) & 3)
  87. goto segv_and_exit;
  88. /* 2. Restore the state */
  89. up_psr = regs->psr;
  90. err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
  91. /* User can only change condition codes and FPU enabling in %psr. */
  92. regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
  93. | (regs->psr & (PSR_ICC | PSR_EF));
  94. /* Prevent syscall restart. */
  95. pt_regs_clear_syscall(regs);
  96. err |= __get_user(fpu_save, &sf->fpu_save);
  97. if (fpu_save)
  98. err |= restore_fpu_state(regs, fpu_save);
  99. err |= __get_user(rwin_save, &sf->rwin_save);
  100. if (rwin_save)
  101. err |= restore_rwin_state(rwin_save);
  102. /* This is pretty much atomic, no amount locking would prevent
  103. * the races which exist anyways.
  104. */
  105. err |= __get_user(set.sig[0], &sf->info.si_mask);
  106. err |= __copy_from_user(&set.sig[1], &sf->extramask,
  107. (_NSIG_WORDS-1) * sizeof(unsigned int));
  108. if (err)
  109. goto segv_and_exit;
  110. set_current_blocked(&set);
  111. return;
  112. segv_and_exit:
  113. force_sig(SIGSEGV, current);
  114. }
  115. asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
  116. {
  117. struct rt_signal_frame __user *sf;
  118. unsigned int psr, pc, npc, ufp;
  119. __siginfo_fpu_t __user *fpu_save;
  120. __siginfo_rwin_t __user *rwin_save;
  121. sigset_t set;
  122. int err;
  123. synchronize_user_stack();
  124. sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
  125. if (invalid_frame_pointer(sf, sizeof(*sf)))
  126. goto segv;
  127. if (get_user(ufp, &sf->regs.u_regs[UREG_FP]))
  128. goto segv;
  129. if (ufp & 0x7)
  130. goto segv;
  131. err = __get_user(pc, &sf->regs.pc);
  132. err |= __get_user(npc, &sf->regs.npc);
  133. err |= ((pc | npc) & 0x03);
  134. err |= __get_user(regs->y, &sf->regs.y);
  135. err |= __get_user(psr, &sf->regs.psr);
  136. err |= __copy_from_user(&regs->u_regs[UREG_G1],
  137. &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
  138. regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
  139. /* Prevent syscall restart. */
  140. pt_regs_clear_syscall(regs);
  141. err |= __get_user(fpu_save, &sf->fpu_save);
  142. if (!err && fpu_save)
  143. err |= restore_fpu_state(regs, fpu_save);
  144. err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
  145. err |= restore_altstack(&sf->stack);
  146. if (err)
  147. goto segv;
  148. regs->pc = pc;
  149. regs->npc = npc;
  150. err |= __get_user(rwin_save, &sf->rwin_save);
  151. if (!err && rwin_save) {
  152. if (restore_rwin_state(rwin_save))
  153. goto segv;
  154. }
  155. set_current_blocked(&set);
  156. return;
  157. segv:
  158. force_sig(SIGSEGV, current);
  159. }
  160. static inline void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
  161. {
  162. unsigned long sp = regs->u_regs[UREG_FP];
  163. /*
  164. * If we are on the alternate signal stack and would overflow it, don't.
  165. * Return an always-bogus address instead so we will die with SIGSEGV.
  166. */
  167. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  168. return (void __user *) -1L;
  169. /* This is the X/Open sanctioned signal stack switching. */
  170. sp = sigsp(sp, ksig) - framesize;
  171. /* Always align the stack frame. This handles two cases. First,
  172. * sigaltstack need not be mindful of platform specific stack
  173. * alignment. Second, if we took this signal because the stack
  174. * is not aligned properly, we'd like to take the signal cleanly
  175. * and report that.
  176. */
  177. sp &= ~15UL;
  178. return (void __user *) sp;
  179. }
  180. static int setup_frame(struct ksignal *ksig, struct pt_regs *regs,
  181. sigset_t *oldset)
  182. {
  183. struct signal_frame __user *sf;
  184. int sigframe_size, err, wsaved;
  185. void __user *tail;
  186. /* 1. Make sure everything is clean */
  187. synchronize_user_stack();
  188. wsaved = current_thread_info()->w_saved;
  189. sigframe_size = sizeof(*sf);
  190. if (used_math())
  191. sigframe_size += sizeof(__siginfo_fpu_t);
  192. if (wsaved)
  193. sigframe_size += sizeof(__siginfo_rwin_t);
  194. sf = (struct signal_frame __user *)
  195. get_sigframe(ksig, regs, sigframe_size);
  196. if (invalid_frame_pointer(sf, sigframe_size)) {
  197. do_exit(SIGILL);
  198. return -EINVAL;
  199. }
  200. tail = sf + 1;
  201. /* 2. Save the current process state */
  202. err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
  203. err |= __put_user(0, &sf->extra_size);
  204. if (used_math()) {
  205. __siginfo_fpu_t __user *fp = tail;
  206. tail += sizeof(*fp);
  207. err |= save_fpu_state(regs, fp);
  208. err |= __put_user(fp, &sf->fpu_save);
  209. } else {
  210. err |= __put_user(0, &sf->fpu_save);
  211. }
  212. if (wsaved) {
  213. __siginfo_rwin_t __user *rwp = tail;
  214. tail += sizeof(*rwp);
  215. err |= save_rwin_state(wsaved, rwp);
  216. err |= __put_user(rwp, &sf->rwin_save);
  217. } else {
  218. err |= __put_user(0, &sf->rwin_save);
  219. }
  220. err |= __put_user(oldset->sig[0], &sf->info.si_mask);
  221. err |= __copy_to_user(sf->extramask, &oldset->sig[1],
  222. (_NSIG_WORDS - 1) * sizeof(unsigned int));
  223. if (!wsaved) {
  224. err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
  225. sizeof(struct reg_window32));
  226. } else {
  227. struct reg_window32 *rp;
  228. rp = &current_thread_info()->reg_window[wsaved - 1];
  229. err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
  230. }
  231. if (err)
  232. return err;
  233. /* 3. signal handler back-trampoline and parameters */
  234. regs->u_regs[UREG_FP] = (unsigned long) sf;
  235. regs->u_regs[UREG_I0] = ksig->sig;
  236. regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
  237. regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
  238. /* 4. signal handler */
  239. regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  240. regs->npc = (regs->pc + 4);
  241. /* 5. return to kernel instructions */
  242. if (ksig->ka.ka_restorer)
  243. regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
  244. else {
  245. regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
  246. /* mov __NR_sigreturn, %g1 */
  247. err |= __put_user(0x821020d8, &sf->insns[0]);
  248. /* t 0x10 */
  249. err |= __put_user(0x91d02010, &sf->insns[1]);
  250. if (err)
  251. return err;
  252. /* Flush instruction space. */
  253. flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
  254. }
  255. return 0;
  256. }
  257. static int setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs,
  258. sigset_t *oldset)
  259. {
  260. struct rt_signal_frame __user *sf;
  261. int sigframe_size, wsaved;
  262. void __user *tail;
  263. unsigned int psr;
  264. int err;
  265. synchronize_user_stack();
  266. wsaved = current_thread_info()->w_saved;
  267. sigframe_size = sizeof(*sf);
  268. if (used_math())
  269. sigframe_size += sizeof(__siginfo_fpu_t);
  270. if (wsaved)
  271. sigframe_size += sizeof(__siginfo_rwin_t);
  272. sf = (struct rt_signal_frame __user *)
  273. get_sigframe(ksig, regs, sigframe_size);
  274. if (invalid_frame_pointer(sf, sigframe_size)) {
  275. do_exit(SIGILL);
  276. return -EINVAL;
  277. }
  278. tail = sf + 1;
  279. err = __put_user(regs->pc, &sf->regs.pc);
  280. err |= __put_user(regs->npc, &sf->regs.npc);
  281. err |= __put_user(regs->y, &sf->regs.y);
  282. psr = regs->psr;
  283. if (used_math())
  284. psr |= PSR_EF;
  285. err |= __put_user(psr, &sf->regs.psr);
  286. err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
  287. err |= __put_user(0, &sf->extra_size);
  288. if (psr & PSR_EF) {
  289. __siginfo_fpu_t __user *fp = tail;
  290. tail += sizeof(*fp);
  291. err |= save_fpu_state(regs, fp);
  292. err |= __put_user(fp, &sf->fpu_save);
  293. } else {
  294. err |= __put_user(0, &sf->fpu_save);
  295. }
  296. if (wsaved) {
  297. __siginfo_rwin_t __user *rwp = tail;
  298. tail += sizeof(*rwp);
  299. err |= save_rwin_state(wsaved, rwp);
  300. err |= __put_user(rwp, &sf->rwin_save);
  301. } else {
  302. err |= __put_user(0, &sf->rwin_save);
  303. }
  304. err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
  305. /* Setup sigaltstack */
  306. err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
  307. if (!wsaved) {
  308. err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
  309. sizeof(struct reg_window32));
  310. } else {
  311. struct reg_window32 *rp;
  312. rp = &current_thread_info()->reg_window[wsaved - 1];
  313. err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
  314. }
  315. err |= copy_siginfo_to_user(&sf->info, &ksig->info);
  316. if (err)
  317. return err;
  318. regs->u_regs[UREG_FP] = (unsigned long) sf;
  319. regs->u_regs[UREG_I0] = ksig->sig;
  320. regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
  321. regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
  322. regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  323. regs->npc = (regs->pc + 4);
  324. if (ksig->ka.ka_restorer)
  325. regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
  326. else {
  327. regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
  328. /* mov __NR_sigreturn, %g1 */
  329. err |= __put_user(0x821020d8, &sf->insns[0]);
  330. /* t 0x10 */
  331. err |= __put_user(0x91d02010, &sf->insns[1]);
  332. if (err)
  333. return err;
  334. /* Flush instruction space. */
  335. flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
  336. }
  337. return 0;
  338. }
  339. static inline void
  340. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  341. {
  342. sigset_t *oldset = sigmask_to_save();
  343. int err;
  344. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  345. err = setup_rt_frame(ksig, regs, oldset);
  346. else
  347. err = setup_frame(ksig, regs, oldset);
  348. signal_setup_done(err, ksig, 0);
  349. }
  350. static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
  351. struct sigaction *sa)
  352. {
  353. switch(regs->u_regs[UREG_I0]) {
  354. case ERESTART_RESTARTBLOCK:
  355. case ERESTARTNOHAND:
  356. no_system_call_restart:
  357. regs->u_regs[UREG_I0] = EINTR;
  358. regs->psr |= PSR_C;
  359. break;
  360. case ERESTARTSYS:
  361. if (!(sa->sa_flags & SA_RESTART))
  362. goto no_system_call_restart;
  363. /* fallthrough */
  364. case ERESTARTNOINTR:
  365. regs->u_regs[UREG_I0] = orig_i0;
  366. regs->pc -= 4;
  367. regs->npc -= 4;
  368. }
  369. }
  370. /* Note that 'init' is a special process: it doesn't get signals it doesn't
  371. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  372. * mistake.
  373. */
  374. static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
  375. {
  376. struct ksignal ksig;
  377. int restart_syscall;
  378. bool has_handler;
  379. /* It's a lot of work and synchronization to add a new ptrace
  380. * register for GDB to save and restore in order to get
  381. * orig_i0 correct for syscall restarts when debugging.
  382. *
  383. * Although it should be the case that most of the global
  384. * registers are volatile across a system call, glibc already
  385. * depends upon that fact that we preserve them. So we can't
  386. * just use any global register to save away the orig_i0 value.
  387. *
  388. * In particular %g2, %g3, %g4, and %g5 are all assumed to be
  389. * preserved across a system call trap by various pieces of
  390. * code in glibc.
  391. *
  392. * %g7 is used as the "thread register". %g6 is not used in
  393. * any fixed manner. %g6 is used as a scratch register and
  394. * a compiler temporary, but it's value is never used across
  395. * a system call. Therefore %g6 is usable for orig_i0 storage.
  396. */
  397. if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
  398. regs->u_regs[UREG_G6] = orig_i0;
  399. has_handler = get_signal(&ksig);
  400. /* If the debugger messes with the program counter, it clears
  401. * the software "in syscall" bit, directing us to not perform
  402. * a syscall restart.
  403. */
  404. restart_syscall = 0;
  405. if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
  406. restart_syscall = 1;
  407. orig_i0 = regs->u_regs[UREG_G6];
  408. }
  409. if (has_handler) {
  410. if (restart_syscall)
  411. syscall_restart(orig_i0, regs, &ksig.ka.sa);
  412. handle_signal(&ksig, regs);
  413. } else {
  414. if (restart_syscall) {
  415. switch (regs->u_regs[UREG_I0]) {
  416. case ERESTARTNOHAND:
  417. case ERESTARTSYS:
  418. case ERESTARTNOINTR:
  419. /* replay the system call when we are done */
  420. regs->u_regs[UREG_I0] = orig_i0;
  421. regs->pc -= 4;
  422. regs->npc -= 4;
  423. pt_regs_clear_syscall(regs);
  424. case ERESTART_RESTARTBLOCK:
  425. regs->u_regs[UREG_G1] = __NR_restart_syscall;
  426. regs->pc -= 4;
  427. regs->npc -= 4;
  428. pt_regs_clear_syscall(regs);
  429. }
  430. }
  431. restore_saved_sigmask();
  432. }
  433. }
  434. void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
  435. unsigned long thread_info_flags)
  436. {
  437. if (thread_info_flags & _TIF_SIGPENDING)
  438. do_signal(regs, orig_i0);
  439. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  440. clear_thread_flag(TIF_NOTIFY_RESUME);
  441. tracehook_notify_resume(regs);
  442. }
  443. }
  444. asmlinkage int do_sys_sigstack(struct sigstack __user *ssptr,
  445. struct sigstack __user *ossptr,
  446. unsigned long sp)
  447. {
  448. int ret = -EFAULT;
  449. /* First see if old state is wanted. */
  450. if (ossptr) {
  451. if (put_user(current->sas_ss_sp + current->sas_ss_size,
  452. &ossptr->the_stack) ||
  453. __put_user(on_sig_stack(sp), &ossptr->cur_status))
  454. goto out;
  455. }
  456. /* Now see if we want to update the new state. */
  457. if (ssptr) {
  458. char *ss_sp;
  459. if (get_user(ss_sp, &ssptr->the_stack))
  460. goto out;
  461. /* If the current stack was set with sigaltstack, don't
  462. swap stacks while we are on it. */
  463. ret = -EPERM;
  464. if (current->sas_ss_sp && on_sig_stack(sp))
  465. goto out;
  466. /* Since we don't know the extent of the stack, and we don't
  467. track onstack-ness, but rather calculate it, we must
  468. presume a size. Ho hum this interface is lossy. */
  469. current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
  470. current->sas_ss_size = SIGSTKSZ;
  471. }
  472. ret = 0;
  473. out:
  474. return ret;
  475. }