signal.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. * linux/arch/m68k/kernel/signal.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. */
  10. /*
  11. * Linux/m68k support by Hamish Macdonald
  12. *
  13. * 68060 fixes by Jesper Skov
  14. *
  15. * 1997-12-01 Modified for POSIX.1b signals by Andreas Schwab
  16. *
  17. * mathemu support by Roman Zippel
  18. * (Note: fpstate in the signal context is completely ignored for the emulator
  19. * and the internal floating point format is put on stack)
  20. */
  21. /*
  22. * ++roman (07/09/96): implemented signal stacks (specially for tosemu on
  23. * Atari :-) Current limitation: Only one sigstack can be active at one time.
  24. * If a second signal with SA_ONSTACK set arrives while working on a sigstack,
  25. * SA_ONSTACK is ignored. This behaviour avoids lots of trouble with nested
  26. * signal handlers!
  27. */
  28. #include <linux/sched.h>
  29. #include <linux/mm.h>
  30. #include <linux/kernel.h>
  31. #include <linux/signal.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/errno.h>
  34. #include <linux/wait.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/unistd.h>
  37. #include <linux/stddef.h>
  38. #include <linux/highuid.h>
  39. #include <linux/personality.h>
  40. #include <linux/tty.h>
  41. #include <linux/binfmts.h>
  42. #include <linux/module.h>
  43. #include <linux/tracehook.h>
  44. #include <asm/setup.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/pgtable.h>
  47. #include <asm/traps.h>
  48. #include <asm/ucontext.h>
  49. #include <asm/cacheflush.h>
  50. #ifdef CONFIG_MMU
  51. /*
  52. * Handle the slight differences in classic 68k and ColdFire trap frames.
  53. */
  54. #ifdef CONFIG_COLDFIRE
  55. #define FORMAT 4
  56. #define FMT4SIZE 0
  57. #else
  58. #define FORMAT 0
  59. #define FMT4SIZE sizeof(((struct frame *)0)->un.fmt4)
  60. #endif
  61. static const int frame_size_change[16] = {
  62. [1] = -1, /* sizeof(((struct frame *)0)->un.fmt1), */
  63. [2] = sizeof(((struct frame *)0)->un.fmt2),
  64. [3] = sizeof(((struct frame *)0)->un.fmt3),
  65. [4] = FMT4SIZE,
  66. [5] = -1, /* sizeof(((struct frame *)0)->un.fmt5), */
  67. [6] = -1, /* sizeof(((struct frame *)0)->un.fmt6), */
  68. [7] = sizeof(((struct frame *)0)->un.fmt7),
  69. [8] = -1, /* sizeof(((struct frame *)0)->un.fmt8), */
  70. [9] = sizeof(((struct frame *)0)->un.fmt9),
  71. [10] = sizeof(((struct frame *)0)->un.fmta),
  72. [11] = sizeof(((struct frame *)0)->un.fmtb),
  73. [12] = -1, /* sizeof(((struct frame *)0)->un.fmtc), */
  74. [13] = -1, /* sizeof(((struct frame *)0)->un.fmtd), */
  75. [14] = -1, /* sizeof(((struct frame *)0)->un.fmte), */
  76. [15] = -1, /* sizeof(((struct frame *)0)->un.fmtf), */
  77. };
  78. static inline int frame_extra_sizes(int f)
  79. {
  80. return frame_size_change[f];
  81. }
  82. int handle_kernel_fault(struct pt_regs *regs)
  83. {
  84. const struct exception_table_entry *fixup;
  85. struct pt_regs *tregs;
  86. /* Are we prepared to handle this kernel fault? */
  87. fixup = search_exception_tables(regs->pc);
  88. if (!fixup)
  89. return 0;
  90. /* Create a new four word stack frame, discarding the old one. */
  91. regs->stkadj = frame_extra_sizes(regs->format);
  92. tregs = (struct pt_regs *)((long)regs + regs->stkadj);
  93. tregs->vector = regs->vector;
  94. tregs->format = FORMAT;
  95. tregs->pc = fixup->fixup;
  96. tregs->sr = regs->sr;
  97. return 1;
  98. }
  99. void ptrace_signal_deliver(void)
  100. {
  101. struct pt_regs *regs = signal_pt_regs();
  102. if (regs->orig_d0 < 0)
  103. return;
  104. switch (regs->d0) {
  105. case -ERESTARTNOHAND:
  106. case -ERESTARTSYS:
  107. case -ERESTARTNOINTR:
  108. regs->d0 = regs->orig_d0;
  109. regs->orig_d0 = -1;
  110. regs->pc -= 2;
  111. break;
  112. }
  113. }
  114. static inline void push_cache (unsigned long vaddr)
  115. {
  116. /*
  117. * Using the old cache_push_v() was really a big waste.
  118. *
  119. * What we are trying to do is to flush 8 bytes to ram.
  120. * Flushing 2 cache lines of 16 bytes is much cheaper than
  121. * flushing 1 or 2 pages, as previously done in
  122. * cache_push_v().
  123. * Jes
  124. */
  125. if (CPU_IS_040) {
  126. unsigned long temp;
  127. __asm__ __volatile__ (".chip 68040\n\t"
  128. "nop\n\t"
  129. "ptestr (%1)\n\t"
  130. "movec %%mmusr,%0\n\t"
  131. ".chip 68k"
  132. : "=r" (temp)
  133. : "a" (vaddr));
  134. temp &= PAGE_MASK;
  135. temp |= vaddr & ~PAGE_MASK;
  136. __asm__ __volatile__ (".chip 68040\n\t"
  137. "nop\n\t"
  138. "cpushl %%bc,(%0)\n\t"
  139. ".chip 68k"
  140. : : "a" (temp));
  141. }
  142. else if (CPU_IS_060) {
  143. unsigned long temp;
  144. __asm__ __volatile__ (".chip 68060\n\t"
  145. "plpar (%0)\n\t"
  146. ".chip 68k"
  147. : "=a" (temp)
  148. : "0" (vaddr));
  149. __asm__ __volatile__ (".chip 68060\n\t"
  150. "cpushl %%bc,(%0)\n\t"
  151. ".chip 68k"
  152. : : "a" (temp));
  153. } else if (!CPU_IS_COLDFIRE) {
  154. /*
  155. * 68030/68020 have no writeback cache;
  156. * still need to clear icache.
  157. * Note that vaddr is guaranteed to be long word aligned.
  158. */
  159. unsigned long temp;
  160. asm volatile ("movec %%cacr,%0" : "=r" (temp));
  161. temp += 4;
  162. asm volatile ("movec %0,%%caar\n\t"
  163. "movec %1,%%cacr"
  164. : : "r" (vaddr), "r" (temp));
  165. asm volatile ("movec %0,%%caar\n\t"
  166. "movec %1,%%cacr"
  167. : : "r" (vaddr + 4), "r" (temp));
  168. } else {
  169. /* CPU_IS_COLDFIRE */
  170. #if defined(CONFIG_CACHE_COPYBACK)
  171. flush_cf_dcache(0, DCACHE_MAX_ADDR);
  172. #endif
  173. /* Invalidate instruction cache for the pushed bytes */
  174. clear_cf_icache(vaddr, vaddr + 8);
  175. }
  176. }
  177. static inline void adjustformat(struct pt_regs *regs)
  178. {
  179. }
  180. static inline void save_a5_state(struct sigcontext *sc, struct pt_regs *regs)
  181. {
  182. }
  183. #else /* CONFIG_MMU */
  184. void ret_from_user_signal(void);
  185. void ret_from_user_rt_signal(void);
  186. static inline int frame_extra_sizes(int f)
  187. {
  188. /* No frame size adjustments required on non-MMU CPUs */
  189. return 0;
  190. }
  191. static inline void adjustformat(struct pt_regs *regs)
  192. {
  193. ((struct switch_stack *)regs - 1)->a5 = current->mm->start_data;
  194. /*
  195. * set format byte to make stack appear modulo 4, which it will
  196. * be when doing the rte
  197. */
  198. regs->format = 0x4;
  199. }
  200. static inline void save_a5_state(struct sigcontext *sc, struct pt_regs *regs)
  201. {
  202. sc->sc_a5 = ((struct switch_stack *)regs - 1)->a5;
  203. }
  204. static inline void push_cache(unsigned long vaddr)
  205. {
  206. }
  207. #endif /* CONFIG_MMU */
  208. /*
  209. * Do a signal return; undo the signal stack.
  210. *
  211. * Keep the return code on the stack quadword aligned!
  212. * That makes the cache flush below easier.
  213. */
  214. struct sigframe
  215. {
  216. char __user *pretcode;
  217. int sig;
  218. int code;
  219. struct sigcontext __user *psc;
  220. char retcode[8];
  221. unsigned long extramask[_NSIG_WORDS-1];
  222. struct sigcontext sc;
  223. };
  224. struct rt_sigframe
  225. {
  226. char __user *pretcode;
  227. int sig;
  228. struct siginfo __user *pinfo;
  229. void __user *puc;
  230. char retcode[8];
  231. struct siginfo info;
  232. struct ucontext uc;
  233. };
  234. #define FPCONTEXT_SIZE 216
  235. #define uc_fpstate uc_filler[0]
  236. #define uc_formatvec uc_filler[FPCONTEXT_SIZE/4]
  237. #define uc_extra uc_filler[FPCONTEXT_SIZE/4+1]
  238. #ifdef CONFIG_FPU
  239. static unsigned char fpu_version; /* version number of fpu, set by setup_frame */
  240. static inline int restore_fpu_state(struct sigcontext *sc)
  241. {
  242. int err = 1;
  243. if (FPU_IS_EMU) {
  244. /* restore registers */
  245. memcpy(current->thread.fpcntl, sc->sc_fpcntl, 12);
  246. memcpy(current->thread.fp, sc->sc_fpregs, 24);
  247. return 0;
  248. }
  249. if (CPU_IS_060 ? sc->sc_fpstate[2] : sc->sc_fpstate[0]) {
  250. /* Verify the frame format. */
  251. if (!(CPU_IS_060 || CPU_IS_COLDFIRE) &&
  252. (sc->sc_fpstate[0] != fpu_version))
  253. goto out;
  254. if (CPU_IS_020_OR_030) {
  255. if (m68k_fputype & FPU_68881 &&
  256. !(sc->sc_fpstate[1] == 0x18 || sc->sc_fpstate[1] == 0xb4))
  257. goto out;
  258. if (m68k_fputype & FPU_68882 &&
  259. !(sc->sc_fpstate[1] == 0x38 || sc->sc_fpstate[1] == 0xd4))
  260. goto out;
  261. } else if (CPU_IS_040) {
  262. if (!(sc->sc_fpstate[1] == 0x00 ||
  263. sc->sc_fpstate[1] == 0x28 ||
  264. sc->sc_fpstate[1] == 0x60))
  265. goto out;
  266. } else if (CPU_IS_060) {
  267. if (!(sc->sc_fpstate[3] == 0x00 ||
  268. sc->sc_fpstate[3] == 0x60 ||
  269. sc->sc_fpstate[3] == 0xe0))
  270. goto out;
  271. } else if (CPU_IS_COLDFIRE) {
  272. if (!(sc->sc_fpstate[0] == 0x00 ||
  273. sc->sc_fpstate[0] == 0x05 ||
  274. sc->sc_fpstate[0] == 0xe5))
  275. goto out;
  276. } else
  277. goto out;
  278. if (CPU_IS_COLDFIRE) {
  279. __asm__ volatile ("fmovemd %0,%%fp0-%%fp1\n\t"
  280. "fmovel %1,%%fpcr\n\t"
  281. "fmovel %2,%%fpsr\n\t"
  282. "fmovel %3,%%fpiar"
  283. : /* no outputs */
  284. : "m" (sc->sc_fpregs[0]),
  285. "m" (sc->sc_fpcntl[0]),
  286. "m" (sc->sc_fpcntl[1]),
  287. "m" (sc->sc_fpcntl[2]));
  288. } else {
  289. __asm__ volatile (".chip 68k/68881\n\t"
  290. "fmovemx %0,%%fp0-%%fp1\n\t"
  291. "fmoveml %1,%%fpcr/%%fpsr/%%fpiar\n\t"
  292. ".chip 68k"
  293. : /* no outputs */
  294. : "m" (*sc->sc_fpregs),
  295. "m" (*sc->sc_fpcntl));
  296. }
  297. }
  298. if (CPU_IS_COLDFIRE) {
  299. __asm__ volatile ("frestore %0" : : "m" (*sc->sc_fpstate));
  300. } else {
  301. __asm__ volatile (".chip 68k/68881\n\t"
  302. "frestore %0\n\t"
  303. ".chip 68k"
  304. : : "m" (*sc->sc_fpstate));
  305. }
  306. err = 0;
  307. out:
  308. return err;
  309. }
  310. static inline int rt_restore_fpu_state(struct ucontext __user *uc)
  311. {
  312. unsigned char fpstate[FPCONTEXT_SIZE];
  313. int context_size = CPU_IS_060 ? 8 : (CPU_IS_COLDFIRE ? 12 : 0);
  314. fpregset_t fpregs;
  315. int err = 1;
  316. if (FPU_IS_EMU) {
  317. /* restore fpu control register */
  318. if (__copy_from_user(current->thread.fpcntl,
  319. uc->uc_mcontext.fpregs.f_fpcntl, 12))
  320. goto out;
  321. /* restore all other fpu register */
  322. if (__copy_from_user(current->thread.fp,
  323. uc->uc_mcontext.fpregs.f_fpregs, 96))
  324. goto out;
  325. return 0;
  326. }
  327. if (__get_user(*(long *)fpstate, (long __user *)&uc->uc_fpstate))
  328. goto out;
  329. if (CPU_IS_060 ? fpstate[2] : fpstate[0]) {
  330. if (!(CPU_IS_060 || CPU_IS_COLDFIRE))
  331. context_size = fpstate[1];
  332. /* Verify the frame format. */
  333. if (!(CPU_IS_060 || CPU_IS_COLDFIRE) &&
  334. (fpstate[0] != fpu_version))
  335. goto out;
  336. if (CPU_IS_020_OR_030) {
  337. if (m68k_fputype & FPU_68881 &&
  338. !(context_size == 0x18 || context_size == 0xb4))
  339. goto out;
  340. if (m68k_fputype & FPU_68882 &&
  341. !(context_size == 0x38 || context_size == 0xd4))
  342. goto out;
  343. } else if (CPU_IS_040) {
  344. if (!(context_size == 0x00 ||
  345. context_size == 0x28 ||
  346. context_size == 0x60))
  347. goto out;
  348. } else if (CPU_IS_060) {
  349. if (!(fpstate[3] == 0x00 ||
  350. fpstate[3] == 0x60 ||
  351. fpstate[3] == 0xe0))
  352. goto out;
  353. } else if (CPU_IS_COLDFIRE) {
  354. if (!(fpstate[3] == 0x00 ||
  355. fpstate[3] == 0x05 ||
  356. fpstate[3] == 0xe5))
  357. goto out;
  358. } else
  359. goto out;
  360. if (__copy_from_user(&fpregs, &uc->uc_mcontext.fpregs,
  361. sizeof(fpregs)))
  362. goto out;
  363. if (CPU_IS_COLDFIRE) {
  364. __asm__ volatile ("fmovemd %0,%%fp0-%%fp7\n\t"
  365. "fmovel %1,%%fpcr\n\t"
  366. "fmovel %2,%%fpsr\n\t"
  367. "fmovel %3,%%fpiar"
  368. : /* no outputs */
  369. : "m" (fpregs.f_fpregs[0]),
  370. "m" (fpregs.f_fpcntl[0]),
  371. "m" (fpregs.f_fpcntl[1]),
  372. "m" (fpregs.f_fpcntl[2]));
  373. } else {
  374. __asm__ volatile (".chip 68k/68881\n\t"
  375. "fmovemx %0,%%fp0-%%fp7\n\t"
  376. "fmoveml %1,%%fpcr/%%fpsr/%%fpiar\n\t"
  377. ".chip 68k"
  378. : /* no outputs */
  379. : "m" (*fpregs.f_fpregs),
  380. "m" (*fpregs.f_fpcntl));
  381. }
  382. }
  383. if (context_size &&
  384. __copy_from_user(fpstate + 4, (long __user *)&uc->uc_fpstate + 1,
  385. context_size))
  386. goto out;
  387. if (CPU_IS_COLDFIRE) {
  388. __asm__ volatile ("frestore %0" : : "m" (*fpstate));
  389. } else {
  390. __asm__ volatile (".chip 68k/68881\n\t"
  391. "frestore %0\n\t"
  392. ".chip 68k"
  393. : : "m" (*fpstate));
  394. }
  395. err = 0;
  396. out:
  397. return err;
  398. }
  399. /*
  400. * Set up a signal frame.
  401. */
  402. static inline void save_fpu_state(struct sigcontext *sc, struct pt_regs *regs)
  403. {
  404. if (FPU_IS_EMU) {
  405. /* save registers */
  406. memcpy(sc->sc_fpcntl, current->thread.fpcntl, 12);
  407. memcpy(sc->sc_fpregs, current->thread.fp, 24);
  408. return;
  409. }
  410. if (CPU_IS_COLDFIRE) {
  411. __asm__ volatile ("fsave %0"
  412. : : "m" (*sc->sc_fpstate) : "memory");
  413. } else {
  414. __asm__ volatile (".chip 68k/68881\n\t"
  415. "fsave %0\n\t"
  416. ".chip 68k"
  417. : : "m" (*sc->sc_fpstate) : "memory");
  418. }
  419. if (CPU_IS_060 ? sc->sc_fpstate[2] : sc->sc_fpstate[0]) {
  420. fpu_version = sc->sc_fpstate[0];
  421. if (CPU_IS_020_OR_030 &&
  422. regs->vector >= (VEC_FPBRUC * 4) &&
  423. regs->vector <= (VEC_FPNAN * 4)) {
  424. /* Clear pending exception in 68882 idle frame */
  425. if (*(unsigned short *) sc->sc_fpstate == 0x1f38)
  426. sc->sc_fpstate[0x38] |= 1 << 3;
  427. }
  428. if (CPU_IS_COLDFIRE) {
  429. __asm__ volatile ("fmovemd %%fp0-%%fp1,%0\n\t"
  430. "fmovel %%fpcr,%1\n\t"
  431. "fmovel %%fpsr,%2\n\t"
  432. "fmovel %%fpiar,%3"
  433. : "=m" (sc->sc_fpregs[0]),
  434. "=m" (sc->sc_fpcntl[0]),
  435. "=m" (sc->sc_fpcntl[1]),
  436. "=m" (sc->sc_fpcntl[2])
  437. : /* no inputs */
  438. : "memory");
  439. } else {
  440. __asm__ volatile (".chip 68k/68881\n\t"
  441. "fmovemx %%fp0-%%fp1,%0\n\t"
  442. "fmoveml %%fpcr/%%fpsr/%%fpiar,%1\n\t"
  443. ".chip 68k"
  444. : "=m" (*sc->sc_fpregs),
  445. "=m" (*sc->sc_fpcntl)
  446. : /* no inputs */
  447. : "memory");
  448. }
  449. }
  450. }
  451. static inline int rt_save_fpu_state(struct ucontext __user *uc, struct pt_regs *regs)
  452. {
  453. unsigned char fpstate[FPCONTEXT_SIZE];
  454. int context_size = CPU_IS_060 ? 8 : (CPU_IS_COLDFIRE ? 12 : 0);
  455. int err = 0;
  456. if (FPU_IS_EMU) {
  457. /* save fpu control register */
  458. err |= copy_to_user(uc->uc_mcontext.fpregs.f_fpcntl,
  459. current->thread.fpcntl, 12);
  460. /* save all other fpu register */
  461. err |= copy_to_user(uc->uc_mcontext.fpregs.f_fpregs,
  462. current->thread.fp, 96);
  463. return err;
  464. }
  465. if (CPU_IS_COLDFIRE) {
  466. __asm__ volatile ("fsave %0" : : "m" (*fpstate) : "memory");
  467. } else {
  468. __asm__ volatile (".chip 68k/68881\n\t"
  469. "fsave %0\n\t"
  470. ".chip 68k"
  471. : : "m" (*fpstate) : "memory");
  472. }
  473. err |= __put_user(*(long *)fpstate, (long __user *)&uc->uc_fpstate);
  474. if (CPU_IS_060 ? fpstate[2] : fpstate[0]) {
  475. fpregset_t fpregs;
  476. if (!(CPU_IS_060 || CPU_IS_COLDFIRE))
  477. context_size = fpstate[1];
  478. fpu_version = fpstate[0];
  479. if (CPU_IS_020_OR_030 &&
  480. regs->vector >= (VEC_FPBRUC * 4) &&
  481. regs->vector <= (VEC_FPNAN * 4)) {
  482. /* Clear pending exception in 68882 idle frame */
  483. if (*(unsigned short *) fpstate == 0x1f38)
  484. fpstate[0x38] |= 1 << 3;
  485. }
  486. if (CPU_IS_COLDFIRE) {
  487. __asm__ volatile ("fmovemd %%fp0-%%fp7,%0\n\t"
  488. "fmovel %%fpcr,%1\n\t"
  489. "fmovel %%fpsr,%2\n\t"
  490. "fmovel %%fpiar,%3"
  491. : "=m" (fpregs.f_fpregs[0]),
  492. "=m" (fpregs.f_fpcntl[0]),
  493. "=m" (fpregs.f_fpcntl[1]),
  494. "=m" (fpregs.f_fpcntl[2])
  495. : /* no inputs */
  496. : "memory");
  497. } else {
  498. __asm__ volatile (".chip 68k/68881\n\t"
  499. "fmovemx %%fp0-%%fp7,%0\n\t"
  500. "fmoveml %%fpcr/%%fpsr/%%fpiar,%1\n\t"
  501. ".chip 68k"
  502. : "=m" (*fpregs.f_fpregs),
  503. "=m" (*fpregs.f_fpcntl)
  504. : /* no inputs */
  505. : "memory");
  506. }
  507. err |= copy_to_user(&uc->uc_mcontext.fpregs, &fpregs,
  508. sizeof(fpregs));
  509. }
  510. if (context_size)
  511. err |= copy_to_user((long __user *)&uc->uc_fpstate + 1, fpstate + 4,
  512. context_size);
  513. return err;
  514. }
  515. #else /* CONFIG_FPU */
  516. /*
  517. * For the case with no FPU configured these all do nothing.
  518. */
  519. static inline int restore_fpu_state(struct sigcontext *sc)
  520. {
  521. return 0;
  522. }
  523. static inline int rt_restore_fpu_state(struct ucontext __user *uc)
  524. {
  525. return 0;
  526. }
  527. static inline void save_fpu_state(struct sigcontext *sc, struct pt_regs *regs)
  528. {
  529. }
  530. static inline int rt_save_fpu_state(struct ucontext __user *uc, struct pt_regs *regs)
  531. {
  532. return 0;
  533. }
  534. #endif /* CONFIG_FPU */
  535. static int mangle_kernel_stack(struct pt_regs *regs, int formatvec,
  536. void __user *fp)
  537. {
  538. int fsize = frame_extra_sizes(formatvec >> 12);
  539. if (fsize < 0) {
  540. /*
  541. * user process trying to return with weird frame format
  542. */
  543. #ifdef DEBUG
  544. printk("user process returning with weird frame format\n");
  545. #endif
  546. return 1;
  547. }
  548. if (!fsize) {
  549. regs->format = formatvec >> 12;
  550. regs->vector = formatvec & 0xfff;
  551. } else {
  552. struct switch_stack *sw = (struct switch_stack *)regs - 1;
  553. unsigned long buf[fsize / 2]; /* yes, twice as much */
  554. /* that'll make sure that expansion won't crap over data */
  555. if (copy_from_user(buf + fsize / 4, fp, fsize))
  556. return 1;
  557. /* point of no return */
  558. regs->format = formatvec >> 12;
  559. regs->vector = formatvec & 0xfff;
  560. #define frame_offset (sizeof(struct pt_regs)+sizeof(struct switch_stack))
  561. __asm__ __volatile__ (
  562. #ifdef CONFIG_COLDFIRE
  563. " movel %0,%/sp\n\t"
  564. " bra ret_from_signal\n"
  565. #else
  566. " movel %0,%/a0\n\t"
  567. " subl %1,%/a0\n\t" /* make room on stack */
  568. " movel %/a0,%/sp\n\t" /* set stack pointer */
  569. /* move switch_stack and pt_regs */
  570. "1: movel %0@+,%/a0@+\n\t"
  571. " dbra %2,1b\n\t"
  572. " lea %/sp@(%c3),%/a0\n\t" /* add offset of fmt */
  573. " lsrl #2,%1\n\t"
  574. " subql #1,%1\n\t"
  575. /* copy to the gap we'd made */
  576. "2: movel %4@+,%/a0@+\n\t"
  577. " dbra %1,2b\n\t"
  578. " bral ret_from_signal\n"
  579. #endif
  580. : /* no outputs, it doesn't ever return */
  581. : "a" (sw), "d" (fsize), "d" (frame_offset/4-1),
  582. "n" (frame_offset), "a" (buf + fsize/4)
  583. : "a0");
  584. #undef frame_offset
  585. }
  586. return 0;
  587. }
  588. static inline int
  589. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *usc, void __user *fp)
  590. {
  591. int formatvec;
  592. struct sigcontext context;
  593. int err = 0;
  594. /* Always make any pending restarted system calls return -EINTR */
  595. current->restart_block.fn = do_no_restart_syscall;
  596. /* get previous context */
  597. if (copy_from_user(&context, usc, sizeof(context)))
  598. goto badframe;
  599. /* restore passed registers */
  600. regs->d0 = context.sc_d0;
  601. regs->d1 = context.sc_d1;
  602. regs->a0 = context.sc_a0;
  603. regs->a1 = context.sc_a1;
  604. regs->sr = (regs->sr & 0xff00) | (context.sc_sr & 0xff);
  605. regs->pc = context.sc_pc;
  606. regs->orig_d0 = -1; /* disable syscall checks */
  607. wrusp(context.sc_usp);
  608. formatvec = context.sc_formatvec;
  609. err = restore_fpu_state(&context);
  610. if (err || mangle_kernel_stack(regs, formatvec, fp))
  611. goto badframe;
  612. return 0;
  613. badframe:
  614. return 1;
  615. }
  616. static inline int
  617. rt_restore_ucontext(struct pt_regs *regs, struct switch_stack *sw,
  618. struct ucontext __user *uc)
  619. {
  620. int temp;
  621. greg_t __user *gregs = uc->uc_mcontext.gregs;
  622. unsigned long usp;
  623. int err;
  624. /* Always make any pending restarted system calls return -EINTR */
  625. current->restart_block.fn = do_no_restart_syscall;
  626. err = __get_user(temp, &uc->uc_mcontext.version);
  627. if (temp != MCONTEXT_VERSION)
  628. goto badframe;
  629. /* restore passed registers */
  630. err |= __get_user(regs->d0, &gregs[0]);
  631. err |= __get_user(regs->d1, &gregs[1]);
  632. err |= __get_user(regs->d2, &gregs[2]);
  633. err |= __get_user(regs->d3, &gregs[3]);
  634. err |= __get_user(regs->d4, &gregs[4]);
  635. err |= __get_user(regs->d5, &gregs[5]);
  636. err |= __get_user(sw->d6, &gregs[6]);
  637. err |= __get_user(sw->d7, &gregs[7]);
  638. err |= __get_user(regs->a0, &gregs[8]);
  639. err |= __get_user(regs->a1, &gregs[9]);
  640. err |= __get_user(regs->a2, &gregs[10]);
  641. err |= __get_user(sw->a3, &gregs[11]);
  642. err |= __get_user(sw->a4, &gregs[12]);
  643. err |= __get_user(sw->a5, &gregs[13]);
  644. err |= __get_user(sw->a6, &gregs[14]);
  645. err |= __get_user(usp, &gregs[15]);
  646. wrusp(usp);
  647. err |= __get_user(regs->pc, &gregs[16]);
  648. err |= __get_user(temp, &gregs[17]);
  649. regs->sr = (regs->sr & 0xff00) | (temp & 0xff);
  650. regs->orig_d0 = -1; /* disable syscall checks */
  651. err |= __get_user(temp, &uc->uc_formatvec);
  652. err |= rt_restore_fpu_state(uc);
  653. err |= restore_altstack(&uc->uc_stack);
  654. if (err)
  655. goto badframe;
  656. if (mangle_kernel_stack(regs, temp, &uc->uc_extra))
  657. goto badframe;
  658. return 0;
  659. badframe:
  660. return 1;
  661. }
  662. asmlinkage int do_sigreturn(unsigned long __unused)
  663. {
  664. struct switch_stack *sw = (struct switch_stack *) &__unused;
  665. struct pt_regs *regs = (struct pt_regs *) (sw + 1);
  666. unsigned long usp = rdusp();
  667. struct sigframe __user *frame = (struct sigframe __user *)(usp - 4);
  668. sigset_t set;
  669. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  670. goto badframe;
  671. if (__get_user(set.sig[0], &frame->sc.sc_mask) ||
  672. (_NSIG_WORDS > 1 &&
  673. __copy_from_user(&set.sig[1], &frame->extramask,
  674. sizeof(frame->extramask))))
  675. goto badframe;
  676. set_current_blocked(&set);
  677. if (restore_sigcontext(regs, &frame->sc, frame + 1))
  678. goto badframe;
  679. return regs->d0;
  680. badframe:
  681. force_sig(SIGSEGV, current);
  682. return 0;
  683. }
  684. asmlinkage int do_rt_sigreturn(unsigned long __unused)
  685. {
  686. struct switch_stack *sw = (struct switch_stack *) &__unused;
  687. struct pt_regs *regs = (struct pt_regs *) (sw + 1);
  688. unsigned long usp = rdusp();
  689. struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(usp - 4);
  690. sigset_t set;
  691. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  692. goto badframe;
  693. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  694. goto badframe;
  695. set_current_blocked(&set);
  696. if (rt_restore_ucontext(regs, sw, &frame->uc))
  697. goto badframe;
  698. return regs->d0;
  699. badframe:
  700. force_sig(SIGSEGV, current);
  701. return 0;
  702. }
  703. static void setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
  704. unsigned long mask)
  705. {
  706. sc->sc_mask = mask;
  707. sc->sc_usp = rdusp();
  708. sc->sc_d0 = regs->d0;
  709. sc->sc_d1 = regs->d1;
  710. sc->sc_a0 = regs->a0;
  711. sc->sc_a1 = regs->a1;
  712. sc->sc_sr = regs->sr;
  713. sc->sc_pc = regs->pc;
  714. sc->sc_formatvec = regs->format << 12 | regs->vector;
  715. save_a5_state(sc, regs);
  716. save_fpu_state(sc, regs);
  717. }
  718. static inline int rt_setup_ucontext(struct ucontext __user *uc, struct pt_regs *regs)
  719. {
  720. struct switch_stack *sw = (struct switch_stack *)regs - 1;
  721. greg_t __user *gregs = uc->uc_mcontext.gregs;
  722. int err = 0;
  723. err |= __put_user(MCONTEXT_VERSION, &uc->uc_mcontext.version);
  724. err |= __put_user(regs->d0, &gregs[0]);
  725. err |= __put_user(regs->d1, &gregs[1]);
  726. err |= __put_user(regs->d2, &gregs[2]);
  727. err |= __put_user(regs->d3, &gregs[3]);
  728. err |= __put_user(regs->d4, &gregs[4]);
  729. err |= __put_user(regs->d5, &gregs[5]);
  730. err |= __put_user(sw->d6, &gregs[6]);
  731. err |= __put_user(sw->d7, &gregs[7]);
  732. err |= __put_user(regs->a0, &gregs[8]);
  733. err |= __put_user(regs->a1, &gregs[9]);
  734. err |= __put_user(regs->a2, &gregs[10]);
  735. err |= __put_user(sw->a3, &gregs[11]);
  736. err |= __put_user(sw->a4, &gregs[12]);
  737. err |= __put_user(sw->a5, &gregs[13]);
  738. err |= __put_user(sw->a6, &gregs[14]);
  739. err |= __put_user(rdusp(), &gregs[15]);
  740. err |= __put_user(regs->pc, &gregs[16]);
  741. err |= __put_user(regs->sr, &gregs[17]);
  742. err |= __put_user((regs->format << 12) | regs->vector, &uc->uc_formatvec);
  743. err |= rt_save_fpu_state(uc, regs);
  744. return err;
  745. }
  746. static inline void __user *
  747. get_sigframe(struct ksignal *ksig, size_t frame_size)
  748. {
  749. unsigned long usp = sigsp(rdusp(), ksig);
  750. return (void __user *)((usp - frame_size) & -8UL);
  751. }
  752. static int setup_frame(struct ksignal *ksig, sigset_t *set,
  753. struct pt_regs *regs)
  754. {
  755. struct sigframe __user *frame;
  756. int fsize = frame_extra_sizes(regs->format);
  757. struct sigcontext context;
  758. int err = 0, sig = ksig->sig;
  759. if (fsize < 0) {
  760. #ifdef DEBUG
  761. printk ("setup_frame: Unknown frame format %#x\n",
  762. regs->format);
  763. #endif
  764. return -EFAULT;
  765. }
  766. frame = get_sigframe(ksig, sizeof(*frame) + fsize);
  767. if (fsize)
  768. err |= copy_to_user (frame + 1, regs + 1, fsize);
  769. err |= __put_user(sig, &frame->sig);
  770. err |= __put_user(regs->vector, &frame->code);
  771. err |= __put_user(&frame->sc, &frame->psc);
  772. if (_NSIG_WORDS > 1)
  773. err |= copy_to_user(frame->extramask, &set->sig[1],
  774. sizeof(frame->extramask));
  775. setup_sigcontext(&context, regs, set->sig[0]);
  776. err |= copy_to_user (&frame->sc, &context, sizeof(context));
  777. /* Set up to return from userspace. */
  778. #ifdef CONFIG_MMU
  779. err |= __put_user(frame->retcode, &frame->pretcode);
  780. /* moveq #,d0; trap #0 */
  781. err |= __put_user(0x70004e40 + (__NR_sigreturn << 16),
  782. (long __user *)(frame->retcode));
  783. #else
  784. err |= __put_user((void *) ret_from_user_signal, &frame->pretcode);
  785. #endif
  786. if (err)
  787. return -EFAULT;
  788. push_cache ((unsigned long) &frame->retcode);
  789. /*
  790. * Set up registers for signal handler. All the state we are about
  791. * to destroy is successfully copied to sigframe.
  792. */
  793. wrusp ((unsigned long) frame);
  794. regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  795. adjustformat(regs);
  796. /*
  797. * This is subtle; if we build more than one sigframe, all but the
  798. * first one will see frame format 0 and have fsize == 0, so we won't
  799. * screw stkadj.
  800. */
  801. if (fsize)
  802. regs->stkadj = fsize;
  803. /* Prepare to skip over the extra stuff in the exception frame. */
  804. if (regs->stkadj) {
  805. struct pt_regs *tregs =
  806. (struct pt_regs *)((ulong)regs + regs->stkadj);
  807. #ifdef DEBUG
  808. printk("Performing stackadjust=%04x\n", regs->stkadj);
  809. #endif
  810. /* This must be copied with decreasing addresses to
  811. handle overlaps. */
  812. tregs->vector = 0;
  813. tregs->format = 0;
  814. tregs->pc = regs->pc;
  815. tregs->sr = regs->sr;
  816. }
  817. return 0;
  818. }
  819. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  820. struct pt_regs *regs)
  821. {
  822. struct rt_sigframe __user *frame;
  823. int fsize = frame_extra_sizes(regs->format);
  824. int err = 0, sig = ksig->sig;
  825. if (fsize < 0) {
  826. #ifdef DEBUG
  827. printk ("setup_frame: Unknown frame format %#x\n",
  828. regs->format);
  829. #endif
  830. return -EFAULT;
  831. }
  832. frame = get_sigframe(ksig, sizeof(*frame));
  833. if (fsize)
  834. err |= copy_to_user (&frame->uc.uc_extra, regs + 1, fsize);
  835. err |= __put_user(sig, &frame->sig);
  836. err |= __put_user(&frame->info, &frame->pinfo);
  837. err |= __put_user(&frame->uc, &frame->puc);
  838. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  839. /* Create the ucontext. */
  840. err |= __put_user(0, &frame->uc.uc_flags);
  841. err |= __put_user(NULL, &frame->uc.uc_link);
  842. err |= __save_altstack(&frame->uc.uc_stack, rdusp());
  843. err |= rt_setup_ucontext(&frame->uc, regs);
  844. err |= copy_to_user (&frame->uc.uc_sigmask, set, sizeof(*set));
  845. /* Set up to return from userspace. */
  846. #ifdef CONFIG_MMU
  847. err |= __put_user(frame->retcode, &frame->pretcode);
  848. #ifdef __mcoldfire__
  849. /* movel #__NR_rt_sigreturn,d0; trap #0 */
  850. err |= __put_user(0x203c0000, (long __user *)(frame->retcode + 0));
  851. err |= __put_user(0x00004e40 + (__NR_rt_sigreturn << 16),
  852. (long __user *)(frame->retcode + 4));
  853. #else
  854. /* moveq #,d0; notb d0; trap #0 */
  855. err |= __put_user(0x70004600 + ((__NR_rt_sigreturn ^ 0xff) << 16),
  856. (long __user *)(frame->retcode + 0));
  857. err |= __put_user(0x4e40, (short __user *)(frame->retcode + 4));
  858. #endif
  859. #else
  860. err |= __put_user((void *) ret_from_user_rt_signal, &frame->pretcode);
  861. #endif /* CONFIG_MMU */
  862. if (err)
  863. return -EFAULT;
  864. push_cache ((unsigned long) &frame->retcode);
  865. /*
  866. * Set up registers for signal handler. All the state we are about
  867. * to destroy is successfully copied to sigframe.
  868. */
  869. wrusp ((unsigned long) frame);
  870. regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
  871. adjustformat(regs);
  872. /*
  873. * This is subtle; if we build more than one sigframe, all but the
  874. * first one will see frame format 0 and have fsize == 0, so we won't
  875. * screw stkadj.
  876. */
  877. if (fsize)
  878. regs->stkadj = fsize;
  879. /* Prepare to skip over the extra stuff in the exception frame. */
  880. if (regs->stkadj) {
  881. struct pt_regs *tregs =
  882. (struct pt_regs *)((ulong)regs + regs->stkadj);
  883. #ifdef DEBUG
  884. printk("Performing stackadjust=%04x\n", regs->stkadj);
  885. #endif
  886. /* This must be copied with decreasing addresses to
  887. handle overlaps. */
  888. tregs->vector = 0;
  889. tregs->format = 0;
  890. tregs->pc = regs->pc;
  891. tregs->sr = regs->sr;
  892. }
  893. return 0;
  894. }
  895. static inline void
  896. handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
  897. {
  898. switch (regs->d0) {
  899. case -ERESTARTNOHAND:
  900. if (!has_handler)
  901. goto do_restart;
  902. regs->d0 = -EINTR;
  903. break;
  904. case -ERESTART_RESTARTBLOCK:
  905. if (!has_handler) {
  906. regs->d0 = __NR_restart_syscall;
  907. regs->pc -= 2;
  908. break;
  909. }
  910. regs->d0 = -EINTR;
  911. break;
  912. case -ERESTARTSYS:
  913. if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
  914. regs->d0 = -EINTR;
  915. break;
  916. }
  917. /* fallthrough */
  918. case -ERESTARTNOINTR:
  919. do_restart:
  920. regs->d0 = regs->orig_d0;
  921. regs->pc -= 2;
  922. break;
  923. }
  924. }
  925. /*
  926. * OK, we're invoking a handler
  927. */
  928. static void
  929. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  930. {
  931. sigset_t *oldset = sigmask_to_save();
  932. int err;
  933. /* are we from a system call? */
  934. if (regs->orig_d0 >= 0)
  935. /* If so, check system call restarting.. */
  936. handle_restart(regs, &ksig->ka, 1);
  937. /* set up the stack frame */
  938. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  939. err = setup_rt_frame(ksig, oldset, regs);
  940. else
  941. err = setup_frame(ksig, oldset, regs);
  942. signal_setup_done(err, ksig, 0);
  943. if (test_thread_flag(TIF_DELAYED_TRACE)) {
  944. regs->sr &= ~0x8000;
  945. send_sig(SIGTRAP, current, 1);
  946. }
  947. }
  948. /*
  949. * Note that 'init' is a special process: it doesn't get signals it doesn't
  950. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  951. * mistake.
  952. */
  953. static void do_signal(struct pt_regs *regs)
  954. {
  955. struct ksignal ksig;
  956. current->thread.esp0 = (unsigned long) regs;
  957. if (get_signal(&ksig)) {
  958. /* Whee! Actually deliver the signal. */
  959. handle_signal(&ksig, regs);
  960. return;
  961. }
  962. /* Did we come from a system call? */
  963. if (regs->orig_d0 >= 0)
  964. /* Restart the system call - no handlers present */
  965. handle_restart(regs, NULL, 0);
  966. /* If there's no signal to deliver, we just restore the saved mask. */
  967. restore_saved_sigmask();
  968. }
  969. void do_notify_resume(struct pt_regs *regs)
  970. {
  971. if (test_thread_flag(TIF_SIGPENDING))
  972. do_signal(regs);
  973. if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
  974. tracehook_notify_resume(regs);
  975. }