signal_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * arch/sh/kernel/signal_64.c
  3. *
  4. * Copyright (C) 2000, 2001 Paolo Alberelli
  5. * Copyright (C) 2003 - 2008 Paul Mundt
  6. * Copyright (C) 2004 Richard Curnow
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/rwsem.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/kernel.h>
  17. #include <linux/signal.h>
  18. #include <linux/errno.h>
  19. #include <linux/wait.h>
  20. #include <linux/personality.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/unistd.h>
  23. #include <linux/stddef.h>
  24. #include <linux/tracehook.h>
  25. #include <asm/ucontext.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/fpu.h>
  30. #define REG_RET 9
  31. #define REG_ARG1 2
  32. #define REG_ARG2 3
  33. #define REG_ARG3 4
  34. #define REG_SP 15
  35. #define REG_PR 18
  36. #define REF_REG_RET regs->regs[REG_RET]
  37. #define REF_REG_SP regs->regs[REG_SP]
  38. #define DEREF_REG_PR regs->regs[REG_PR]
  39. #define DEBUG_SIG 0
  40. static void
  41. handle_signal(struct ksignal *ksig, struct pt_regs *regs);
  42. static inline void
  43. handle_syscall_restart(struct pt_regs *regs, struct sigaction *sa)
  44. {
  45. /* If we're not from a syscall, bail out */
  46. if (regs->syscall_nr < 0)
  47. return;
  48. /* check for system call restart.. */
  49. switch (regs->regs[REG_RET]) {
  50. case -ERESTART_RESTARTBLOCK:
  51. case -ERESTARTNOHAND:
  52. no_system_call_restart:
  53. regs->regs[REG_RET] = -EINTR;
  54. break;
  55. case -ERESTARTSYS:
  56. if (!(sa->sa_flags & SA_RESTART))
  57. goto no_system_call_restart;
  58. /* fallthrough */
  59. case -ERESTARTNOINTR:
  60. /* Decode syscall # */
  61. regs->regs[REG_RET] = regs->syscall_nr;
  62. regs->pc -= 4;
  63. break;
  64. }
  65. }
  66. /*
  67. * Note that 'init' is a special process: it doesn't get signals it doesn't
  68. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  69. * mistake.
  70. *
  71. * Note that we go through the signals twice: once to check the signals that
  72. * the kernel can handle, and then we build all the user-level signal handling
  73. * stack-frames in one go after that.
  74. */
  75. static void do_signal(struct pt_regs *regs)
  76. {
  77. struct ksignal ksig;
  78. /*
  79. * We want the common case to go fast, which
  80. * is why we may in certain cases get here from
  81. * kernel mode. Just return without doing anything
  82. * if so.
  83. */
  84. if (!user_mode(regs))
  85. return;
  86. if (get_signal(&ksig)) {
  87. handle_syscall_restart(regs, &ksig.ka.sa);
  88. /* Whee! Actually deliver the signal. */
  89. handle_signal(&ksig, regs);
  90. return;
  91. }
  92. /* Did we come from a system call? */
  93. if (regs->syscall_nr >= 0) {
  94. /* Restart the system call - no handlers present */
  95. switch (regs->regs[REG_RET]) {
  96. case -ERESTARTNOHAND:
  97. case -ERESTARTSYS:
  98. case -ERESTARTNOINTR:
  99. /* Decode Syscall # */
  100. regs->regs[REG_RET] = regs->syscall_nr;
  101. regs->pc -= 4;
  102. break;
  103. case -ERESTART_RESTARTBLOCK:
  104. regs->regs[REG_RET] = __NR_restart_syscall;
  105. regs->pc -= 4;
  106. break;
  107. }
  108. }
  109. /* No signal to deliver -- put the saved sigmask back */
  110. restore_saved_sigmask();
  111. }
  112. /*
  113. * Do a signal return; undo the signal stack.
  114. */
  115. struct sigframe {
  116. struct sigcontext sc;
  117. unsigned long extramask[_NSIG_WORDS-1];
  118. long long retcode[2];
  119. };
  120. struct rt_sigframe {
  121. struct siginfo __user *pinfo;
  122. void *puc;
  123. struct siginfo info;
  124. struct ucontext uc;
  125. long long retcode[2];
  126. };
  127. #ifdef CONFIG_SH_FPU
  128. static inline int
  129. restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
  130. {
  131. int err = 0;
  132. int fpvalid;
  133. err |= __get_user (fpvalid, &sc->sc_fpvalid);
  134. conditional_used_math(fpvalid);
  135. if (! fpvalid)
  136. return err;
  137. if (current == last_task_used_math) {
  138. last_task_used_math = NULL;
  139. regs->sr |= SR_FD;
  140. }
  141. err |= __copy_from_user(&current->thread.xstate->hardfpu, &sc->sc_fpregs[0],
  142. (sizeof(long long) * 32) + (sizeof(int) * 1));
  143. return err;
  144. }
  145. static inline int
  146. setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
  147. {
  148. int err = 0;
  149. int fpvalid;
  150. fpvalid = !!used_math();
  151. err |= __put_user(fpvalid, &sc->sc_fpvalid);
  152. if (! fpvalid)
  153. return err;
  154. if (current == last_task_used_math) {
  155. enable_fpu();
  156. save_fpu(current);
  157. disable_fpu();
  158. last_task_used_math = NULL;
  159. regs->sr |= SR_FD;
  160. }
  161. err |= __copy_to_user(&sc->sc_fpregs[0], &current->thread.xstate->hardfpu,
  162. (sizeof(long long) * 32) + (sizeof(int) * 1));
  163. clear_used_math();
  164. return err;
  165. }
  166. #else
  167. static inline int
  168. restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
  169. {
  170. return 0;
  171. }
  172. static inline int
  173. setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
  174. {
  175. return 0;
  176. }
  177. #endif
  178. static int
  179. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, long long *r2_p)
  180. {
  181. unsigned int err = 0;
  182. unsigned long long current_sr, new_sr;
  183. #define SR_MASK 0xffff8cfd
  184. #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
  185. COPY(regs[0]); COPY(regs[1]); COPY(regs[2]); COPY(regs[3]);
  186. COPY(regs[4]); COPY(regs[5]); COPY(regs[6]); COPY(regs[7]);
  187. COPY(regs[8]); COPY(regs[9]); COPY(regs[10]); COPY(regs[11]);
  188. COPY(regs[12]); COPY(regs[13]); COPY(regs[14]); COPY(regs[15]);
  189. COPY(regs[16]); COPY(regs[17]); COPY(regs[18]); COPY(regs[19]);
  190. COPY(regs[20]); COPY(regs[21]); COPY(regs[22]); COPY(regs[23]);
  191. COPY(regs[24]); COPY(regs[25]); COPY(regs[26]); COPY(regs[27]);
  192. COPY(regs[28]); COPY(regs[29]); COPY(regs[30]); COPY(regs[31]);
  193. COPY(regs[32]); COPY(regs[33]); COPY(regs[34]); COPY(regs[35]);
  194. COPY(regs[36]); COPY(regs[37]); COPY(regs[38]); COPY(regs[39]);
  195. COPY(regs[40]); COPY(regs[41]); COPY(regs[42]); COPY(regs[43]);
  196. COPY(regs[44]); COPY(regs[45]); COPY(regs[46]); COPY(regs[47]);
  197. COPY(regs[48]); COPY(regs[49]); COPY(regs[50]); COPY(regs[51]);
  198. COPY(regs[52]); COPY(regs[53]); COPY(regs[54]); COPY(regs[55]);
  199. COPY(regs[56]); COPY(regs[57]); COPY(regs[58]); COPY(regs[59]);
  200. COPY(regs[60]); COPY(regs[61]); COPY(regs[62]);
  201. COPY(tregs[0]); COPY(tregs[1]); COPY(tregs[2]); COPY(tregs[3]);
  202. COPY(tregs[4]); COPY(tregs[5]); COPY(tregs[6]); COPY(tregs[7]);
  203. /* Prevent the signal handler manipulating SR in a way that can
  204. crash the kernel. i.e. only allow S, Q, M, PR, SZ, FR to be
  205. modified */
  206. current_sr = regs->sr;
  207. err |= __get_user(new_sr, &sc->sc_sr);
  208. regs->sr &= SR_MASK;
  209. regs->sr |= (new_sr & ~SR_MASK);
  210. COPY(pc);
  211. #undef COPY
  212. /* Must do this last in case it sets regs->sr.fd (i.e. after rest of sr
  213. * has been restored above.) */
  214. err |= restore_sigcontext_fpu(regs, sc);
  215. regs->syscall_nr = -1; /* disable syscall checks */
  216. err |= __get_user(*r2_p, &sc->sc_regs[REG_RET]);
  217. return err;
  218. }
  219. asmlinkage int sys_sigreturn(unsigned long r2, unsigned long r3,
  220. unsigned long r4, unsigned long r5,
  221. unsigned long r6, unsigned long r7,
  222. struct pt_regs * regs)
  223. {
  224. struct sigframe __user *frame = (struct sigframe __user *) (long) REF_REG_SP;
  225. sigset_t set;
  226. long long ret;
  227. /* Always make any pending restarted system calls return -EINTR */
  228. current->restart_block.fn = do_no_restart_syscall;
  229. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  230. goto badframe;
  231. if (__get_user(set.sig[0], &frame->sc.oldmask)
  232. || (_NSIG_WORDS > 1
  233. && __copy_from_user(&set.sig[1], &frame->extramask,
  234. sizeof(frame->extramask))))
  235. goto badframe;
  236. set_current_blocked(&set);
  237. if (restore_sigcontext(regs, &frame->sc, &ret))
  238. goto badframe;
  239. regs->pc -= 4;
  240. return (int) ret;
  241. badframe:
  242. force_sig(SIGSEGV, current);
  243. return 0;
  244. }
  245. asmlinkage int sys_rt_sigreturn(unsigned long r2, unsigned long r3,
  246. unsigned long r4, unsigned long r5,
  247. unsigned long r6, unsigned long r7,
  248. struct pt_regs * regs)
  249. {
  250. struct rt_sigframe __user *frame = (struct rt_sigframe __user *) (long) REF_REG_SP;
  251. sigset_t set;
  252. long long ret;
  253. /* Always make any pending restarted system calls return -EINTR */
  254. current->restart_block.fn = do_no_restart_syscall;
  255. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  256. goto badframe;
  257. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  258. goto badframe;
  259. set_current_blocked(&set);
  260. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ret))
  261. goto badframe;
  262. regs->pc -= 4;
  263. if (restore_altstack(&frame->uc.uc_stack))
  264. goto badframe;
  265. return (int) ret;
  266. badframe:
  267. force_sig(SIGSEGV, current);
  268. return 0;
  269. }
  270. /*
  271. * Set up a signal frame.
  272. */
  273. static int
  274. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  275. unsigned long mask)
  276. {
  277. int err = 0;
  278. /* Do this first, otherwise is this sets sr->fd, that value isn't preserved. */
  279. err |= setup_sigcontext_fpu(regs, sc);
  280. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  281. COPY(regs[0]); COPY(regs[1]); COPY(regs[2]); COPY(regs[3]);
  282. COPY(regs[4]); COPY(regs[5]); COPY(regs[6]); COPY(regs[7]);
  283. COPY(regs[8]); COPY(regs[9]); COPY(regs[10]); COPY(regs[11]);
  284. COPY(regs[12]); COPY(regs[13]); COPY(regs[14]); COPY(regs[15]);
  285. COPY(regs[16]); COPY(regs[17]); COPY(regs[18]); COPY(regs[19]);
  286. COPY(regs[20]); COPY(regs[21]); COPY(regs[22]); COPY(regs[23]);
  287. COPY(regs[24]); COPY(regs[25]); COPY(regs[26]); COPY(regs[27]);
  288. COPY(regs[28]); COPY(regs[29]); COPY(regs[30]); COPY(regs[31]);
  289. COPY(regs[32]); COPY(regs[33]); COPY(regs[34]); COPY(regs[35]);
  290. COPY(regs[36]); COPY(regs[37]); COPY(regs[38]); COPY(regs[39]);
  291. COPY(regs[40]); COPY(regs[41]); COPY(regs[42]); COPY(regs[43]);
  292. COPY(regs[44]); COPY(regs[45]); COPY(regs[46]); COPY(regs[47]);
  293. COPY(regs[48]); COPY(regs[49]); COPY(regs[50]); COPY(regs[51]);
  294. COPY(regs[52]); COPY(regs[53]); COPY(regs[54]); COPY(regs[55]);
  295. COPY(regs[56]); COPY(regs[57]); COPY(regs[58]); COPY(regs[59]);
  296. COPY(regs[60]); COPY(regs[61]); COPY(regs[62]);
  297. COPY(tregs[0]); COPY(tregs[1]); COPY(tregs[2]); COPY(tregs[3]);
  298. COPY(tregs[4]); COPY(tregs[5]); COPY(tregs[6]); COPY(tregs[7]);
  299. COPY(sr); COPY(pc);
  300. #undef COPY
  301. err |= __put_user(mask, &sc->oldmask);
  302. return err;
  303. }
  304. /*
  305. * Determine which stack to use..
  306. */
  307. static inline void __user *
  308. get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
  309. {
  310. if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! sas_ss_flags(sp))
  311. sp = current->sas_ss_sp + current->sas_ss_size;
  312. return (void __user *)((sp - frame_size) & -8ul);
  313. }
  314. void sa_default_restorer(void); /* See comments below */
  315. void sa_default_rt_restorer(void); /* See comments below */
  316. static int setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  317. {
  318. struct sigframe __user *frame;
  319. int err = 0, sig = ksig->sig;
  320. int signal;
  321. frame = get_sigframe(&ksig->ka, regs->regs[REG_SP], sizeof(*frame));
  322. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  323. return -EFAULT;
  324. err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
  325. /* Give up earlier as i386, in case */
  326. if (err)
  327. return -EFAULT;
  328. if (_NSIG_WORDS > 1) {
  329. err |= __copy_to_user(frame->extramask, &set->sig[1],
  330. sizeof(frame->extramask)); }
  331. /* Give up earlier as i386, in case */
  332. if (err)
  333. return -EFAULT;
  334. /* Set up to return from userspace. If provided, use a stub
  335. already in userspace. */
  336. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  337. /*
  338. * On SH5 all edited pointers are subject to NEFF
  339. */
  340. DEREF_REG_PR = neff_sign_extend((unsigned long)
  341. ksig->ka->sa.sa_restorer | 0x1);
  342. } else {
  343. /*
  344. * Different approach on SH5.
  345. * . Endianness independent asm code gets placed in entry.S .
  346. * This is limited to four ASM instructions corresponding
  347. * to two long longs in size.
  348. * . err checking is done on the else branch only
  349. * . flush_icache_range() is called upon __put_user() only
  350. * . all edited pointers are subject to NEFF
  351. * . being code, linker turns ShMedia bit on, always
  352. * dereference index -1.
  353. */
  354. DEREF_REG_PR = neff_sign_extend((unsigned long)
  355. frame->retcode | 0x01);
  356. if (__copy_to_user(frame->retcode,
  357. (void *)((unsigned long)sa_default_restorer & (~1)), 16) != 0)
  358. return -EFAULT;
  359. /* Cohere the trampoline with the I-cache. */
  360. flush_cache_sigtramp(DEREF_REG_PR-1);
  361. }
  362. /*
  363. * Set up registers for signal handler.
  364. * All edited pointers are subject to NEFF.
  365. */
  366. regs->regs[REG_SP] = neff_sign_extend((unsigned long)frame);
  367. regs->regs[REG_ARG1] = sig; /* Arg for signal handler */
  368. /* FIXME:
  369. The glibc profiling support for SH-5 needs to be passed a sigcontext
  370. so it can retrieve the PC. At some point during 2003 the glibc
  371. support was changed to receive the sigcontext through the 2nd
  372. argument, but there are still versions of libc.so in use that use
  373. the 3rd argument. Until libc.so is stabilised, pass the sigcontext
  374. through both 2nd and 3rd arguments.
  375. */
  376. regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
  377. regs->regs[REG_ARG3] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
  378. regs->pc = neff_sign_extend((unsigned long)ksig->ka.sa.sa_handler);
  379. /* Broken %016Lx */
  380. pr_debug("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
  381. sig, current->comm, current->pid, frame,
  382. regs->pc >> 32, regs->pc & 0xffffffff,
  383. DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
  384. return 0;
  385. }
  386. static int setup_rt_frame(struct ksignal *kig, sigset_t *set,
  387. struct pt_regs *regs)
  388. {
  389. struct rt_sigframe __user *frame;
  390. int err = 0, sig = ksig->sig;
  391. frame = get_sigframe(&ksig->ka, regs->regs[REG_SP], sizeof(*frame));
  392. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  393. return -EFAULT;
  394. err |= __put_user(&frame->info, &frame->pinfo);
  395. err |= __put_user(&frame->uc, &frame->puc);
  396. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  397. /* Give up earlier as i386, in case */
  398. if (err)
  399. return -EFAULT;
  400. /* Create the ucontext. */
  401. err |= __put_user(0, &frame->uc.uc_flags);
  402. err |= __put_user(0, &frame->uc.uc_link);
  403. err |= __save_altstack(&frame->uc.uc_stack, regs->regs[REG_SP]);
  404. err |= setup_sigcontext(&frame->uc.uc_mcontext,
  405. regs, set->sig[0]);
  406. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  407. /* Give up earlier as i386, in case */
  408. if (err)
  409. return -EFAULT;
  410. /* Set up to return from userspace. If provided, use a stub
  411. already in userspace. */
  412. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  413. /*
  414. * On SH5 all edited pointers are subject to NEFF
  415. */
  416. DEREF_REG_PR = neff_sign_extend((unsigned long)
  417. ksig->ka.sa.sa_restorer | 0x1);
  418. } else {
  419. /*
  420. * Different approach on SH5.
  421. * . Endianness independent asm code gets placed in entry.S .
  422. * This is limited to four ASM instructions corresponding
  423. * to two long longs in size.
  424. * . err checking is done on the else branch only
  425. * . flush_icache_range() is called upon __put_user() only
  426. * . all edited pointers are subject to NEFF
  427. * . being code, linker turns ShMedia bit on, always
  428. * dereference index -1.
  429. */
  430. DEREF_REG_PR = neff_sign_extend((unsigned long)
  431. frame->retcode | 0x01);
  432. if (__copy_to_user(frame->retcode,
  433. (void *)((unsigned long)sa_default_rt_restorer & (~1)), 16) != 0)
  434. return -EFAULT;
  435. /* Cohere the trampoline with the I-cache. */
  436. flush_icache_range(DEREF_REG_PR-1, DEREF_REG_PR-1+15);
  437. }
  438. /*
  439. * Set up registers for signal handler.
  440. * All edited pointers are subject to NEFF.
  441. */
  442. regs->regs[REG_SP] = neff_sign_extend((unsigned long)frame);
  443. regs->regs[REG_ARG1] = sig; /* Arg for signal handler */
  444. regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->info;
  445. regs->regs[REG_ARG3] = (unsigned long long)(unsigned long)(signed long)&frame->uc.uc_mcontext;
  446. regs->pc = neff_sign_extend((unsigned long)ksig->ka.sa.sa_handler);
  447. pr_debug("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
  448. sig, current->comm, current->pid, frame,
  449. regs->pc >> 32, regs->pc & 0xffffffff,
  450. DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
  451. return 0;
  452. }
  453. /*
  454. * OK, we're invoking a handler
  455. */
  456. static void
  457. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  458. {
  459. sigset_t *oldset = sigmask_to_save();
  460. int ret;
  461. /* Set up the stack frame */
  462. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  463. ret = setup_rt_frame(ksig, oldset, regs);
  464. else
  465. ret = setup_frame(ksig, oldset, regs);
  466. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  467. }
  468. asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
  469. {
  470. if (thread_info_flags & _TIF_SIGPENDING)
  471. do_signal(regs);
  472. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  473. clear_thread_flag(TIF_NOTIFY_RESUME);
  474. tracehook_notify_resume(regs);
  475. }
  476. }