signal.c 20 KB

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