signal.c 19 KB

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