signal.c 30 KB

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