signal_32.c 15 KB

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