signal.c 21 KB

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