signal.c 15 KB

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