signal.c 13 KB

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