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