signal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * linux/arch/unicore32/kernel/signal.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/signal.h>
  14. #include <linux/personality.h>
  15. #include <linux/freezer.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/tracehook.h>
  18. #include <linux/elf.h>
  19. #include <linux/unistd.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/ucontext.h>
  22. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  23. /*
  24. * For UniCore syscalls, we encode the syscall number into the instruction.
  25. */
  26. #define SWI_SYS_SIGRETURN (0xff000000) /* error number for new abi */
  27. #define SWI_SYS_RT_SIGRETURN (0xff000000 | (__NR_rt_sigreturn))
  28. #define SWI_SYS_RESTART (0xff000000 | (__NR_restart_syscall))
  29. #define KERN_SIGRETURN_CODE (KUSER_VECPAGE_BASE + 0x00000500)
  30. #define KERN_RESTART_CODE (KERN_SIGRETURN_CODE + sizeof(sigreturn_codes))
  31. const unsigned long sigreturn_codes[3] = {
  32. SWI_SYS_SIGRETURN, SWI_SYS_RT_SIGRETURN,
  33. };
  34. const unsigned long syscall_restart_code[2] = {
  35. SWI_SYS_RESTART, /* swi __NR_restart_syscall */
  36. 0x69efc004, /* ldr pc, [sp], #4 */
  37. };
  38. /*
  39. * Do a signal return; undo the signal stack. These are aligned to 64-bit.
  40. */
  41. struct sigframe {
  42. struct ucontext uc;
  43. unsigned long retcode[2];
  44. };
  45. struct rt_sigframe {
  46. struct siginfo info;
  47. struct sigframe sig;
  48. };
  49. static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
  50. {
  51. sigset_t set;
  52. int err;
  53. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  54. if (err == 0) {
  55. sigdelsetmask(&set, ~_BLOCKABLE);
  56. spin_lock_irq(&current->sighand->siglock);
  57. current->blocked = set;
  58. recalc_sigpending();
  59. spin_unlock_irq(&current->sighand->siglock);
  60. }
  61. err |= __get_user(regs->UCreg_00, &sf->uc.uc_mcontext.regs.UCreg_00);
  62. err |= __get_user(regs->UCreg_01, &sf->uc.uc_mcontext.regs.UCreg_01);
  63. err |= __get_user(regs->UCreg_02, &sf->uc.uc_mcontext.regs.UCreg_02);
  64. err |= __get_user(regs->UCreg_03, &sf->uc.uc_mcontext.regs.UCreg_03);
  65. err |= __get_user(regs->UCreg_04, &sf->uc.uc_mcontext.regs.UCreg_04);
  66. err |= __get_user(regs->UCreg_05, &sf->uc.uc_mcontext.regs.UCreg_05);
  67. err |= __get_user(regs->UCreg_06, &sf->uc.uc_mcontext.regs.UCreg_06);
  68. err |= __get_user(regs->UCreg_07, &sf->uc.uc_mcontext.regs.UCreg_07);
  69. err |= __get_user(regs->UCreg_08, &sf->uc.uc_mcontext.regs.UCreg_08);
  70. err |= __get_user(regs->UCreg_09, &sf->uc.uc_mcontext.regs.UCreg_09);
  71. err |= __get_user(regs->UCreg_10, &sf->uc.uc_mcontext.regs.UCreg_10);
  72. err |= __get_user(regs->UCreg_11, &sf->uc.uc_mcontext.regs.UCreg_11);
  73. err |= __get_user(regs->UCreg_12, &sf->uc.uc_mcontext.regs.UCreg_12);
  74. err |= __get_user(regs->UCreg_13, &sf->uc.uc_mcontext.regs.UCreg_13);
  75. err |= __get_user(regs->UCreg_14, &sf->uc.uc_mcontext.regs.UCreg_14);
  76. err |= __get_user(regs->UCreg_15, &sf->uc.uc_mcontext.regs.UCreg_15);
  77. err |= __get_user(regs->UCreg_16, &sf->uc.uc_mcontext.regs.UCreg_16);
  78. err |= __get_user(regs->UCreg_17, &sf->uc.uc_mcontext.regs.UCreg_17);
  79. err |= __get_user(regs->UCreg_18, &sf->uc.uc_mcontext.regs.UCreg_18);
  80. err |= __get_user(regs->UCreg_19, &sf->uc.uc_mcontext.regs.UCreg_19);
  81. err |= __get_user(regs->UCreg_20, &sf->uc.uc_mcontext.regs.UCreg_20);
  82. err |= __get_user(regs->UCreg_21, &sf->uc.uc_mcontext.regs.UCreg_21);
  83. err |= __get_user(regs->UCreg_22, &sf->uc.uc_mcontext.regs.UCreg_22);
  84. err |= __get_user(regs->UCreg_23, &sf->uc.uc_mcontext.regs.UCreg_23);
  85. err |= __get_user(regs->UCreg_24, &sf->uc.uc_mcontext.regs.UCreg_24);
  86. err |= __get_user(regs->UCreg_25, &sf->uc.uc_mcontext.regs.UCreg_25);
  87. err |= __get_user(regs->UCreg_26, &sf->uc.uc_mcontext.regs.UCreg_26);
  88. err |= __get_user(regs->UCreg_fp, &sf->uc.uc_mcontext.regs.UCreg_fp);
  89. err |= __get_user(regs->UCreg_ip, &sf->uc.uc_mcontext.regs.UCreg_ip);
  90. err |= __get_user(regs->UCreg_sp, &sf->uc.uc_mcontext.regs.UCreg_sp);
  91. err |= __get_user(regs->UCreg_lr, &sf->uc.uc_mcontext.regs.UCreg_lr);
  92. err |= __get_user(regs->UCreg_pc, &sf->uc.uc_mcontext.regs.UCreg_pc);
  93. err |= __get_user(regs->UCreg_asr, &sf->uc.uc_mcontext.regs.UCreg_asr);
  94. err |= !valid_user_regs(regs);
  95. return err;
  96. }
  97. asmlinkage int __sys_rt_sigreturn(struct pt_regs *regs)
  98. {
  99. struct rt_sigframe __user *frame;
  100. /* Always make any pending restarted system calls return -EINTR */
  101. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  102. /*
  103. * Since we stacked the signal on a 64-bit boundary,
  104. * then 'sp' should be word aligned here. If it's
  105. * not, then the user is trying to mess with us.
  106. */
  107. if (regs->UCreg_sp & 7)
  108. goto badframe;
  109. frame = (struct rt_sigframe __user *)regs->UCreg_sp;
  110. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  111. goto badframe;
  112. if (restore_sigframe(regs, &frame->sig))
  113. goto badframe;
  114. if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->UCreg_sp)
  115. == -EFAULT)
  116. goto badframe;
  117. return regs->UCreg_00;
  118. badframe:
  119. force_sig(SIGSEGV, current);
  120. return 0;
  121. }
  122. static int setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs,
  123. sigset_t *set)
  124. {
  125. int err = 0;
  126. err |= __put_user(regs->UCreg_00, &sf->uc.uc_mcontext.regs.UCreg_00);
  127. err |= __put_user(regs->UCreg_01, &sf->uc.uc_mcontext.regs.UCreg_01);
  128. err |= __put_user(regs->UCreg_02, &sf->uc.uc_mcontext.regs.UCreg_02);
  129. err |= __put_user(regs->UCreg_03, &sf->uc.uc_mcontext.regs.UCreg_03);
  130. err |= __put_user(regs->UCreg_04, &sf->uc.uc_mcontext.regs.UCreg_04);
  131. err |= __put_user(regs->UCreg_05, &sf->uc.uc_mcontext.regs.UCreg_05);
  132. err |= __put_user(regs->UCreg_06, &sf->uc.uc_mcontext.regs.UCreg_06);
  133. err |= __put_user(regs->UCreg_07, &sf->uc.uc_mcontext.regs.UCreg_07);
  134. err |= __put_user(regs->UCreg_08, &sf->uc.uc_mcontext.regs.UCreg_08);
  135. err |= __put_user(regs->UCreg_09, &sf->uc.uc_mcontext.regs.UCreg_09);
  136. err |= __put_user(regs->UCreg_10, &sf->uc.uc_mcontext.regs.UCreg_10);
  137. err |= __put_user(regs->UCreg_11, &sf->uc.uc_mcontext.regs.UCreg_11);
  138. err |= __put_user(regs->UCreg_12, &sf->uc.uc_mcontext.regs.UCreg_12);
  139. err |= __put_user(regs->UCreg_13, &sf->uc.uc_mcontext.regs.UCreg_13);
  140. err |= __put_user(regs->UCreg_14, &sf->uc.uc_mcontext.regs.UCreg_14);
  141. err |= __put_user(regs->UCreg_15, &sf->uc.uc_mcontext.regs.UCreg_15);
  142. err |= __put_user(regs->UCreg_16, &sf->uc.uc_mcontext.regs.UCreg_16);
  143. err |= __put_user(regs->UCreg_17, &sf->uc.uc_mcontext.regs.UCreg_17);
  144. err |= __put_user(regs->UCreg_18, &sf->uc.uc_mcontext.regs.UCreg_18);
  145. err |= __put_user(regs->UCreg_19, &sf->uc.uc_mcontext.regs.UCreg_19);
  146. err |= __put_user(regs->UCreg_20, &sf->uc.uc_mcontext.regs.UCreg_20);
  147. err |= __put_user(regs->UCreg_21, &sf->uc.uc_mcontext.regs.UCreg_21);
  148. err |= __put_user(regs->UCreg_22, &sf->uc.uc_mcontext.regs.UCreg_22);
  149. err |= __put_user(regs->UCreg_23, &sf->uc.uc_mcontext.regs.UCreg_23);
  150. err |= __put_user(regs->UCreg_24, &sf->uc.uc_mcontext.regs.UCreg_24);
  151. err |= __put_user(regs->UCreg_25, &sf->uc.uc_mcontext.regs.UCreg_25);
  152. err |= __put_user(regs->UCreg_26, &sf->uc.uc_mcontext.regs.UCreg_26);
  153. err |= __put_user(regs->UCreg_fp, &sf->uc.uc_mcontext.regs.UCreg_fp);
  154. err |= __put_user(regs->UCreg_ip, &sf->uc.uc_mcontext.regs.UCreg_ip);
  155. err |= __put_user(regs->UCreg_sp, &sf->uc.uc_mcontext.regs.UCreg_sp);
  156. err |= __put_user(regs->UCreg_lr, &sf->uc.uc_mcontext.regs.UCreg_lr);
  157. err |= __put_user(regs->UCreg_pc, &sf->uc.uc_mcontext.regs.UCreg_pc);
  158. err |= __put_user(regs->UCreg_asr, &sf->uc.uc_mcontext.regs.UCreg_asr);
  159. err |= __put_user(current->thread.trap_no,
  160. &sf->uc.uc_mcontext.trap_no);
  161. err |= __put_user(current->thread.error_code,
  162. &sf->uc.uc_mcontext.error_code);
  163. err |= __put_user(current->thread.address,
  164. &sf->uc.uc_mcontext.fault_address);
  165. err |= __put_user(set->sig[0], &sf->uc.uc_mcontext.oldmask);
  166. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
  167. return err;
  168. }
  169. static inline void __user *get_sigframe(struct k_sigaction *ka,
  170. struct pt_regs *regs, int framesize)
  171. {
  172. unsigned long sp = regs->UCreg_sp;
  173. void __user *frame;
  174. /*
  175. * This is the X/Open sanctioned signal stack switching.
  176. */
  177. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
  178. sp = current->sas_ss_sp + current->sas_ss_size;
  179. /*
  180. * ATPCS B01 mandates 8-byte alignment
  181. */
  182. frame = (void __user *)((sp - framesize) & ~7);
  183. /*
  184. * Check that we can actually write to the signal frame.
  185. */
  186. if (!access_ok(VERIFY_WRITE, frame, framesize))
  187. frame = NULL;
  188. return frame;
  189. }
  190. static int setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  191. unsigned long __user *rc, void __user *frame, int usig)
  192. {
  193. unsigned long handler = (unsigned long)ka->sa.sa_handler;
  194. unsigned long retcode;
  195. unsigned long asr = regs->UCreg_asr & ~PSR_f;
  196. unsigned int idx = 0;
  197. if (ka->sa.sa_flags & SA_SIGINFO)
  198. idx += 1;
  199. if (__put_user(sigreturn_codes[idx], rc) ||
  200. __put_user(sigreturn_codes[idx+1], rc+1))
  201. return 1;
  202. retcode = KERN_SIGRETURN_CODE + (idx << 2);
  203. regs->UCreg_00 = usig;
  204. regs->UCreg_sp = (unsigned long)frame;
  205. regs->UCreg_lr = retcode;
  206. regs->UCreg_pc = handler;
  207. regs->UCreg_asr = asr;
  208. return 0;
  209. }
  210. static int setup_frame(int usig, struct k_sigaction *ka,
  211. sigset_t *set, struct pt_regs *regs)
  212. {
  213. struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
  214. int err = 0;
  215. if (!frame)
  216. return 1;
  217. /*
  218. * Set uc.uc_flags to a value which sc.trap_no would never have.
  219. */
  220. err |= __put_user(0x5ac3c35a, &frame->uc.uc_flags);
  221. err |= setup_sigframe(frame, regs, set);
  222. if (err == 0)
  223. err |= setup_return(regs, ka, frame->retcode, frame, usig);
  224. return err;
  225. }
  226. static int setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
  227. sigset_t *set, struct pt_regs *regs)
  228. {
  229. struct rt_sigframe __user *frame =
  230. get_sigframe(ka, regs, sizeof(*frame));
  231. stack_t stack;
  232. int err = 0;
  233. if (!frame)
  234. return 1;
  235. err |= copy_siginfo_to_user(&frame->info, info);
  236. err |= __put_user(0, &frame->sig.uc.uc_flags);
  237. err |= __put_user(NULL, &frame->sig.uc.uc_link);
  238. memset(&stack, 0, sizeof(stack));
  239. stack.ss_sp = (void __user *)current->sas_ss_sp;
  240. stack.ss_flags = sas_ss_flags(regs->UCreg_sp);
  241. stack.ss_size = current->sas_ss_size;
  242. err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
  243. err |= setup_sigframe(&frame->sig, regs, set);
  244. if (err == 0)
  245. err |= setup_return(regs, ka, frame->sig.retcode, frame, usig);
  246. if (err == 0) {
  247. /*
  248. * For realtime signals we must also set the second and third
  249. * arguments for the signal handler.
  250. */
  251. regs->UCreg_01 = (unsigned long)&frame->info;
  252. regs->UCreg_02 = (unsigned long)&frame->sig.uc;
  253. }
  254. return err;
  255. }
  256. static inline void setup_syscall_restart(struct pt_regs *regs)
  257. {
  258. regs->UCreg_00 = regs->UCreg_ORIG_00;
  259. regs->UCreg_pc -= 4;
  260. }
  261. /*
  262. * OK, we're invoking a handler
  263. */
  264. static int handle_signal(unsigned long sig, struct k_sigaction *ka,
  265. siginfo_t *info, sigset_t *oldset,
  266. struct pt_regs *regs, int syscall)
  267. {
  268. struct thread_info *thread = current_thread_info();
  269. struct task_struct *tsk = current;
  270. int usig = sig;
  271. int ret;
  272. /*
  273. * If we were from a system call, check for system call restarting...
  274. */
  275. if (syscall) {
  276. switch (regs->UCreg_00) {
  277. case -ERESTART_RESTARTBLOCK:
  278. case -ERESTARTNOHAND:
  279. regs->UCreg_00 = -EINTR;
  280. break;
  281. case -ERESTARTSYS:
  282. if (!(ka->sa.sa_flags & SA_RESTART)) {
  283. regs->UCreg_00 = -EINTR;
  284. break;
  285. }
  286. /* fallthrough */
  287. case -ERESTARTNOINTR:
  288. setup_syscall_restart(regs);
  289. }
  290. }
  291. /*
  292. * translate the signal
  293. */
  294. if (usig < 32 && thread->exec_domain
  295. && thread->exec_domain->signal_invmap)
  296. usig = thread->exec_domain->signal_invmap[usig];
  297. /*
  298. * Set up the stack frame
  299. */
  300. if (ka->sa.sa_flags & SA_SIGINFO)
  301. ret = setup_rt_frame(usig, ka, info, oldset, regs);
  302. else
  303. ret = setup_frame(usig, ka, oldset, regs);
  304. /*
  305. * Check that the resulting registers are actually sane.
  306. */
  307. ret |= !valid_user_regs(regs);
  308. if (ret != 0) {
  309. force_sigsegv(sig, tsk);
  310. return ret;
  311. }
  312. /*
  313. * Block the signal if we were successful.
  314. */
  315. spin_lock_irq(&tsk->sighand->siglock);
  316. sigorsets(&tsk->blocked, &tsk->blocked,
  317. &ka->sa.sa_mask);
  318. if (!(ka->sa.sa_flags & SA_NODEFER))
  319. sigaddset(&tsk->blocked, sig);
  320. recalc_sigpending();
  321. spin_unlock_irq(&tsk->sighand->siglock);
  322. return 0;
  323. }
  324. /*
  325. * Note that 'init' is a special process: it doesn't get signals it doesn't
  326. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  327. * mistake.
  328. *
  329. * Note that we go through the signals twice: once to check the signals that
  330. * the kernel can handle, and then we build all the user-level signal handling
  331. * stack-frames in one go after that.
  332. */
  333. static void do_signal(struct pt_regs *regs, int syscall)
  334. {
  335. struct k_sigaction ka;
  336. siginfo_t info;
  337. int signr;
  338. /*
  339. * We want the common case to go fast, which
  340. * is why we may in certain cases get here from
  341. * kernel mode. Just return without doing anything
  342. * if so.
  343. */
  344. if (!user_mode(regs))
  345. return;
  346. if (try_to_freeze())
  347. goto no_signal;
  348. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  349. if (signr > 0) {
  350. sigset_t *oldset;
  351. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  352. oldset = &current->saved_sigmask;
  353. else
  354. oldset = &current->blocked;
  355. if (handle_signal(signr, &ka, &info, oldset, regs, syscall)
  356. == 0) {
  357. /*
  358. * A signal was successfully delivered; the saved
  359. * sigmask will have been stored in the signal frame,
  360. * and will be restored by sigreturn, so we can simply
  361. * clear the TIF_RESTORE_SIGMASK flag.
  362. */
  363. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  364. clear_thread_flag(TIF_RESTORE_SIGMASK);
  365. }
  366. return;
  367. }
  368. no_signal:
  369. /*
  370. * No signal to deliver to the process - restart the syscall.
  371. */
  372. if (syscall) {
  373. if (regs->UCreg_00 == -ERESTART_RESTARTBLOCK) {
  374. u32 __user *usp;
  375. regs->UCreg_sp -= 4;
  376. usp = (u32 __user *)regs->UCreg_sp;
  377. if (put_user(regs->UCreg_pc, usp) == 0) {
  378. regs->UCreg_pc = KERN_RESTART_CODE;
  379. } else {
  380. regs->UCreg_sp += 4;
  381. force_sigsegv(0, current);
  382. }
  383. }
  384. if (regs->UCreg_00 == -ERESTARTNOHAND ||
  385. regs->UCreg_00 == -ERESTARTSYS ||
  386. regs->UCreg_00 == -ERESTARTNOINTR) {
  387. setup_syscall_restart(regs);
  388. }
  389. /* If there's no signal to deliver, we just put the saved
  390. * sigmask back.
  391. */
  392. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  393. clear_thread_flag(TIF_RESTORE_SIGMASK);
  394. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  395. }
  396. }
  397. }
  398. asmlinkage void do_notify_resume(struct pt_regs *regs,
  399. unsigned int thread_flags, int syscall)
  400. {
  401. if (thread_flags & _TIF_SIGPENDING)
  402. do_signal(regs, syscall);
  403. if (thread_flags & _TIF_NOTIFY_RESUME) {
  404. clear_thread_flag(TIF_NOTIFY_RESUME);
  405. tracehook_notify_resume(regs);
  406. if (current->replacement_session_keyring)
  407. key_replace_session_keyring();
  408. }
  409. }
  410. /*
  411. * Copy signal return handlers into the vector page, and
  412. * set sigreturn to be a pointer to these.
  413. */
  414. void __init early_signal_init(void)
  415. {
  416. memcpy((void *)kuser_vecpage_to_vectors(KERN_SIGRETURN_CODE),
  417. sigreturn_codes, sizeof(sigreturn_codes));
  418. memcpy((void *)kuser_vecpage_to_vectors(KERN_RESTART_CODE),
  419. syscall_restart_code, sizeof(syscall_restart_code));
  420. /* Need not to flush icache, since early_trap_init will do it last. */
  421. }