signal_64.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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/freezer.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/unistd.h>
  24. #include <linux/stddef.h>
  25. #include <linux/tracehook.h>
  26. #include <asm/ucontext.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/fpu.h>
  31. #define REG_RET 9
  32. #define REG_ARG1 2
  33. #define REG_ARG2 3
  34. #define REG_ARG3 4
  35. #define REG_SP 15
  36. #define REG_PR 18
  37. #define REF_REG_RET regs->regs[REG_RET]
  38. #define REF_REG_SP regs->regs[REG_SP]
  39. #define DEREF_REG_PR regs->regs[REG_PR]
  40. #define DEBUG_SIG 0
  41. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  42. static int
  43. handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
  44. sigset_t *oldset, struct pt_regs * regs);
  45. static inline void
  46. handle_syscall_restart(struct pt_regs *regs, struct sigaction *sa)
  47. {
  48. /* If we're not from a syscall, bail out */
  49. if (regs->syscall_nr < 0)
  50. return;
  51. /* check for system call restart.. */
  52. switch (regs->regs[REG_RET]) {
  53. case -ERESTART_RESTARTBLOCK:
  54. case -ERESTARTNOHAND:
  55. no_system_call_restart:
  56. regs->regs[REG_RET] = -EINTR;
  57. regs->sr |= 1;
  58. break;
  59. case -ERESTARTSYS:
  60. if (!(sa->sa_flags & SA_RESTART))
  61. goto no_system_call_restart;
  62. /* fallthrough */
  63. case -ERESTARTNOINTR:
  64. /* Decode syscall # */
  65. regs->regs[REG_RET] = regs->syscall_nr;
  66. regs->pc -= 4;
  67. break;
  68. }
  69. }
  70. /*
  71. * Note that 'init' is a special process: it doesn't get signals it doesn't
  72. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  73. * mistake.
  74. *
  75. * Note that we go through the signals twice: once to check the signals that
  76. * the kernel can handle, and then we build all the user-level signal handling
  77. * stack-frames in one go after that.
  78. */
  79. static int do_signal(struct pt_regs *regs, sigset_t *oldset)
  80. {
  81. siginfo_t info;
  82. int signr;
  83. struct k_sigaction ka;
  84. /*
  85. * We want the common case to go fast, which
  86. * is why we may in certain cases get here from
  87. * kernel mode. Just return without doing anything
  88. * if so.
  89. */
  90. if (!user_mode(regs))
  91. return 1;
  92. if (try_to_freeze())
  93. goto no_signal;
  94. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  95. oldset = &current->saved_sigmask;
  96. else if (!oldset)
  97. oldset = &current->blocked;
  98. signr = get_signal_to_deliver(&info, &ka, regs, 0);
  99. if (signr > 0) {
  100. if (regs->sr & 1)
  101. handle_syscall_restart(regs, &ka.sa);
  102. /* Whee! Actually deliver the signal. */
  103. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  104. /*
  105. * If a signal was successfully delivered, the
  106. * saved sigmask is in its frame, and we can
  107. * clear the TIF_RESTORE_SIGMASK flag.
  108. */
  109. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  110. clear_thread_flag(TIF_RESTORE_SIGMASK);
  111. tracehook_signal_handler(signr, &info, &ka, regs, 0);
  112. return 1;
  113. }
  114. }
  115. no_signal:
  116. /* Did we come from a system call? */
  117. if (regs->syscall_nr >= 0) {
  118. /* Restart the system call - no handlers present */
  119. switch (regs->regs[REG_RET]) {
  120. case -ERESTARTNOHAND:
  121. case -ERESTARTSYS:
  122. case -ERESTARTNOINTR:
  123. /* Decode Syscall # */
  124. regs->regs[REG_RET] = regs->syscall_nr;
  125. regs->pc -= 4;
  126. break;
  127. case -ERESTART_RESTARTBLOCK:
  128. regs->regs[REG_RET] = __NR_restart_syscall;
  129. regs->pc -= 4;
  130. break;
  131. }
  132. }
  133. /* No signal to deliver -- put the saved sigmask back */
  134. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  135. clear_thread_flag(TIF_RESTORE_SIGMASK);
  136. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  137. }
  138. return 0;
  139. }
  140. /*
  141. * Atomically swap in the new signal mask, and wait for a signal.
  142. */
  143. asmlinkage int
  144. sys_sigsuspend(old_sigset_t mask,
  145. unsigned long r3, unsigned long r4, unsigned long r5,
  146. unsigned long r6, unsigned long r7,
  147. struct pt_regs * regs)
  148. {
  149. sigset_t saveset;
  150. mask &= _BLOCKABLE;
  151. spin_lock_irq(&current->sighand->siglock);
  152. saveset = current->blocked;
  153. siginitset(&current->blocked, mask);
  154. recalc_sigpending();
  155. spin_unlock_irq(&current->sighand->siglock);
  156. REF_REG_RET = -EINTR;
  157. while (1) {
  158. current->state = TASK_INTERRUPTIBLE;
  159. schedule();
  160. regs->pc += 4; /* because sys_sigreturn decrements the pc */
  161. if (do_signal(regs, &saveset)) {
  162. /* pc now points at signal handler. Need to decrement
  163. it because entry.S will increment it. */
  164. regs->pc -= 4;
  165. return -EINTR;
  166. }
  167. }
  168. }
  169. asmlinkage int
  170. sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize,
  171. unsigned long r4, unsigned long r5, unsigned long r6,
  172. unsigned long r7,
  173. struct pt_regs * regs)
  174. {
  175. sigset_t saveset, newset;
  176. /* XXX: Don't preclude handling different sized sigset_t's. */
  177. if (sigsetsize != sizeof(sigset_t))
  178. return -EINVAL;
  179. if (copy_from_user(&newset, unewset, sizeof(newset)))
  180. return -EFAULT;
  181. sigdelsetmask(&newset, ~_BLOCKABLE);
  182. spin_lock_irq(&current->sighand->siglock);
  183. saveset = current->blocked;
  184. current->blocked = newset;
  185. recalc_sigpending();
  186. spin_unlock_irq(&current->sighand->siglock);
  187. REF_REG_RET = -EINTR;
  188. while (1) {
  189. current->state = TASK_INTERRUPTIBLE;
  190. schedule();
  191. regs->pc += 4; /* because sys_sigreturn decrements the pc */
  192. if (do_signal(regs, &saveset)) {
  193. /* pc now points at signal handler. Need to decrement
  194. it because entry.S will increment it. */
  195. regs->pc -= 4;
  196. return -EINTR;
  197. }
  198. }
  199. }
  200. asmlinkage int
  201. sys_sigaction(int sig, const struct old_sigaction __user *act,
  202. struct old_sigaction __user *oact)
  203. {
  204. struct k_sigaction new_ka, old_ka;
  205. int ret;
  206. if (act) {
  207. old_sigset_t mask;
  208. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  209. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  210. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  211. return -EFAULT;
  212. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  213. __get_user(mask, &act->sa_mask);
  214. siginitset(&new_ka.sa.sa_mask, mask);
  215. }
  216. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  217. if (!ret && oact) {
  218. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  219. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  220. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  221. return -EFAULT;
  222. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  223. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  224. }
  225. return ret;
  226. }
  227. asmlinkage int
  228. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  229. unsigned long r4, unsigned long r5, unsigned long r6,
  230. unsigned long r7,
  231. struct pt_regs * regs)
  232. {
  233. return do_sigaltstack(uss, uoss, REF_REG_SP);
  234. }
  235. /*
  236. * Do a signal return; undo the signal stack.
  237. */
  238. struct sigframe {
  239. struct sigcontext sc;
  240. unsigned long extramask[_NSIG_WORDS-1];
  241. long long retcode[2];
  242. };
  243. struct rt_sigframe {
  244. struct siginfo __user *pinfo;
  245. void *puc;
  246. struct siginfo info;
  247. struct ucontext uc;
  248. long long retcode[2];
  249. };
  250. #ifdef CONFIG_SH_FPU
  251. static inline int
  252. restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
  253. {
  254. int err = 0;
  255. int fpvalid;
  256. err |= __get_user (fpvalid, &sc->sc_fpvalid);
  257. conditional_used_math(fpvalid);
  258. if (! fpvalid)
  259. return err;
  260. if (current == last_task_used_math) {
  261. last_task_used_math = NULL;
  262. regs->sr |= SR_FD;
  263. }
  264. err |= __copy_from_user(&current->thread.fpu.hard, &sc->sc_fpregs[0],
  265. (sizeof(long long) * 32) + (sizeof(int) * 1));
  266. return err;
  267. }
  268. static inline int
  269. setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
  270. {
  271. int err = 0;
  272. int fpvalid;
  273. fpvalid = !!used_math();
  274. err |= __put_user(fpvalid, &sc->sc_fpvalid);
  275. if (! fpvalid)
  276. return err;
  277. if (current == last_task_used_math) {
  278. enable_fpu();
  279. save_fpu(current, regs);
  280. disable_fpu();
  281. last_task_used_math = NULL;
  282. regs->sr |= SR_FD;
  283. }
  284. err |= __copy_to_user(&sc->sc_fpregs[0], &current->thread.fpu.hard,
  285. (sizeof(long long) * 32) + (sizeof(int) * 1));
  286. clear_used_math();
  287. return err;
  288. }
  289. #else
  290. static inline int
  291. restore_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
  292. {
  293. return 0;
  294. }
  295. static inline int
  296. setup_sigcontext_fpu(struct pt_regs *regs, struct sigcontext __user *sc)
  297. {
  298. return 0;
  299. }
  300. #endif
  301. static int
  302. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, long long *r2_p)
  303. {
  304. unsigned int err = 0;
  305. unsigned long long current_sr, new_sr;
  306. #define SR_MASK 0xffff8cfd
  307. #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
  308. COPY(regs[0]); COPY(regs[1]); COPY(regs[2]); COPY(regs[3]);
  309. COPY(regs[4]); COPY(regs[5]); COPY(regs[6]); COPY(regs[7]);
  310. COPY(regs[8]); COPY(regs[9]); COPY(regs[10]); COPY(regs[11]);
  311. COPY(regs[12]); COPY(regs[13]); COPY(regs[14]); COPY(regs[15]);
  312. COPY(regs[16]); COPY(regs[17]); COPY(regs[18]); COPY(regs[19]);
  313. COPY(regs[20]); COPY(regs[21]); COPY(regs[22]); COPY(regs[23]);
  314. COPY(regs[24]); COPY(regs[25]); COPY(regs[26]); COPY(regs[27]);
  315. COPY(regs[28]); COPY(regs[29]); COPY(regs[30]); COPY(regs[31]);
  316. COPY(regs[32]); COPY(regs[33]); COPY(regs[34]); COPY(regs[35]);
  317. COPY(regs[36]); COPY(regs[37]); COPY(regs[38]); COPY(regs[39]);
  318. COPY(regs[40]); COPY(regs[41]); COPY(regs[42]); COPY(regs[43]);
  319. COPY(regs[44]); COPY(regs[45]); COPY(regs[46]); COPY(regs[47]);
  320. COPY(regs[48]); COPY(regs[49]); COPY(regs[50]); COPY(regs[51]);
  321. COPY(regs[52]); COPY(regs[53]); COPY(regs[54]); COPY(regs[55]);
  322. COPY(regs[56]); COPY(regs[57]); COPY(regs[58]); COPY(regs[59]);
  323. COPY(regs[60]); COPY(regs[61]); COPY(regs[62]);
  324. COPY(tregs[0]); COPY(tregs[1]); COPY(tregs[2]); COPY(tregs[3]);
  325. COPY(tregs[4]); COPY(tregs[5]); COPY(tregs[6]); COPY(tregs[7]);
  326. /* Prevent the signal handler manipulating SR in a way that can
  327. crash the kernel. i.e. only allow S, Q, M, PR, SZ, FR to be
  328. modified */
  329. current_sr = regs->sr;
  330. err |= __get_user(new_sr, &sc->sc_sr);
  331. regs->sr &= SR_MASK;
  332. regs->sr |= (new_sr & ~SR_MASK);
  333. COPY(pc);
  334. #undef COPY
  335. /* Must do this last in case it sets regs->sr.fd (i.e. after rest of sr
  336. * has been restored above.) */
  337. err |= restore_sigcontext_fpu(regs, sc);
  338. regs->syscall_nr = -1; /* disable syscall checks */
  339. err |= __get_user(*r2_p, &sc->sc_regs[REG_RET]);
  340. return err;
  341. }
  342. asmlinkage int sys_sigreturn(unsigned long r2, unsigned long r3,
  343. unsigned long r4, unsigned long r5,
  344. unsigned long r6, unsigned long r7,
  345. struct pt_regs * regs)
  346. {
  347. struct sigframe __user *frame = (struct sigframe __user *) (long) REF_REG_SP;
  348. sigset_t set;
  349. long long ret;
  350. /* Always make any pending restarted system calls return -EINTR */
  351. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  352. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  353. goto badframe;
  354. if (__get_user(set.sig[0], &frame->sc.oldmask)
  355. || (_NSIG_WORDS > 1
  356. && __copy_from_user(&set.sig[1], &frame->extramask,
  357. sizeof(frame->extramask))))
  358. goto badframe;
  359. sigdelsetmask(&set, ~_BLOCKABLE);
  360. spin_lock_irq(&current->sighand->siglock);
  361. current->blocked = set;
  362. recalc_sigpending();
  363. spin_unlock_irq(&current->sighand->siglock);
  364. if (restore_sigcontext(regs, &frame->sc, &ret))
  365. goto badframe;
  366. regs->pc -= 4;
  367. return (int) ret;
  368. badframe:
  369. force_sig(SIGSEGV, current);
  370. return 0;
  371. }
  372. asmlinkage int sys_rt_sigreturn(unsigned long r2, unsigned long r3,
  373. unsigned long r4, unsigned long r5,
  374. unsigned long r6, unsigned long r7,
  375. struct pt_regs * regs)
  376. {
  377. struct rt_sigframe __user *frame = (struct rt_sigframe __user *) (long) REF_REG_SP;
  378. sigset_t set;
  379. stack_t __user st;
  380. long long ret;
  381. /* Always make any pending restarted system calls return -EINTR */
  382. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  383. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  384. goto badframe;
  385. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  386. goto badframe;
  387. sigdelsetmask(&set, ~_BLOCKABLE);
  388. spin_lock_irq(&current->sighand->siglock);
  389. current->blocked = set;
  390. recalc_sigpending();
  391. spin_unlock_irq(&current->sighand->siglock);
  392. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ret))
  393. goto badframe;
  394. regs->pc -= 4;
  395. if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
  396. goto badframe;
  397. /* It is more difficult to avoid calling this function than to
  398. call it and ignore errors. */
  399. do_sigaltstack(&st, NULL, REF_REG_SP);
  400. return (int) ret;
  401. badframe:
  402. force_sig(SIGSEGV, current);
  403. return 0;
  404. }
  405. /*
  406. * Set up a signal frame.
  407. */
  408. static int
  409. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  410. unsigned long mask)
  411. {
  412. int err = 0;
  413. /* Do this first, otherwise is this sets sr->fd, that value isn't preserved. */
  414. err |= setup_sigcontext_fpu(regs, sc);
  415. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  416. COPY(regs[0]); COPY(regs[1]); COPY(regs[2]); COPY(regs[3]);
  417. COPY(regs[4]); COPY(regs[5]); COPY(regs[6]); COPY(regs[7]);
  418. COPY(regs[8]); COPY(regs[9]); COPY(regs[10]); COPY(regs[11]);
  419. COPY(regs[12]); COPY(regs[13]); COPY(regs[14]); COPY(regs[15]);
  420. COPY(regs[16]); COPY(regs[17]); COPY(regs[18]); COPY(regs[19]);
  421. COPY(regs[20]); COPY(regs[21]); COPY(regs[22]); COPY(regs[23]);
  422. COPY(regs[24]); COPY(regs[25]); COPY(regs[26]); COPY(regs[27]);
  423. COPY(regs[28]); COPY(regs[29]); COPY(regs[30]); COPY(regs[31]);
  424. COPY(regs[32]); COPY(regs[33]); COPY(regs[34]); COPY(regs[35]);
  425. COPY(regs[36]); COPY(regs[37]); COPY(regs[38]); COPY(regs[39]);
  426. COPY(regs[40]); COPY(regs[41]); COPY(regs[42]); COPY(regs[43]);
  427. COPY(regs[44]); COPY(regs[45]); COPY(regs[46]); COPY(regs[47]);
  428. COPY(regs[48]); COPY(regs[49]); COPY(regs[50]); COPY(regs[51]);
  429. COPY(regs[52]); COPY(regs[53]); COPY(regs[54]); COPY(regs[55]);
  430. COPY(regs[56]); COPY(regs[57]); COPY(regs[58]); COPY(regs[59]);
  431. COPY(regs[60]); COPY(regs[61]); COPY(regs[62]);
  432. COPY(tregs[0]); COPY(tregs[1]); COPY(tregs[2]); COPY(tregs[3]);
  433. COPY(tregs[4]); COPY(tregs[5]); COPY(tregs[6]); COPY(tregs[7]);
  434. COPY(sr); COPY(pc);
  435. #undef COPY
  436. err |= __put_user(mask, &sc->oldmask);
  437. return err;
  438. }
  439. /*
  440. * Determine which stack to use..
  441. */
  442. static inline void __user *
  443. get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
  444. {
  445. if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! sas_ss_flags(sp))
  446. sp = current->sas_ss_sp + current->sas_ss_size;
  447. return (void __user *)((sp - frame_size) & -8ul);
  448. }
  449. void sa_default_restorer(void); /* See comments below */
  450. void sa_default_rt_restorer(void); /* See comments below */
  451. static int setup_frame(int sig, struct k_sigaction *ka,
  452. sigset_t *set, struct pt_regs *regs)
  453. {
  454. struct sigframe __user *frame;
  455. int err = 0;
  456. int signal;
  457. frame = get_sigframe(ka, regs->regs[REG_SP], sizeof(*frame));
  458. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  459. goto give_sigsegv;
  460. signal = current_thread_info()->exec_domain
  461. && current_thread_info()->exec_domain->signal_invmap
  462. && sig < 32
  463. ? current_thread_info()->exec_domain->signal_invmap[sig]
  464. : sig;
  465. err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
  466. /* Give up earlier as i386, in case */
  467. if (err)
  468. goto give_sigsegv;
  469. if (_NSIG_WORDS > 1) {
  470. err |= __copy_to_user(frame->extramask, &set->sig[1],
  471. sizeof(frame->extramask)); }
  472. /* Give up earlier as i386, in case */
  473. if (err)
  474. goto give_sigsegv;
  475. /* Set up to return from userspace. If provided, use a stub
  476. already in userspace. */
  477. if (ka->sa.sa_flags & SA_RESTORER) {
  478. DEREF_REG_PR = (unsigned long) ka->sa.sa_restorer | 0x1;
  479. /*
  480. * On SH5 all edited pointers are subject to NEFF
  481. */
  482. DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
  483. (DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
  484. } else {
  485. /*
  486. * Different approach on SH5.
  487. * . Endianness independent asm code gets placed in entry.S .
  488. * This is limited to four ASM instructions corresponding
  489. * to two long longs in size.
  490. * . err checking is done on the else branch only
  491. * . flush_icache_range() is called upon __put_user() only
  492. * . all edited pointers are subject to NEFF
  493. * . being code, linker turns ShMedia bit on, always
  494. * dereference index -1.
  495. */
  496. DEREF_REG_PR = (unsigned long) frame->retcode | 0x01;
  497. DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
  498. (DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
  499. if (__copy_to_user(frame->retcode,
  500. (void *)((unsigned long)sa_default_restorer & (~1)), 16) != 0)
  501. goto give_sigsegv;
  502. /* Cohere the trampoline with the I-cache. */
  503. flush_cache_sigtramp(DEREF_REG_PR-1);
  504. }
  505. /*
  506. * Set up registers for signal handler.
  507. * All edited pointers are subject to NEFF.
  508. */
  509. regs->regs[REG_SP] = (unsigned long) frame;
  510. regs->regs[REG_SP] = (regs->regs[REG_SP] & NEFF_SIGN) ?
  511. (regs->regs[REG_SP] | NEFF_MASK) : regs->regs[REG_SP];
  512. regs->regs[REG_ARG1] = signal; /* Arg for signal handler */
  513. /* FIXME:
  514. The glibc profiling support for SH-5 needs to be passed a sigcontext
  515. so it can retrieve the PC. At some point during 2003 the glibc
  516. support was changed to receive the sigcontext through the 2nd
  517. argument, but there are still versions of libc.so in use that use
  518. the 3rd argument. Until libc.so is stabilised, pass the sigcontext
  519. through both 2nd and 3rd arguments.
  520. */
  521. regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
  522. regs->regs[REG_ARG3] = (unsigned long long)(unsigned long)(signed long)&frame->sc;
  523. regs->pc = (unsigned long) ka->sa.sa_handler;
  524. regs->pc = (regs->pc & NEFF_SIGN) ? (regs->pc | NEFF_MASK) : regs->pc;
  525. set_fs(USER_DS);
  526. /* Broken %016Lx */
  527. pr_debug("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
  528. signal, current->comm, current->pid, frame,
  529. regs->pc >> 32, regs->pc & 0xffffffff,
  530. DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
  531. return 0;
  532. give_sigsegv:
  533. force_sigsegv(sig, current);
  534. return -EFAULT;
  535. }
  536. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  537. sigset_t *set, struct pt_regs *regs)
  538. {
  539. struct rt_sigframe __user *frame;
  540. int err = 0;
  541. int signal;
  542. frame = get_sigframe(ka, regs->regs[REG_SP], sizeof(*frame));
  543. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  544. goto give_sigsegv;
  545. signal = current_thread_info()->exec_domain
  546. && current_thread_info()->exec_domain->signal_invmap
  547. && sig < 32
  548. ? current_thread_info()->exec_domain->signal_invmap[sig]
  549. : sig;
  550. err |= __put_user(&frame->info, &frame->pinfo);
  551. err |= __put_user(&frame->uc, &frame->puc);
  552. err |= copy_siginfo_to_user(&frame->info, info);
  553. /* Give up earlier as i386, in case */
  554. if (err)
  555. goto give_sigsegv;
  556. /* Create the ucontext. */
  557. err |= __put_user(0, &frame->uc.uc_flags);
  558. err |= __put_user(0, &frame->uc.uc_link);
  559. err |= __put_user((void *)current->sas_ss_sp,
  560. &frame->uc.uc_stack.ss_sp);
  561. err |= __put_user(sas_ss_flags(regs->regs[REG_SP]),
  562. &frame->uc.uc_stack.ss_flags);
  563. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  564. err |= setup_sigcontext(&frame->uc.uc_mcontext,
  565. regs, set->sig[0]);
  566. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  567. /* Give up earlier as i386, in case */
  568. if (err)
  569. goto give_sigsegv;
  570. /* Set up to return from userspace. If provided, use a stub
  571. already in userspace. */
  572. if (ka->sa.sa_flags & SA_RESTORER) {
  573. DEREF_REG_PR = (unsigned long) ka->sa.sa_restorer | 0x1;
  574. /*
  575. * On SH5 all edited pointers are subject to NEFF
  576. */
  577. DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
  578. (DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
  579. } else {
  580. /*
  581. * Different approach on SH5.
  582. * . Endianness independent asm code gets placed in entry.S .
  583. * This is limited to four ASM instructions corresponding
  584. * to two long longs in size.
  585. * . err checking is done on the else branch only
  586. * . flush_icache_range() is called upon __put_user() only
  587. * . all edited pointers are subject to NEFF
  588. * . being code, linker turns ShMedia bit on, always
  589. * dereference index -1.
  590. */
  591. DEREF_REG_PR = (unsigned long) frame->retcode | 0x01;
  592. DEREF_REG_PR = (DEREF_REG_PR & NEFF_SIGN) ?
  593. (DEREF_REG_PR | NEFF_MASK) : DEREF_REG_PR;
  594. if (__copy_to_user(frame->retcode,
  595. (void *)((unsigned long)sa_default_rt_restorer & (~1)), 16) != 0)
  596. goto give_sigsegv;
  597. flush_icache_range(DEREF_REG_PR-1, DEREF_REG_PR-1+15);
  598. }
  599. /*
  600. * Set up registers for signal handler.
  601. * All edited pointers are subject to NEFF.
  602. */
  603. regs->regs[REG_SP] = (unsigned long) frame;
  604. regs->regs[REG_SP] = (regs->regs[REG_SP] & NEFF_SIGN) ?
  605. (regs->regs[REG_SP] | NEFF_MASK) : regs->regs[REG_SP];
  606. regs->regs[REG_ARG1] = signal; /* Arg for signal handler */
  607. regs->regs[REG_ARG2] = (unsigned long long)(unsigned long)(signed long)&frame->info;
  608. regs->regs[REG_ARG3] = (unsigned long long)(unsigned long)(signed long)&frame->uc.uc_mcontext;
  609. regs->pc = (unsigned long) ka->sa.sa_handler;
  610. regs->pc = (regs->pc & NEFF_SIGN) ? (regs->pc | NEFF_MASK) : regs->pc;
  611. set_fs(USER_DS);
  612. pr_debug("SIG deliver (#%d,%s:%d): sp=%p pc=%08Lx%08Lx link=%08Lx%08Lx\n",
  613. signal, current->comm, current->pid, frame,
  614. regs->pc >> 32, regs->pc & 0xffffffff,
  615. DEREF_REG_PR >> 32, DEREF_REG_PR & 0xffffffff);
  616. return 0;
  617. give_sigsegv:
  618. force_sigsegv(sig, current);
  619. return -EFAULT;
  620. }
  621. /*
  622. * OK, we're invoking a handler
  623. */
  624. static int
  625. handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
  626. sigset_t *oldset, struct pt_regs * regs)
  627. {
  628. int ret;
  629. /* Set up the stack frame */
  630. if (ka->sa.sa_flags & SA_SIGINFO)
  631. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  632. else
  633. ret = setup_frame(sig, ka, oldset, regs);
  634. if (ka->sa.sa_flags & SA_ONESHOT)
  635. ka->sa.sa_handler = SIG_DFL;
  636. if (ret == 0) {
  637. spin_lock_irq(&current->sighand->siglock);
  638. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  639. if (!(ka->sa.sa_flags & SA_NODEFER))
  640. sigaddset(&current->blocked,sig);
  641. recalc_sigpending();
  642. spin_unlock_irq(&current->sighand->siglock);
  643. }
  644. return ret;
  645. }
  646. asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
  647. {
  648. if (thread_info_flags & _TIF_SIGPENDING)
  649. do_signal(regs, 0);
  650. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  651. clear_thread_flag(TIF_NOTIFY_RESUME);
  652. tracehook_notify_resume(regs);
  653. }
  654. }