signal.c 28 KB

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