signal.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * linux/arch/arm/kernel/signal.c
  3. *
  4. * Copyright (C) 1995-2009 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/signal.h>
  12. #include <linux/personality.h>
  13. #include <linux/freezer.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/tracehook.h>
  16. #include <asm/elf.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/ucontext.h>
  19. #include <asm/unistd.h>
  20. #include <asm/vfp.h>
  21. #include "ptrace.h"
  22. #include "signal.h"
  23. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  24. /*
  25. * For ARM syscalls, we encode the syscall number into the instruction.
  26. */
  27. #define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
  28. #define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
  29. #define SWI_SYS_RESTART (0xef000000|__NR_restart_syscall|__NR_OABI_SYSCALL_BASE)
  30. /*
  31. * With EABI, the syscall number has to be loaded into r7.
  32. */
  33. #define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
  34. #define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
  35. /*
  36. * For Thumb syscalls, we pass the syscall number via r7. We therefore
  37. * need two 16-bit instructions.
  38. */
  39. #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
  40. #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
  41. const unsigned long sigreturn_codes[7] = {
  42. MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
  43. MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
  44. };
  45. /*
  46. * Either we support OABI only, or we have EABI with the OABI
  47. * compat layer enabled. In the later case we don't know if
  48. * user space is EABI or not, and if not we must not clobber r7.
  49. * Always using the OABI syscall solves that issue and works for
  50. * all those cases.
  51. */
  52. const unsigned long syscall_restart_code[2] = {
  53. SWI_SYS_RESTART, /* swi __NR_restart_syscall */
  54. 0xe49df004, /* ldr pc, [sp], #4 */
  55. };
  56. /*
  57. * atomically swap in the new signal mask, and wait for a signal.
  58. */
  59. asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask)
  60. {
  61. mask &= _BLOCKABLE;
  62. spin_lock_irq(&current->sighand->siglock);
  63. current->saved_sigmask = current->blocked;
  64. siginitset(&current->blocked, mask);
  65. recalc_sigpending();
  66. spin_unlock_irq(&current->sighand->siglock);
  67. current->state = TASK_INTERRUPTIBLE;
  68. schedule();
  69. set_restore_sigmask();
  70. return -ERESTARTNOHAND;
  71. }
  72. asmlinkage int
  73. sys_sigaction(int sig, const struct old_sigaction __user *act,
  74. struct old_sigaction __user *oact)
  75. {
  76. struct k_sigaction new_ka, old_ka;
  77. int ret;
  78. if (act) {
  79. old_sigset_t mask;
  80. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  81. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  82. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  83. return -EFAULT;
  84. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  85. __get_user(mask, &act->sa_mask);
  86. siginitset(&new_ka.sa.sa_mask, mask);
  87. }
  88. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  89. if (!ret && oact) {
  90. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  91. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  92. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  93. return -EFAULT;
  94. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  95. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  96. }
  97. return ret;
  98. }
  99. #ifdef CONFIG_CRUNCH
  100. static int preserve_crunch_context(struct crunch_sigframe __user *frame)
  101. {
  102. char kbuf[sizeof(*frame) + 8];
  103. struct crunch_sigframe *kframe;
  104. /* the crunch context must be 64 bit aligned */
  105. kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
  106. kframe->magic = CRUNCH_MAGIC;
  107. kframe->size = CRUNCH_STORAGE_SIZE;
  108. crunch_task_copy(current_thread_info(), &kframe->storage);
  109. return __copy_to_user(frame, kframe, sizeof(*frame));
  110. }
  111. static int restore_crunch_context(struct crunch_sigframe __user *frame)
  112. {
  113. char kbuf[sizeof(*frame) + 8];
  114. struct crunch_sigframe *kframe;
  115. /* the crunch context must be 64 bit aligned */
  116. kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
  117. if (__copy_from_user(kframe, frame, sizeof(*frame)))
  118. return -1;
  119. if (kframe->magic != CRUNCH_MAGIC ||
  120. kframe->size != CRUNCH_STORAGE_SIZE)
  121. return -1;
  122. crunch_task_restore(current_thread_info(), &kframe->storage);
  123. return 0;
  124. }
  125. #endif
  126. #ifdef CONFIG_IWMMXT
  127. static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
  128. {
  129. char kbuf[sizeof(*frame) + 8];
  130. struct iwmmxt_sigframe *kframe;
  131. /* the iWMMXt context must be 64 bit aligned */
  132. kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
  133. kframe->magic = IWMMXT_MAGIC;
  134. kframe->size = IWMMXT_STORAGE_SIZE;
  135. iwmmxt_task_copy(current_thread_info(), &kframe->storage);
  136. return __copy_to_user(frame, kframe, sizeof(*frame));
  137. }
  138. static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
  139. {
  140. char kbuf[sizeof(*frame) + 8];
  141. struct iwmmxt_sigframe *kframe;
  142. /* the iWMMXt context must be 64 bit aligned */
  143. kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
  144. if (__copy_from_user(kframe, frame, sizeof(*frame)))
  145. return -1;
  146. if (kframe->magic != IWMMXT_MAGIC ||
  147. kframe->size != IWMMXT_STORAGE_SIZE)
  148. return -1;
  149. iwmmxt_task_restore(current_thread_info(), &kframe->storage);
  150. return 0;
  151. }
  152. #endif
  153. #ifdef CONFIG_VFP
  154. static int preserve_vfp_context(struct vfp_sigframe __user *frame)
  155. {
  156. struct thread_info *thread = current_thread_info();
  157. struct vfp_hard_struct *h = &thread->vfpstate.hard;
  158. const unsigned long magic = VFP_MAGIC;
  159. const unsigned long size = VFP_STORAGE_SIZE;
  160. int err = 0;
  161. vfp_sync_hwstate(thread);
  162. __put_user_error(magic, &frame->magic, err);
  163. __put_user_error(size, &frame->size, err);
  164. /*
  165. * Copy the floating point registers. There can be unused
  166. * registers see asm/hwcap.h for details.
  167. */
  168. err |= __copy_to_user(&frame->ufp.fpregs, &h->fpregs,
  169. sizeof(h->fpregs));
  170. /*
  171. * Copy the status and control register.
  172. */
  173. __put_user_error(h->fpscr, &frame->ufp.fpscr, err);
  174. /*
  175. * Copy the exception registers.
  176. */
  177. __put_user_error(h->fpexc, &frame->ufp_exc.fpexc, err);
  178. __put_user_error(h->fpinst, &frame->ufp_exc.fpinst, err);
  179. __put_user_error(h->fpinst2, &frame->ufp_exc.fpinst2, err);
  180. return err ? -EFAULT : 0;
  181. }
  182. static int restore_vfp_context(struct vfp_sigframe __user *frame)
  183. {
  184. struct thread_info *thread = current_thread_info();
  185. struct vfp_hard_struct *h = &thread->vfpstate.hard;
  186. unsigned long magic;
  187. unsigned long size;
  188. unsigned long fpexc;
  189. int err = 0;
  190. __get_user_error(magic, &frame->magic, err);
  191. __get_user_error(size, &frame->size, err);
  192. if (err)
  193. return -EFAULT;
  194. if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
  195. return -EINVAL;
  196. /*
  197. * Copy the floating point registers. There can be unused
  198. * registers see asm/hwcap.h for details.
  199. */
  200. err |= __copy_from_user(&h->fpregs, &frame->ufp.fpregs,
  201. sizeof(h->fpregs));
  202. /*
  203. * Copy the status and control register.
  204. */
  205. __get_user_error(h->fpscr, &frame->ufp.fpscr, err);
  206. /*
  207. * Sanitise and restore the exception registers.
  208. */
  209. __get_user_error(fpexc, &frame->ufp_exc.fpexc, err);
  210. /* Ensure the VFP is enabled. */
  211. fpexc |= FPEXC_EN;
  212. /* Ensure FPINST2 is invalid and the exception flag is cleared. */
  213. fpexc &= ~(FPEXC_EX | FPEXC_FP2V);
  214. h->fpexc = fpexc;
  215. __get_user_error(h->fpinst, &frame->ufp_exc.fpinst, err);
  216. __get_user_error(h->fpinst2, &frame->ufp_exc.fpinst2, err);
  217. if (!err)
  218. vfp_flush_hwstate(thread);
  219. return err ? -EFAULT : 0;
  220. }
  221. #endif
  222. /*
  223. * Do a signal return; undo the signal stack. These are aligned to 64-bit.
  224. */
  225. struct sigframe {
  226. struct ucontext uc;
  227. unsigned long retcode[2];
  228. };
  229. struct rt_sigframe {
  230. struct siginfo info;
  231. struct sigframe sig;
  232. };
  233. static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
  234. {
  235. struct aux_sigframe __user *aux;
  236. sigset_t set;
  237. int err;
  238. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  239. if (err == 0) {
  240. sigdelsetmask(&set, ~_BLOCKABLE);
  241. spin_lock_irq(&current->sighand->siglock);
  242. current->blocked = set;
  243. recalc_sigpending();
  244. spin_unlock_irq(&current->sighand->siglock);
  245. }
  246. __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
  247. __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
  248. __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
  249. __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
  250. __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
  251. __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
  252. __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
  253. __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
  254. __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
  255. __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
  256. __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
  257. __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
  258. __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
  259. __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
  260. __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
  261. __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
  262. __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
  263. err |= !valid_user_regs(regs);
  264. aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
  265. #ifdef CONFIG_CRUNCH
  266. if (err == 0)
  267. err |= restore_crunch_context(&aux->crunch);
  268. #endif
  269. #ifdef CONFIG_IWMMXT
  270. if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
  271. err |= restore_iwmmxt_context(&aux->iwmmxt);
  272. #endif
  273. #ifdef CONFIG_VFP
  274. if (err == 0)
  275. err |= restore_vfp_context(&aux->vfp);
  276. #endif
  277. return err;
  278. }
  279. asmlinkage int sys_sigreturn(struct pt_regs *regs)
  280. {
  281. struct sigframe __user *frame;
  282. /* Always make any pending restarted system calls return -EINTR */
  283. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  284. /*
  285. * Since we stacked the signal on a 64-bit boundary,
  286. * then 'sp' should be word aligned here. If it's
  287. * not, then the user is trying to mess with us.
  288. */
  289. if (regs->ARM_sp & 7)
  290. goto badframe;
  291. frame = (struct sigframe __user *)regs->ARM_sp;
  292. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  293. goto badframe;
  294. if (restore_sigframe(regs, frame))
  295. goto badframe;
  296. single_step_trap(current);
  297. return regs->ARM_r0;
  298. badframe:
  299. force_sig(SIGSEGV, current);
  300. return 0;
  301. }
  302. asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
  303. {
  304. struct rt_sigframe __user *frame;
  305. /* Always make any pending restarted system calls return -EINTR */
  306. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  307. /*
  308. * Since we stacked the signal on a 64-bit boundary,
  309. * then 'sp' should be word aligned here. If it's
  310. * not, then the user is trying to mess with us.
  311. */
  312. if (regs->ARM_sp & 7)
  313. goto badframe;
  314. frame = (struct rt_sigframe __user *)regs->ARM_sp;
  315. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  316. goto badframe;
  317. if (restore_sigframe(regs, &frame->sig))
  318. goto badframe;
  319. if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
  320. goto badframe;
  321. single_step_trap(current);
  322. return regs->ARM_r0;
  323. badframe:
  324. force_sig(SIGSEGV, current);
  325. return 0;
  326. }
  327. static int
  328. setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
  329. {
  330. struct aux_sigframe __user *aux;
  331. int err = 0;
  332. __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
  333. __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
  334. __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
  335. __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
  336. __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
  337. __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
  338. __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
  339. __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
  340. __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
  341. __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
  342. __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
  343. __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
  344. __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
  345. __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
  346. __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
  347. __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
  348. __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
  349. __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
  350. __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
  351. __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
  352. __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
  353. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
  354. aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
  355. #ifdef CONFIG_CRUNCH
  356. if (err == 0)
  357. err |= preserve_crunch_context(&aux->crunch);
  358. #endif
  359. #ifdef CONFIG_IWMMXT
  360. if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
  361. err |= preserve_iwmmxt_context(&aux->iwmmxt);
  362. #endif
  363. #ifdef CONFIG_VFP
  364. if (err == 0)
  365. err |= preserve_vfp_context(&aux->vfp);
  366. #endif
  367. __put_user_error(0, &aux->end_magic, err);
  368. return err;
  369. }
  370. static inline void __user *
  371. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
  372. {
  373. unsigned long sp = regs->ARM_sp;
  374. void __user *frame;
  375. /*
  376. * This is the X/Open sanctioned signal stack switching.
  377. */
  378. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
  379. sp = current->sas_ss_sp + current->sas_ss_size;
  380. /*
  381. * ATPCS B01 mandates 8-byte alignment
  382. */
  383. frame = (void __user *)((sp - framesize) & ~7);
  384. /*
  385. * Check that we can actually write to the signal frame.
  386. */
  387. if (!access_ok(VERIFY_WRITE, frame, framesize))
  388. frame = NULL;
  389. return frame;
  390. }
  391. static int
  392. setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  393. unsigned long __user *rc, void __user *frame, int usig)
  394. {
  395. unsigned long handler = (unsigned long)ka->sa.sa_handler;
  396. unsigned long retcode;
  397. int thumb = 0;
  398. unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
  399. /*
  400. * Maybe we need to deliver a 32-bit signal to a 26-bit task.
  401. */
  402. if (ka->sa.sa_flags & SA_THIRTYTWO)
  403. cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
  404. #ifdef CONFIG_ARM_THUMB
  405. if (elf_hwcap & HWCAP_THUMB) {
  406. /*
  407. * The LSB of the handler determines if we're going to
  408. * be using THUMB or ARM mode for this signal handler.
  409. */
  410. thumb = handler & 1;
  411. if (thumb) {
  412. cpsr |= PSR_T_BIT;
  413. #if __LINUX_ARM_ARCH__ >= 7
  414. /* clear the If-Then Thumb-2 execution state */
  415. cpsr &= ~PSR_IT_MASK;
  416. #endif
  417. } else
  418. cpsr &= ~PSR_T_BIT;
  419. }
  420. #endif
  421. if (ka->sa.sa_flags & SA_RESTORER) {
  422. retcode = (unsigned long)ka->sa.sa_restorer;
  423. } else {
  424. unsigned int idx = thumb << 1;
  425. if (ka->sa.sa_flags & SA_SIGINFO)
  426. idx += 3;
  427. if (__put_user(sigreturn_codes[idx], rc) ||
  428. __put_user(sigreturn_codes[idx+1], rc+1))
  429. return 1;
  430. if (cpsr & MODE32_BIT) {
  431. /*
  432. * 32-bit code can use the new high-page
  433. * signal return code support.
  434. */
  435. retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
  436. } else {
  437. /*
  438. * Ensure that the instruction cache sees
  439. * the return code written onto the stack.
  440. */
  441. flush_icache_range((unsigned long)rc,
  442. (unsigned long)(rc + 2));
  443. retcode = ((unsigned long)rc) + thumb;
  444. }
  445. }
  446. regs->ARM_r0 = usig;
  447. regs->ARM_sp = (unsigned long)frame;
  448. regs->ARM_lr = retcode;
  449. regs->ARM_pc = handler;
  450. regs->ARM_cpsr = cpsr;
  451. return 0;
  452. }
  453. static int
  454. setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
  455. {
  456. struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
  457. int err = 0;
  458. if (!frame)
  459. return 1;
  460. /*
  461. * Set uc.uc_flags to a value which sc.trap_no would never have.
  462. */
  463. __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
  464. err |= setup_sigframe(frame, regs, set);
  465. if (err == 0)
  466. err = setup_return(regs, ka, frame->retcode, frame, usig);
  467. return err;
  468. }
  469. static int
  470. setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
  471. sigset_t *set, struct pt_regs *regs)
  472. {
  473. struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
  474. stack_t stack;
  475. int err = 0;
  476. if (!frame)
  477. return 1;
  478. err |= copy_siginfo_to_user(&frame->info, info);
  479. __put_user_error(0, &frame->sig.uc.uc_flags, err);
  480. __put_user_error(NULL, &frame->sig.uc.uc_link, err);
  481. memset(&stack, 0, sizeof(stack));
  482. stack.ss_sp = (void __user *)current->sas_ss_sp;
  483. stack.ss_flags = sas_ss_flags(regs->ARM_sp);
  484. stack.ss_size = current->sas_ss_size;
  485. err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
  486. err |= setup_sigframe(&frame->sig, regs, set);
  487. if (err == 0)
  488. err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
  489. if (err == 0) {
  490. /*
  491. * For realtime signals we must also set the second and third
  492. * arguments for the signal handler.
  493. * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
  494. */
  495. regs->ARM_r1 = (unsigned long)&frame->info;
  496. regs->ARM_r2 = (unsigned long)&frame->sig.uc;
  497. }
  498. return err;
  499. }
  500. static inline void setup_syscall_restart(struct pt_regs *regs)
  501. {
  502. regs->ARM_r0 = regs->ARM_ORIG_r0;
  503. regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
  504. }
  505. /*
  506. * OK, we're invoking a handler
  507. */
  508. static int
  509. handle_signal(unsigned long sig, struct k_sigaction *ka,
  510. siginfo_t *info, sigset_t *oldset,
  511. struct pt_regs * regs, int syscall)
  512. {
  513. struct thread_info *thread = current_thread_info();
  514. struct task_struct *tsk = current;
  515. int usig = sig;
  516. int ret;
  517. /*
  518. * If we were from a system call, check for system call restarting...
  519. */
  520. if (syscall) {
  521. switch (regs->ARM_r0) {
  522. case -ERESTART_RESTARTBLOCK:
  523. case -ERESTARTNOHAND:
  524. regs->ARM_r0 = -EINTR;
  525. break;
  526. case -ERESTARTSYS:
  527. if (!(ka->sa.sa_flags & SA_RESTART)) {
  528. regs->ARM_r0 = -EINTR;
  529. break;
  530. }
  531. /* fallthrough */
  532. case -ERESTARTNOINTR:
  533. setup_syscall_restart(regs);
  534. }
  535. }
  536. /*
  537. * translate the signal
  538. */
  539. if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
  540. usig = thread->exec_domain->signal_invmap[usig];
  541. /*
  542. * Set up the stack frame
  543. */
  544. if (ka->sa.sa_flags & SA_SIGINFO)
  545. ret = setup_rt_frame(usig, ka, info, oldset, regs);
  546. else
  547. ret = setup_frame(usig, ka, oldset, regs);
  548. /*
  549. * Check that the resulting registers are actually sane.
  550. */
  551. ret |= !valid_user_regs(regs);
  552. if (ret != 0) {
  553. force_sigsegv(sig, tsk);
  554. return ret;
  555. }
  556. /*
  557. * Block the signal if we were successful.
  558. */
  559. spin_lock_irq(&tsk->sighand->siglock);
  560. sigorsets(&tsk->blocked, &tsk->blocked,
  561. &ka->sa.sa_mask);
  562. if (!(ka->sa.sa_flags & SA_NODEFER))
  563. sigaddset(&tsk->blocked, sig);
  564. recalc_sigpending();
  565. spin_unlock_irq(&tsk->sighand->siglock);
  566. return 0;
  567. }
  568. /*
  569. * Note that 'init' is a special process: it doesn't get signals it doesn't
  570. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  571. * mistake.
  572. *
  573. * Note that we go through the signals twice: once to check the signals that
  574. * the kernel can handle, and then we build all the user-level signal handling
  575. * stack-frames in one go after that.
  576. */
  577. static void do_signal(struct pt_regs *regs, int syscall)
  578. {
  579. struct k_sigaction ka;
  580. siginfo_t info;
  581. int signr;
  582. /*
  583. * We want the common case to go fast, which
  584. * is why we may in certain cases get here from
  585. * kernel mode. Just return without doing anything
  586. * if so.
  587. */
  588. if (!user_mode(regs))
  589. return;
  590. if (try_to_freeze())
  591. goto no_signal;
  592. single_step_clear(current);
  593. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  594. if (signr > 0) {
  595. sigset_t *oldset;
  596. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  597. oldset = &current->saved_sigmask;
  598. else
  599. oldset = &current->blocked;
  600. if (handle_signal(signr, &ka, &info, oldset, regs, syscall) == 0) {
  601. /*
  602. * A signal was successfully delivered; the saved
  603. * sigmask will have been stored in the signal frame,
  604. * and will be restored by sigreturn, so we can simply
  605. * clear the TIF_RESTORE_SIGMASK flag.
  606. */
  607. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  608. clear_thread_flag(TIF_RESTORE_SIGMASK);
  609. }
  610. single_step_set(current);
  611. return;
  612. }
  613. no_signal:
  614. /*
  615. * No signal to deliver to the process - restart the syscall.
  616. */
  617. if (syscall) {
  618. if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
  619. if (thumb_mode(regs)) {
  620. regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
  621. regs->ARM_pc -= 2;
  622. } else {
  623. #if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
  624. regs->ARM_r7 = __NR_restart_syscall;
  625. regs->ARM_pc -= 4;
  626. #else
  627. u32 __user *usp;
  628. regs->ARM_sp -= 4;
  629. usp = (u32 __user *)regs->ARM_sp;
  630. if (put_user(regs->ARM_pc, usp) == 0) {
  631. regs->ARM_pc = KERN_RESTART_CODE;
  632. } else {
  633. regs->ARM_sp += 4;
  634. force_sigsegv(0, current);
  635. }
  636. #endif
  637. }
  638. }
  639. if (regs->ARM_r0 == -ERESTARTNOHAND ||
  640. regs->ARM_r0 == -ERESTARTSYS ||
  641. regs->ARM_r0 == -ERESTARTNOINTR) {
  642. setup_syscall_restart(regs);
  643. }
  644. /* If there's no signal to deliver, we just put the saved sigmask
  645. * back.
  646. */
  647. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  648. clear_thread_flag(TIF_RESTORE_SIGMASK);
  649. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  650. }
  651. }
  652. single_step_set(current);
  653. }
  654. asmlinkage void
  655. do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  656. {
  657. if (thread_flags & _TIF_SIGPENDING)
  658. do_signal(regs, syscall);
  659. if (thread_flags & _TIF_NOTIFY_RESUME) {
  660. clear_thread_flag(TIF_NOTIFY_RESUME);
  661. tracehook_notify_resume(regs);
  662. if (current->replacement_session_keyring)
  663. key_replace_session_keyring();
  664. }
  665. }