signal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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/random.h>
  12. #include <linux/signal.h>
  13. #include <linux/personality.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/tracehook.h>
  16. #include <linux/uprobes.h>
  17. #include <linux/syscalls.h>
  18. #include <asm/elf.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/traps.h>
  21. #include <asm/unistd.h>
  22. #include <asm/vfp.h>
  23. #include "signal.h"
  24. extern const unsigned long sigreturn_codes[17];
  25. static unsigned long signal_return_offset;
  26. #ifdef CONFIG_CRUNCH
  27. static int preserve_crunch_context(struct crunch_sigframe __user *frame)
  28. {
  29. char kbuf[sizeof(*frame) + 8];
  30. struct crunch_sigframe *kframe;
  31. /* the crunch context must be 64 bit aligned */
  32. kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
  33. kframe->magic = CRUNCH_MAGIC;
  34. kframe->size = CRUNCH_STORAGE_SIZE;
  35. crunch_task_copy(current_thread_info(), &kframe->storage);
  36. return __copy_to_user(frame, kframe, sizeof(*frame));
  37. }
  38. static int restore_crunch_context(char __user **auxp)
  39. {
  40. struct crunch_sigframe __user *frame =
  41. (struct crunch_sigframe __user *)*auxp;
  42. char kbuf[sizeof(*frame) + 8];
  43. struct crunch_sigframe *kframe;
  44. /* the crunch context must be 64 bit aligned */
  45. kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
  46. if (__copy_from_user(kframe, frame, sizeof(*frame)))
  47. return -1;
  48. if (kframe->magic != CRUNCH_MAGIC ||
  49. kframe->size != CRUNCH_STORAGE_SIZE)
  50. return -1;
  51. *auxp += CRUNCH_STORAGE_SIZE;
  52. crunch_task_restore(current_thread_info(), &kframe->storage);
  53. return 0;
  54. }
  55. #endif
  56. #ifdef CONFIG_IWMMXT
  57. static int preserve_iwmmxt_context(struct iwmmxt_sigframe __user *frame)
  58. {
  59. char kbuf[sizeof(*frame) + 8];
  60. struct iwmmxt_sigframe *kframe;
  61. int err = 0;
  62. /* the iWMMXt context must be 64 bit aligned */
  63. kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
  64. if (test_thread_flag(TIF_USING_IWMMXT)) {
  65. kframe->magic = IWMMXT_MAGIC;
  66. kframe->size = IWMMXT_STORAGE_SIZE;
  67. iwmmxt_task_copy(current_thread_info(), &kframe->storage);
  68. err = __copy_to_user(frame, kframe, sizeof(*frame));
  69. } else {
  70. /*
  71. * For bug-compatibility with older kernels, some space
  72. * has to be reserved for iWMMXt even if it's not used.
  73. * Set the magic and size appropriately so that properly
  74. * written userspace can skip it reliably:
  75. */
  76. __put_user_error(DUMMY_MAGIC, &frame->magic, err);
  77. __put_user_error(IWMMXT_STORAGE_SIZE, &frame->size, err);
  78. }
  79. return err;
  80. }
  81. static int restore_iwmmxt_context(char __user **auxp)
  82. {
  83. struct iwmmxt_sigframe __user *frame =
  84. (struct iwmmxt_sigframe __user *)*auxp;
  85. char kbuf[sizeof(*frame) + 8];
  86. struct iwmmxt_sigframe *kframe;
  87. /* the iWMMXt context must be 64 bit aligned */
  88. kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
  89. if (__copy_from_user(kframe, frame, sizeof(*frame)))
  90. return -1;
  91. /*
  92. * For non-iWMMXt threads: a single iwmmxt_sigframe-sized dummy
  93. * block is discarded for compatibility with setup_sigframe() if
  94. * present, but we don't mandate its presence. If some other
  95. * magic is here, it's not for us:
  96. */
  97. if (!test_thread_flag(TIF_USING_IWMMXT) &&
  98. kframe->magic != DUMMY_MAGIC)
  99. return 0;
  100. if (kframe->size != IWMMXT_STORAGE_SIZE)
  101. return -1;
  102. if (test_thread_flag(TIF_USING_IWMMXT)) {
  103. if (kframe->magic != IWMMXT_MAGIC)
  104. return -1;
  105. iwmmxt_task_restore(current_thread_info(), &kframe->storage);
  106. }
  107. *auxp += IWMMXT_STORAGE_SIZE;
  108. return 0;
  109. }
  110. #endif
  111. #ifdef CONFIG_VFP
  112. static int preserve_vfp_context(struct vfp_sigframe __user *frame)
  113. {
  114. const unsigned long magic = VFP_MAGIC;
  115. const unsigned long size = VFP_STORAGE_SIZE;
  116. int err = 0;
  117. __put_user_error(magic, &frame->magic, err);
  118. __put_user_error(size, &frame->size, err);
  119. if (err)
  120. return -EFAULT;
  121. return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
  122. }
  123. static int restore_vfp_context(char __user **auxp)
  124. {
  125. struct vfp_sigframe __user *frame =
  126. (struct vfp_sigframe __user *)*auxp;
  127. unsigned long magic;
  128. unsigned long size;
  129. int err = 0;
  130. __get_user_error(magic, &frame->magic, err);
  131. __get_user_error(size, &frame->size, err);
  132. if (err)
  133. return -EFAULT;
  134. if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
  135. return -EINVAL;
  136. *auxp += size;
  137. return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
  138. }
  139. #endif
  140. /*
  141. * Do a signal return; undo the signal stack. These are aligned to 64-bit.
  142. */
  143. static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
  144. {
  145. char __user *aux;
  146. sigset_t set;
  147. int err;
  148. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  149. if (err == 0)
  150. set_current_blocked(&set);
  151. __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
  152. __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
  153. __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
  154. __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
  155. __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
  156. __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
  157. __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
  158. __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
  159. __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
  160. __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
  161. __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
  162. __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
  163. __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
  164. __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
  165. __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
  166. __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
  167. __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
  168. err |= !valid_user_regs(regs);
  169. aux = (char __user *) sf->uc.uc_regspace;
  170. #ifdef CONFIG_CRUNCH
  171. if (err == 0)
  172. err |= restore_crunch_context(&aux);
  173. #endif
  174. #ifdef CONFIG_IWMMXT
  175. if (err == 0)
  176. err |= restore_iwmmxt_context(&aux);
  177. #endif
  178. #ifdef CONFIG_VFP
  179. if (err == 0)
  180. err |= restore_vfp_context(&aux);
  181. #endif
  182. return err;
  183. }
  184. asmlinkage int sys_sigreturn(struct pt_regs *regs)
  185. {
  186. struct sigframe __user *frame;
  187. /* Always make any pending restarted system calls return -EINTR */
  188. current->restart_block.fn = do_no_restart_syscall;
  189. /*
  190. * Since we stacked the signal on a 64-bit boundary,
  191. * then 'sp' should be word aligned here. If it's
  192. * not, then the user is trying to mess with us.
  193. */
  194. if (regs->ARM_sp & 7)
  195. goto badframe;
  196. frame = (struct sigframe __user *)regs->ARM_sp;
  197. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  198. goto badframe;
  199. if (restore_sigframe(regs, frame))
  200. goto badframe;
  201. return regs->ARM_r0;
  202. badframe:
  203. force_sig(SIGSEGV, current);
  204. return 0;
  205. }
  206. asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
  207. {
  208. struct rt_sigframe __user *frame;
  209. /* Always make any pending restarted system calls return -EINTR */
  210. current->restart_block.fn = do_no_restart_syscall;
  211. /*
  212. * Since we stacked the signal on a 64-bit boundary,
  213. * then 'sp' should be word aligned here. If it's
  214. * not, then the user is trying to mess with us.
  215. */
  216. if (regs->ARM_sp & 7)
  217. goto badframe;
  218. frame = (struct rt_sigframe __user *)regs->ARM_sp;
  219. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  220. goto badframe;
  221. if (restore_sigframe(regs, &frame->sig))
  222. goto badframe;
  223. if (restore_altstack(&frame->sig.uc.uc_stack))
  224. goto badframe;
  225. return regs->ARM_r0;
  226. badframe:
  227. force_sig(SIGSEGV, current);
  228. return 0;
  229. }
  230. static int
  231. setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
  232. {
  233. struct aux_sigframe __user *aux;
  234. int err = 0;
  235. __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
  236. __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
  237. __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
  238. __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
  239. __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
  240. __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
  241. __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
  242. __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
  243. __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
  244. __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
  245. __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
  246. __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
  247. __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
  248. __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
  249. __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
  250. __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
  251. __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
  252. __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
  253. __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
  254. __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
  255. __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
  256. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
  257. aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
  258. #ifdef CONFIG_CRUNCH
  259. if (err == 0)
  260. err |= preserve_crunch_context(&aux->crunch);
  261. #endif
  262. #ifdef CONFIG_IWMMXT
  263. if (err == 0)
  264. err |= preserve_iwmmxt_context(&aux->iwmmxt);
  265. #endif
  266. #ifdef CONFIG_VFP
  267. if (err == 0)
  268. err |= preserve_vfp_context(&aux->vfp);
  269. #endif
  270. __put_user_error(0, &aux->end_magic, err);
  271. return err;
  272. }
  273. static inline void __user *
  274. get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
  275. {
  276. unsigned long sp = sigsp(regs->ARM_sp, ksig);
  277. void __user *frame;
  278. /*
  279. * ATPCS B01 mandates 8-byte alignment
  280. */
  281. frame = (void __user *)((sp - framesize) & ~7);
  282. /*
  283. * Check that we can actually write to the signal frame.
  284. */
  285. if (!access_ok(VERIFY_WRITE, frame, framesize))
  286. frame = NULL;
  287. return frame;
  288. }
  289. static int
  290. setup_return(struct pt_regs *regs, struct ksignal *ksig,
  291. unsigned long __user *rc, void __user *frame)
  292. {
  293. unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
  294. unsigned long handler_fdpic_GOT = 0;
  295. unsigned long retcode;
  296. unsigned int idx, thumb = 0;
  297. unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
  298. bool fdpic = IS_ENABLED(CONFIG_BINFMT_ELF_FDPIC) &&
  299. (current->personality & FDPIC_FUNCPTRS);
  300. if (fdpic) {
  301. unsigned long __user *fdpic_func_desc =
  302. (unsigned long __user *)handler;
  303. if (__get_user(handler, &fdpic_func_desc[0]) ||
  304. __get_user(handler_fdpic_GOT, &fdpic_func_desc[1]))
  305. return 1;
  306. }
  307. cpsr |= PSR_ENDSTATE;
  308. /*
  309. * Maybe we need to deliver a 32-bit signal to a 26-bit task.
  310. */
  311. if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
  312. cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
  313. #ifdef CONFIG_ARM_THUMB
  314. if (elf_hwcap & HWCAP_THUMB) {
  315. /*
  316. * The LSB of the handler determines if we're going to
  317. * be using THUMB or ARM mode for this signal handler.
  318. */
  319. thumb = handler & 1;
  320. /*
  321. * Clear the If-Then Thumb-2 execution state. ARM spec
  322. * requires this to be all 000s in ARM mode. Snapdragon
  323. * S4/Krait misbehaves on a Thumb=>ARM signal transition
  324. * without this.
  325. *
  326. * We must do this whenever we are running on a Thumb-2
  327. * capable CPU, which includes ARMv6T2. However, we elect
  328. * to always do this to simplify the code; this field is
  329. * marked UNK/SBZP for older architectures.
  330. */
  331. cpsr &= ~PSR_IT_MASK;
  332. if (thumb) {
  333. cpsr |= PSR_T_BIT;
  334. } else
  335. cpsr &= ~PSR_T_BIT;
  336. }
  337. #endif
  338. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  339. retcode = (unsigned long)ksig->ka.sa.sa_restorer;
  340. if (fdpic) {
  341. /*
  342. * We need code to load the function descriptor.
  343. * That code follows the standard sigreturn code
  344. * (6 words), and is made of 3 + 2 words for each
  345. * variant. The 4th copied word is the actual FD
  346. * address that the assembly code expects.
  347. */
  348. idx = 6 + thumb * 3;
  349. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  350. idx += 5;
  351. if (__put_user(sigreturn_codes[idx], rc ) ||
  352. __put_user(sigreturn_codes[idx+1], rc+1) ||
  353. __put_user(sigreturn_codes[idx+2], rc+2) ||
  354. __put_user(retcode, rc+3))
  355. return 1;
  356. goto rc_finish;
  357. }
  358. } else {
  359. idx = thumb << 1;
  360. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  361. idx += 3;
  362. /*
  363. * Put the sigreturn code on the stack no matter which return
  364. * mechanism we use in order to remain ABI compliant
  365. */
  366. if (__put_user(sigreturn_codes[idx], rc) ||
  367. __put_user(sigreturn_codes[idx+1], rc+1))
  368. return 1;
  369. rc_finish:
  370. #ifdef CONFIG_MMU
  371. if (cpsr & MODE32_BIT) {
  372. struct mm_struct *mm = current->mm;
  373. /*
  374. * 32-bit code can use the signal return page
  375. * except when the MPU has protected the vectors
  376. * page from PL0
  377. */
  378. retcode = mm->context.sigpage + signal_return_offset +
  379. (idx << 2) + thumb;
  380. } else
  381. #endif
  382. {
  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 + 3));
  389. retcode = ((unsigned long)rc) + thumb;
  390. }
  391. }
  392. regs->ARM_r0 = ksig->sig;
  393. regs->ARM_sp = (unsigned long)frame;
  394. regs->ARM_lr = retcode;
  395. regs->ARM_pc = handler;
  396. if (fdpic)
  397. regs->ARM_r9 = handler_fdpic_GOT;
  398. regs->ARM_cpsr = cpsr;
  399. return 0;
  400. }
  401. static int
  402. setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  403. {
  404. struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
  405. int err = 0;
  406. if (!frame)
  407. return 1;
  408. /*
  409. * Set uc.uc_flags to a value which sc.trap_no would never have.
  410. */
  411. __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
  412. err |= setup_sigframe(frame, regs, set);
  413. if (err == 0)
  414. err = setup_return(regs, ksig, frame->retcode, frame);
  415. return err;
  416. }
  417. static int
  418. setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  419. {
  420. struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
  421. int err = 0;
  422. if (!frame)
  423. return 1;
  424. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  425. __put_user_error(0, &frame->sig.uc.uc_flags, err);
  426. __put_user_error(NULL, &frame->sig.uc.uc_link, err);
  427. err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
  428. err |= setup_sigframe(&frame->sig, regs, set);
  429. if (err == 0)
  430. err = setup_return(regs, ksig, frame->sig.retcode, frame);
  431. if (err == 0) {
  432. /*
  433. * For realtime signals we must also set the second and third
  434. * arguments for the signal handler.
  435. * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
  436. */
  437. regs->ARM_r1 = (unsigned long)&frame->info;
  438. regs->ARM_r2 = (unsigned long)&frame->sig.uc;
  439. }
  440. return err;
  441. }
  442. /*
  443. * OK, we're invoking a handler
  444. */
  445. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  446. {
  447. sigset_t *oldset = sigmask_to_save();
  448. int ret;
  449. /*
  450. * Increment event counter and perform fixup for the pre-signal
  451. * frame.
  452. */
  453. rseq_signal_deliver(ksig, regs);
  454. /*
  455. * Set up the stack frame
  456. */
  457. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  458. ret = setup_rt_frame(ksig, oldset, regs);
  459. else
  460. ret = setup_frame(ksig, oldset, regs);
  461. /*
  462. * Check that the resulting registers are actually sane.
  463. */
  464. ret |= !valid_user_regs(regs);
  465. signal_setup_done(ret, ksig, 0);
  466. }
  467. /*
  468. * Note that 'init' is a special process: it doesn't get signals it doesn't
  469. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  470. * mistake.
  471. *
  472. * Note that we go through the signals twice: once to check the signals that
  473. * the kernel can handle, and then we build all the user-level signal handling
  474. * stack-frames in one go after that.
  475. */
  476. static int do_signal(struct pt_regs *regs, int syscall)
  477. {
  478. unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
  479. struct ksignal ksig;
  480. int restart = 0;
  481. /*
  482. * If we were from a system call, check for system call restarting...
  483. */
  484. if (syscall) {
  485. continue_addr = regs->ARM_pc;
  486. restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
  487. retval = regs->ARM_r0;
  488. /*
  489. * Prepare for system call restart. We do this here so that a
  490. * debugger will see the already changed PSW.
  491. */
  492. switch (retval) {
  493. case -ERESTART_RESTARTBLOCK:
  494. restart -= 2;
  495. case -ERESTARTNOHAND:
  496. case -ERESTARTSYS:
  497. case -ERESTARTNOINTR:
  498. restart++;
  499. regs->ARM_r0 = regs->ARM_ORIG_r0;
  500. regs->ARM_pc = restart_addr;
  501. break;
  502. }
  503. }
  504. /*
  505. * Get the signal to deliver. When running under ptrace, at this
  506. * point the debugger may change all our registers ...
  507. */
  508. /*
  509. * Depending on the signal settings we may need to revert the
  510. * decision to restart the system call. But skip this if a
  511. * debugger has chosen to restart at a different PC.
  512. */
  513. if (get_signal(&ksig)) {
  514. /* handler */
  515. if (unlikely(restart) && regs->ARM_pc == restart_addr) {
  516. if (retval == -ERESTARTNOHAND ||
  517. retval == -ERESTART_RESTARTBLOCK
  518. || (retval == -ERESTARTSYS
  519. && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
  520. regs->ARM_r0 = -EINTR;
  521. regs->ARM_pc = continue_addr;
  522. }
  523. }
  524. handle_signal(&ksig, regs);
  525. } else {
  526. /* no handler */
  527. restore_saved_sigmask();
  528. if (unlikely(restart) && regs->ARM_pc == restart_addr) {
  529. regs->ARM_pc = continue_addr;
  530. return restart;
  531. }
  532. }
  533. return 0;
  534. }
  535. asmlinkage int
  536. do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  537. {
  538. /*
  539. * The assembly code enters us with IRQs off, but it hasn't
  540. * informed the tracing code of that for efficiency reasons.
  541. * Update the trace code with the current status.
  542. */
  543. trace_hardirqs_off();
  544. do {
  545. if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  546. schedule();
  547. } else {
  548. if (unlikely(!user_mode(regs)))
  549. return 0;
  550. local_irq_enable();
  551. if (thread_flags & _TIF_SIGPENDING) {
  552. int restart = do_signal(regs, syscall);
  553. if (unlikely(restart)) {
  554. /*
  555. * Restart without handlers.
  556. * Deal with it without leaving
  557. * the kernel space.
  558. */
  559. return restart;
  560. }
  561. syscall = 0;
  562. } else if (thread_flags & _TIF_UPROBE) {
  563. uprobe_notify_resume(regs);
  564. } else {
  565. clear_thread_flag(TIF_NOTIFY_RESUME);
  566. tracehook_notify_resume(regs);
  567. rseq_handle_notify_resume(NULL, regs);
  568. }
  569. }
  570. local_irq_disable();
  571. thread_flags = current_thread_info()->flags;
  572. } while (thread_flags & _TIF_WORK_MASK);
  573. return 0;
  574. }
  575. struct page *get_signal_page(void)
  576. {
  577. unsigned long ptr;
  578. unsigned offset;
  579. struct page *page;
  580. void *addr;
  581. page = alloc_pages(GFP_KERNEL, 0);
  582. if (!page)
  583. return NULL;
  584. addr = page_address(page);
  585. /* Give the signal return code some randomness */
  586. offset = 0x200 + (get_random_int() & 0x7fc);
  587. signal_return_offset = offset;
  588. /*
  589. * Copy signal return handlers into the vector page, and
  590. * set sigreturn to be a pointer to these.
  591. */
  592. memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
  593. ptr = (unsigned long)addr + offset;
  594. flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
  595. return page;
  596. }
  597. /* Defer to generic check */
  598. asmlinkage void addr_limit_check_failed(void)
  599. {
  600. addr_limit_user_check();
  601. }
  602. #ifdef CONFIG_DEBUG_RSEQ
  603. asmlinkage void do_rseq_syscall(struct pt_regs *regs)
  604. {
  605. rseq_syscall(regs);
  606. }
  607. #endif