signal.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * arch/ppc/kernel/signal.c
  3. *
  4. * PowerPC version
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. *
  7. * Derived from "arch/i386/kernel/signal.c"
  8. * Copyright (C) 1991, 1992 Linus Torvalds
  9. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/sched.h>
  17. #include <linux/mm.h>
  18. #include <linux/smp.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/kernel.h>
  21. #include <linux/signal.h>
  22. #include <linux/errno.h>
  23. #include <linux/wait.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/unistd.h>
  26. #include <linux/stddef.h>
  27. #include <linux/elf.h>
  28. #include <linux/tty.h>
  29. #include <linux/binfmts.h>
  30. #include <linux/suspend.h>
  31. #include <asm/ucontext.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/cacheflush.h>
  35. #undef DEBUG_SIG
  36. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  37. extern void sigreturn_exit(struct pt_regs *);
  38. #define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
  39. int do_signal(sigset_t *oldset, struct pt_regs *regs);
  40. /*
  41. * Atomically swap in the new signal mask, and wait for a signal.
  42. */
  43. int
  44. sys_sigsuspend(old_sigset_t mask, int p2, int p3, int p4, int p6, int p7,
  45. struct pt_regs *regs)
  46. {
  47. sigset_t saveset;
  48. mask &= _BLOCKABLE;
  49. spin_lock_irq(&current->sighand->siglock);
  50. saveset = current->blocked;
  51. siginitset(&current->blocked, mask);
  52. recalc_sigpending();
  53. spin_unlock_irq(&current->sighand->siglock);
  54. regs->result = -EINTR;
  55. regs->gpr[3] = EINTR;
  56. regs->ccr |= 0x10000000;
  57. while (1) {
  58. current->state = TASK_INTERRUPTIBLE;
  59. schedule();
  60. if (do_signal(&saveset, regs))
  61. sigreturn_exit(regs);
  62. }
  63. }
  64. int
  65. sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, int p3, int p4,
  66. int p6, int p7, struct pt_regs *regs)
  67. {
  68. sigset_t saveset, newset;
  69. /* XXX: Don't preclude handling different sized sigset_t's. */
  70. if (sigsetsize != sizeof(sigset_t))
  71. return -EINVAL;
  72. if (copy_from_user(&newset, unewset, sizeof(newset)))
  73. return -EFAULT;
  74. sigdelsetmask(&newset, ~_BLOCKABLE);
  75. spin_lock_irq(&current->sighand->siglock);
  76. saveset = current->blocked;
  77. current->blocked = newset;
  78. recalc_sigpending();
  79. spin_unlock_irq(&current->sighand->siglock);
  80. regs->result = -EINTR;
  81. regs->gpr[3] = EINTR;
  82. regs->ccr |= 0x10000000;
  83. while (1) {
  84. current->state = TASK_INTERRUPTIBLE;
  85. schedule();
  86. if (do_signal(&saveset, regs))
  87. sigreturn_exit(regs);
  88. }
  89. }
  90. int
  91. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss, int r5,
  92. int r6, int r7, int r8, struct pt_regs *regs)
  93. {
  94. return do_sigaltstack(uss, uoss, regs->gpr[1]);
  95. }
  96. int
  97. sys_sigaction(int sig, const struct old_sigaction __user *act,
  98. struct old_sigaction __user *oact)
  99. {
  100. struct k_sigaction new_ka, old_ka;
  101. int ret;
  102. if (act) {
  103. old_sigset_t mask;
  104. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  105. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  106. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  107. return -EFAULT;
  108. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  109. __get_user(mask, &act->sa_mask);
  110. siginitset(&new_ka.sa.sa_mask, mask);
  111. }
  112. ret = do_sigaction(sig, (act? &new_ka: NULL), (oact? &old_ka: NULL));
  113. if (!ret && oact) {
  114. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  115. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  116. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  117. return -EFAULT;
  118. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  119. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  120. }
  121. return ret;
  122. }
  123. /*
  124. * When we have signals to deliver, we set up on the
  125. * user stack, going down from the original stack pointer:
  126. * a sigregs struct
  127. * a sigcontext struct
  128. * a gap of __SIGNAL_FRAMESIZE bytes
  129. *
  130. * Each of these things must be a multiple of 16 bytes in size.
  131. *
  132. */
  133. struct sigregs {
  134. struct mcontext mctx; /* all the register values */
  135. /* Programs using the rs6000/xcoff abi can save up to 19 gp regs
  136. and 18 fp regs below sp before decrementing it. */
  137. int abigap[56];
  138. };
  139. /* We use the mc_pad field for the signal return trampoline. */
  140. #define tramp mc_pad
  141. /*
  142. * When we have rt signals to deliver, we set up on the
  143. * user stack, going down from the original stack pointer:
  144. * one rt_sigframe struct (siginfo + ucontext + ABI gap)
  145. * a gap of __SIGNAL_FRAMESIZE+16 bytes
  146. * (the +16 is to get the siginfo and ucontext in the same
  147. * positions as in older kernels).
  148. *
  149. * Each of these things must be a multiple of 16 bytes in size.
  150. *
  151. */
  152. struct rt_sigframe
  153. {
  154. struct siginfo info;
  155. struct ucontext uc;
  156. /* Programs using the rs6000/xcoff abi can save up to 19 gp regs
  157. and 18 fp regs below sp before decrementing it. */
  158. int abigap[56];
  159. };
  160. /*
  161. * Save the current user registers on the user stack.
  162. * We only save the altivec/spe registers if the process has used
  163. * altivec/spe instructions at some point.
  164. */
  165. static int
  166. save_user_regs(struct pt_regs *regs, struct mcontext __user *frame, int sigret)
  167. {
  168. /* save general and floating-point registers */
  169. CHECK_FULL_REGS(regs);
  170. preempt_disable();
  171. if (regs->msr & MSR_FP)
  172. giveup_fpu(current);
  173. #ifdef CONFIG_ALTIVEC
  174. if (current->thread.used_vr && (regs->msr & MSR_VEC))
  175. giveup_altivec(current);
  176. #endif /* CONFIG_ALTIVEC */
  177. #ifdef CONFIG_SPE
  178. if (current->thread.used_spe && (regs->msr & MSR_SPE))
  179. giveup_spe(current);
  180. #endif /* CONFIG_ALTIVEC */
  181. preempt_enable();
  182. if (__copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE)
  183. || __copy_to_user(&frame->mc_fregs, current->thread.fpr,
  184. ELF_NFPREG * sizeof(double)))
  185. return 1;
  186. current->thread.fpscr = 0; /* turn off all fp exceptions */
  187. #ifdef CONFIG_ALTIVEC
  188. /* save altivec registers */
  189. if (current->thread.used_vr) {
  190. if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
  191. ELF_NVRREG * sizeof(vector128)))
  192. return 1;
  193. /* set MSR_VEC in the saved MSR value to indicate that
  194. frame->mc_vregs contains valid data */
  195. if (__put_user(regs->msr | MSR_VEC, &frame->mc_gregs[PT_MSR]))
  196. return 1;
  197. }
  198. /* else assert((regs->msr & MSR_VEC) == 0) */
  199. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  200. * use altivec. Since VSCR only contains 32 bits saved in the least
  201. * significant bits of a vector, we "cheat" and stuff VRSAVE in the
  202. * most significant bits of that same vector. --BenH
  203. */
  204. if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
  205. return 1;
  206. #endif /* CONFIG_ALTIVEC */
  207. #ifdef CONFIG_SPE
  208. /* save spe registers */
  209. if (current->thread.used_spe) {
  210. if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
  211. ELF_NEVRREG * sizeof(u32)))
  212. return 1;
  213. /* set MSR_SPE in the saved MSR value to indicate that
  214. frame->mc_vregs contains valid data */
  215. if (__put_user(regs->msr | MSR_SPE, &frame->mc_gregs[PT_MSR]))
  216. return 1;
  217. }
  218. /* else assert((regs->msr & MSR_SPE) == 0) */
  219. /* We always copy to/from spefscr */
  220. if (__put_user(current->thread.spefscr, (u32 *)&frame->mc_vregs + ELF_NEVRREG))
  221. return 1;
  222. #endif /* CONFIG_SPE */
  223. if (sigret) {
  224. /* Set up the sigreturn trampoline: li r0,sigret; sc */
  225. if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
  226. || __put_user(0x44000002UL, &frame->tramp[1]))
  227. return 1;
  228. flush_icache_range((unsigned long) &frame->tramp[0],
  229. (unsigned long) &frame->tramp[2]);
  230. }
  231. return 0;
  232. }
  233. /*
  234. * Restore the current user register values from the user stack,
  235. * (except for MSR).
  236. */
  237. static int
  238. restore_user_regs(struct pt_regs *regs, struct mcontext __user *sr, int sig)
  239. {
  240. unsigned long save_r2 = 0;
  241. #if defined(CONFIG_ALTIVEC) || defined(CONFIG_SPE)
  242. unsigned long msr;
  243. #endif
  244. /* backup/restore the TLS as we don't want it to be modified */
  245. if (!sig)
  246. save_r2 = regs->gpr[2];
  247. /* copy up to but not including MSR */
  248. if (__copy_from_user(regs, &sr->mc_gregs, PT_MSR * sizeof(elf_greg_t)))
  249. return 1;
  250. /* copy from orig_r3 (the word after the MSR) up to the end */
  251. if (__copy_from_user(&regs->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
  252. GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
  253. return 1;
  254. if (!sig)
  255. regs->gpr[2] = save_r2;
  256. /* force the process to reload the FP registers from
  257. current->thread when it next does FP instructions */
  258. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  259. if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
  260. sizeof(sr->mc_fregs)))
  261. return 1;
  262. #ifdef CONFIG_ALTIVEC
  263. /* force the process to reload the altivec registers from
  264. current->thread when it next does altivec instructions */
  265. regs->msr &= ~MSR_VEC;
  266. if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_VEC) != 0) {
  267. /* restore altivec registers from the stack */
  268. if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
  269. sizeof(sr->mc_vregs)))
  270. return 1;
  271. } else if (current->thread.used_vr)
  272. memset(&current->thread.vr, 0, ELF_NVRREG * sizeof(vector128));
  273. /* Always get VRSAVE back */
  274. if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
  275. return 1;
  276. #endif /* CONFIG_ALTIVEC */
  277. #ifdef CONFIG_SPE
  278. /* force the process to reload the spe registers from
  279. current->thread when it next does spe instructions */
  280. regs->msr &= ~MSR_SPE;
  281. if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_SPE) != 0) {
  282. /* restore spe registers from the stack */
  283. if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
  284. ELF_NEVRREG * sizeof(u32)))
  285. return 1;
  286. } else if (current->thread.used_spe)
  287. memset(&current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
  288. /* Always get SPEFSCR back */
  289. if (__get_user(current->thread.spefscr, (u32 *)&sr->mc_vregs + ELF_NEVRREG))
  290. return 1;
  291. #endif /* CONFIG_SPE */
  292. #ifndef CONFIG_SMP
  293. preempt_disable();
  294. if (last_task_used_math == current)
  295. last_task_used_math = NULL;
  296. if (last_task_used_altivec == current)
  297. last_task_used_altivec = NULL;
  298. if (last_task_used_spe == current)
  299. last_task_used_spe = NULL;
  300. preempt_enable();
  301. #endif
  302. return 0;
  303. }
  304. /*
  305. * Restore the user process's signal mask
  306. */
  307. static void
  308. restore_sigmask(sigset_t *set)
  309. {
  310. sigdelsetmask(set, ~_BLOCKABLE);
  311. spin_lock_irq(&current->sighand->siglock);
  312. current->blocked = *set;
  313. recalc_sigpending();
  314. spin_unlock_irq(&current->sighand->siglock);
  315. }
  316. /*
  317. * Set up a signal frame for a "real-time" signal handler
  318. * (one which gets siginfo).
  319. */
  320. static void
  321. handle_rt_signal(unsigned long sig, struct k_sigaction *ka,
  322. siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
  323. unsigned long newsp)
  324. {
  325. struct rt_sigframe __user *rt_sf;
  326. struct mcontext __user *frame;
  327. unsigned long origsp = newsp;
  328. /* Set up Signal Frame */
  329. /* Put a Real Time Context onto stack */
  330. newsp -= sizeof(*rt_sf);
  331. rt_sf = (struct rt_sigframe __user *) newsp;
  332. /* create a stack frame for the caller of the handler */
  333. newsp -= __SIGNAL_FRAMESIZE + 16;
  334. if (!access_ok(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
  335. goto badframe;
  336. /* Put the siginfo & fill in most of the ucontext */
  337. if (copy_siginfo_to_user(&rt_sf->info, info)
  338. || __put_user(0, &rt_sf->uc.uc_flags)
  339. || __put_user(0, &rt_sf->uc.uc_link)
  340. || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
  341. || __put_user(sas_ss_flags(regs->gpr[1]),
  342. &rt_sf->uc.uc_stack.ss_flags)
  343. || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
  344. || __put_user(&rt_sf->uc.uc_mcontext, &rt_sf->uc.uc_regs)
  345. || __copy_to_user(&rt_sf->uc.uc_sigmask, oldset, sizeof(*oldset)))
  346. goto badframe;
  347. /* Save user registers on the stack */
  348. frame = &rt_sf->uc.uc_mcontext;
  349. if (save_user_regs(regs, frame, __NR_rt_sigreturn))
  350. goto badframe;
  351. if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
  352. goto badframe;
  353. regs->gpr[1] = newsp;
  354. regs->gpr[3] = sig;
  355. regs->gpr[4] = (unsigned long) &rt_sf->info;
  356. regs->gpr[5] = (unsigned long) &rt_sf->uc;
  357. regs->gpr[6] = (unsigned long) rt_sf;
  358. regs->nip = (unsigned long) ka->sa.sa_handler;
  359. regs->link = (unsigned long) frame->tramp;
  360. regs->trap = 0;
  361. return;
  362. badframe:
  363. #ifdef DEBUG_SIG
  364. printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
  365. regs, frame, newsp);
  366. #endif
  367. force_sigsegv(sig, current);
  368. }
  369. static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
  370. {
  371. sigset_t set;
  372. struct mcontext __user *mcp;
  373. if (__copy_from_user(&set, &ucp->uc_sigmask, sizeof(set))
  374. || __get_user(mcp, &ucp->uc_regs))
  375. return -EFAULT;
  376. restore_sigmask(&set);
  377. if (restore_user_regs(regs, mcp, sig))
  378. return -EFAULT;
  379. return 0;
  380. }
  381. int sys_swapcontext(struct ucontext __user *old_ctx,
  382. struct ucontext __user *new_ctx,
  383. int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
  384. {
  385. unsigned char tmp;
  386. /* Context size is for future use. Right now, we only make sure
  387. * we are passed something we understand
  388. */
  389. if (ctx_size < sizeof(struct ucontext))
  390. return -EINVAL;
  391. if (old_ctx != NULL) {
  392. if (!access_ok(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
  393. || save_user_regs(regs, &old_ctx->uc_mcontext, 0)
  394. || __copy_to_user(&old_ctx->uc_sigmask,
  395. &current->blocked, sizeof(sigset_t))
  396. || __put_user(&old_ctx->uc_mcontext, &old_ctx->uc_regs))
  397. return -EFAULT;
  398. }
  399. if (new_ctx == NULL)
  400. return 0;
  401. if (!access_ok(VERIFY_READ, new_ctx, sizeof(*new_ctx))
  402. || __get_user(tmp, (u8 __user *) new_ctx)
  403. || __get_user(tmp, (u8 __user *) (new_ctx + 1) - 1))
  404. return -EFAULT;
  405. /*
  406. * If we get a fault copying the context into the kernel's
  407. * image of the user's registers, we can't just return -EFAULT
  408. * because the user's registers will be corrupted. For instance
  409. * the NIP value may have been updated but not some of the
  410. * other registers. Given that we have done the access_ok
  411. * and successfully read the first and last bytes of the region
  412. * above, this should only happen in an out-of-memory situation
  413. * or if another thread unmaps the region containing the context.
  414. * We kill the task with a SIGSEGV in this situation.
  415. */
  416. if (do_setcontext(new_ctx, regs, 0))
  417. do_exit(SIGSEGV);
  418. sigreturn_exit(regs);
  419. /* doesn't actually return back to here */
  420. return 0;
  421. }
  422. int sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
  423. struct pt_regs *regs)
  424. {
  425. struct rt_sigframe __user *rt_sf;
  426. /* Always make any pending restarted system calls return -EINTR */
  427. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  428. rt_sf = (struct rt_sigframe __user *)
  429. (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
  430. if (!access_ok(VERIFY_READ, rt_sf, sizeof(struct rt_sigframe)))
  431. goto bad;
  432. if (do_setcontext(&rt_sf->uc, regs, 1))
  433. goto bad;
  434. /*
  435. * It's not clear whether or why it is desirable to save the
  436. * sigaltstack setting on signal delivery and restore it on
  437. * signal return. But other architectures do this and we have
  438. * always done it up until now so it is probably better not to
  439. * change it. -- paulus
  440. */
  441. do_sigaltstack(&rt_sf->uc.uc_stack, NULL, regs->gpr[1]);
  442. sigreturn_exit(regs); /* doesn't return here */
  443. return 0;
  444. bad:
  445. force_sig(SIGSEGV, current);
  446. return 0;
  447. }
  448. int sys_debug_setcontext(struct ucontext __user *ctx,
  449. int ndbg, struct sig_dbg_op __user *dbg,
  450. int r6, int r7, int r8,
  451. struct pt_regs *regs)
  452. {
  453. struct sig_dbg_op op;
  454. int i;
  455. unsigned long new_msr = regs->msr;
  456. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  457. unsigned long new_dbcr0 = current->thread.dbcr0;
  458. #endif
  459. for (i=0; i<ndbg; i++) {
  460. if (__copy_from_user(&op, dbg, sizeof(op)))
  461. return -EFAULT;
  462. switch (op.dbg_type) {
  463. case SIG_DBG_SINGLE_STEPPING:
  464. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  465. if (op.dbg_value) {
  466. new_msr |= MSR_DE;
  467. new_dbcr0 |= (DBCR0_IDM | DBCR0_IC);
  468. } else {
  469. new_msr &= ~MSR_DE;
  470. new_dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);
  471. }
  472. #else
  473. if (op.dbg_value)
  474. new_msr |= MSR_SE;
  475. else
  476. new_msr &= ~MSR_SE;
  477. #endif
  478. break;
  479. case SIG_DBG_BRANCH_TRACING:
  480. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  481. return -EINVAL;
  482. #else
  483. if (op.dbg_value)
  484. new_msr |= MSR_BE;
  485. else
  486. new_msr &= ~MSR_BE;
  487. #endif
  488. break;
  489. default:
  490. return -EINVAL;
  491. }
  492. }
  493. /* We wait until here to actually install the values in the
  494. registers so if we fail in the above loop, it will not
  495. affect the contents of these registers. After this point,
  496. failure is a problem, anyway, and it's very unlikely unless
  497. the user is really doing something wrong. */
  498. regs->msr = new_msr;
  499. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  500. current->thread.dbcr0 = new_dbcr0;
  501. #endif
  502. /*
  503. * If we get a fault copying the context into the kernel's
  504. * image of the user's registers, we can't just return -EFAULT
  505. * because the user's registers will be corrupted. For instance
  506. * the NIP value may have been updated but not some of the
  507. * other registers. Given that we have done the access_ok
  508. * and successfully read the first and last bytes of the region
  509. * above, this should only happen in an out-of-memory situation
  510. * or if another thread unmaps the region containing the context.
  511. * We kill the task with a SIGSEGV in this situation.
  512. */
  513. if (do_setcontext(ctx, regs, 1)) {
  514. force_sig(SIGSEGV, current);
  515. goto out;
  516. }
  517. /*
  518. * It's not clear whether or why it is desirable to save the
  519. * sigaltstack setting on signal delivery and restore it on
  520. * signal return. But other architectures do this and we have
  521. * always done it up until now so it is probably better not to
  522. * change it. -- paulus
  523. */
  524. do_sigaltstack(&ctx->uc_stack, NULL, regs->gpr[1]);
  525. sigreturn_exit(regs);
  526. /* doesn't actually return back to here */
  527. out:
  528. return 0;
  529. }
  530. /*
  531. * OK, we're invoking a handler
  532. */
  533. static void
  534. handle_signal(unsigned long sig, struct k_sigaction *ka,
  535. siginfo_t *info, sigset_t *oldset, struct pt_regs * regs,
  536. unsigned long newsp)
  537. {
  538. struct sigcontext __user *sc;
  539. struct sigregs __user *frame;
  540. unsigned long origsp = newsp;
  541. /* Set up Signal Frame */
  542. newsp -= sizeof(struct sigregs);
  543. frame = (struct sigregs __user *) newsp;
  544. /* Put a sigcontext on the stack */
  545. newsp -= sizeof(*sc);
  546. sc = (struct sigcontext __user *) newsp;
  547. /* create a stack frame for the caller of the handler */
  548. newsp -= __SIGNAL_FRAMESIZE;
  549. if (!access_ok(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
  550. goto badframe;
  551. #if _NSIG != 64
  552. #error "Please adjust handle_signal()"
  553. #endif
  554. if (__put_user((unsigned long) ka->sa.sa_handler, &sc->handler)
  555. || __put_user(oldset->sig[0], &sc->oldmask)
  556. || __put_user(oldset->sig[1], &sc->_unused[3])
  557. || __put_user((struct pt_regs __user *)frame, &sc->regs)
  558. || __put_user(sig, &sc->signal))
  559. goto badframe;
  560. if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
  561. goto badframe;
  562. if (put_user(regs->gpr[1], (unsigned long __user *)newsp))
  563. goto badframe;
  564. regs->gpr[1] = newsp;
  565. regs->gpr[3] = sig;
  566. regs->gpr[4] = (unsigned long) sc;
  567. regs->nip = (unsigned long) ka->sa.sa_handler;
  568. regs->link = (unsigned long) frame->mctx.tramp;
  569. regs->trap = 0;
  570. return;
  571. badframe:
  572. #ifdef DEBUG_SIG
  573. printk("badframe in handle_signal, regs=%p frame=%p newsp=%lx\n",
  574. regs, frame, newsp);
  575. #endif
  576. force_sigsegv(sig, current);
  577. }
  578. /*
  579. * Do a signal return; undo the signal stack.
  580. */
  581. int sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
  582. struct pt_regs *regs)
  583. {
  584. struct sigcontext __user *sc;
  585. struct sigcontext sigctx;
  586. struct mcontext __user *sr;
  587. sigset_t set;
  588. /* Always make any pending restarted system calls return -EINTR */
  589. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  590. sc = (struct sigcontext __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
  591. if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
  592. goto badframe;
  593. set.sig[0] = sigctx.oldmask;
  594. set.sig[1] = sigctx._unused[3];
  595. restore_sigmask(&set);
  596. sr = (struct mcontext __user *) sigctx.regs;
  597. if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
  598. || restore_user_regs(regs, sr, 1))
  599. goto badframe;
  600. sigreturn_exit(regs); /* doesn't return */
  601. return 0;
  602. badframe:
  603. force_sig(SIGSEGV, current);
  604. return 0;
  605. }
  606. /*
  607. * Note that 'init' is a special process: it doesn't get signals it doesn't
  608. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  609. * mistake.
  610. */
  611. int do_signal(sigset_t *oldset, struct pt_regs *regs)
  612. {
  613. siginfo_t info;
  614. struct k_sigaction ka;
  615. unsigned long frame, newsp;
  616. int signr, ret;
  617. if (try_to_freeze()) {
  618. signr = 0;
  619. if (!signal_pending(current))
  620. goto no_signal;
  621. }
  622. if (!oldset)
  623. oldset = &current->blocked;
  624. newsp = frame = 0;
  625. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  626. no_signal:
  627. if (TRAP(regs) == 0x0C00 /* System Call! */
  628. && regs->ccr & 0x10000000 /* error signalled */
  629. && ((ret = regs->gpr[3]) == ERESTARTSYS
  630. || ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
  631. || ret == ERESTART_RESTARTBLOCK)) {
  632. if (signr > 0
  633. && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
  634. || (ret == ERESTARTSYS
  635. && !(ka.sa.sa_flags & SA_RESTART)))) {
  636. /* make the system call return an EINTR error */
  637. regs->result = -EINTR;
  638. regs->gpr[3] = EINTR;
  639. /* note that the cr0.SO bit is already set */
  640. } else {
  641. regs->nip -= 4; /* Back up & retry system call */
  642. regs->result = 0;
  643. regs->trap = 0;
  644. if (ret == ERESTART_RESTARTBLOCK)
  645. regs->gpr[0] = __NR_restart_syscall;
  646. else
  647. regs->gpr[3] = regs->orig_gpr3;
  648. }
  649. }
  650. if (signr == 0)
  651. return 0; /* no signals delivered */
  652. if ((ka.sa.sa_flags & SA_ONSTACK) && current->sas_ss_size
  653. && !on_sig_stack(regs->gpr[1]))
  654. newsp = current->sas_ss_sp + current->sas_ss_size;
  655. else
  656. newsp = regs->gpr[1];
  657. newsp &= ~0xfUL;
  658. /* Whee! Actually deliver the signal. */
  659. if (ka.sa.sa_flags & SA_SIGINFO)
  660. handle_rt_signal(signr, &ka, &info, oldset, regs, newsp);
  661. else
  662. handle_signal(signr, &ka, &info, oldset, regs, newsp);
  663. spin_lock_irq(&current->sighand->siglock);
  664. sigorsets(&current->blocked,&current->blocked,&ka.sa.sa_mask);
  665. if (!(ka.sa.sa_flags & SA_NODEFER))
  666. sigaddset(&current->blocked, signr);
  667. recalc_sigpending();
  668. spin_unlock_irq(&current->sighand->siglock);
  669. return 1;
  670. }