signal.c 21 KB

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