signal.c 20 KB

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