signal_64.c 22 KB

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