signal_32.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * Signal handling for 32bit PPC and 32bit tasks on 64bit PPC
  3. *
  4. * PowerPC version
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. * Copyright (C) 2001 IBM
  7. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  8. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  9. *
  10. * Derived from "arch/i386/kernel/signal.c"
  11. * Copyright (C) 1991, 1992 Linus Torvalds
  12. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #include <linux/sched.h>
  20. #include <linux/mm.h>
  21. #include <linux/smp.h>
  22. #include <linux/kernel.h>
  23. #include <linux/signal.h>
  24. #include <linux/errno.h>
  25. #include <linux/elf.h>
  26. #ifdef CONFIG_PPC64
  27. #include <linux/syscalls.h>
  28. #include <linux/compat.h>
  29. #include <linux/ptrace.h>
  30. #else
  31. #include <linux/wait.h>
  32. #include <linux/ptrace.h>
  33. #include <linux/unistd.h>
  34. #include <linux/stddef.h>
  35. #include <linux/tty.h>
  36. #include <linux/binfmts.h>
  37. #include <linux/freezer.h>
  38. #endif
  39. #include <asm/uaccess.h>
  40. #include <asm/cacheflush.h>
  41. #include <asm/syscalls.h>
  42. #include <asm/sigcontext.h>
  43. #include <asm/vdso.h>
  44. #ifdef CONFIG_PPC64
  45. #include "ppc32.h"
  46. #include <asm/unistd.h>
  47. #else
  48. #include <asm/ucontext.h>
  49. #include <asm/pgtable.h>
  50. #endif
  51. #include "signal.h"
  52. #undef DEBUG_SIG
  53. #ifdef CONFIG_PPC64
  54. #define sys_sigsuspend compat_sys_sigsuspend
  55. #define sys_rt_sigsuspend compat_sys_rt_sigsuspend
  56. #define sys_rt_sigreturn compat_sys_rt_sigreturn
  57. #define sys_sigaction compat_sys_sigaction
  58. #define sys_swapcontext compat_sys_swapcontext
  59. #define sys_sigreturn compat_sys_sigreturn
  60. #define old_sigaction old_sigaction32
  61. #define sigcontext sigcontext32
  62. #define mcontext mcontext32
  63. #define ucontext ucontext32
  64. /*
  65. * Returning 0 means we return to userspace via
  66. * ret_from_except and thus restore all user
  67. * registers from *regs. This is what we need
  68. * to do when a signal has been delivered.
  69. */
  70. #define GP_REGS_SIZE min(sizeof(elf_gregset_t32), sizeof(struct pt_regs32))
  71. #undef __SIGNAL_FRAMESIZE
  72. #define __SIGNAL_FRAMESIZE __SIGNAL_FRAMESIZE32
  73. #undef ELF_NVRREG
  74. #define ELF_NVRREG ELF_NVRREG32
  75. /*
  76. * Functions for flipping sigsets (thanks to brain dead generic
  77. * implementation that makes things simple for little endian only)
  78. */
  79. static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
  80. {
  81. compat_sigset_t cset;
  82. switch (_NSIG_WORDS) {
  83. case 4: cset.sig[5] = set->sig[3] & 0xffffffffull;
  84. cset.sig[7] = set->sig[3] >> 32;
  85. case 3: cset.sig[4] = set->sig[2] & 0xffffffffull;
  86. cset.sig[5] = set->sig[2] >> 32;
  87. case 2: cset.sig[2] = set->sig[1] & 0xffffffffull;
  88. cset.sig[3] = set->sig[1] >> 32;
  89. case 1: cset.sig[0] = set->sig[0] & 0xffffffffull;
  90. cset.sig[1] = set->sig[0] >> 32;
  91. }
  92. return copy_to_user(uset, &cset, sizeof(*uset));
  93. }
  94. static inline int get_sigset_t(sigset_t *set,
  95. const compat_sigset_t __user *uset)
  96. {
  97. compat_sigset_t s32;
  98. if (copy_from_user(&s32, uset, sizeof(*uset)))
  99. return -EFAULT;
  100. /*
  101. * Swap the 2 words of the 64-bit sigset_t (they are stored
  102. * in the "wrong" endian in 32-bit user storage).
  103. */
  104. switch (_NSIG_WORDS) {
  105. case 4: set->sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
  106. case 3: set->sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
  107. case 2: set->sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
  108. case 1: set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
  109. }
  110. return 0;
  111. }
  112. static inline int get_old_sigaction(struct k_sigaction *new_ka,
  113. struct old_sigaction __user *act)
  114. {
  115. compat_old_sigset_t mask;
  116. compat_uptr_t handler, restorer;
  117. if (get_user(handler, &act->sa_handler) ||
  118. __get_user(restorer, &act->sa_restorer) ||
  119. __get_user(new_ka->sa.sa_flags, &act->sa_flags) ||
  120. __get_user(mask, &act->sa_mask))
  121. return -EFAULT;
  122. new_ka->sa.sa_handler = compat_ptr(handler);
  123. new_ka->sa.sa_restorer = compat_ptr(restorer);
  124. siginitset(&new_ka->sa.sa_mask, mask);
  125. return 0;
  126. }
  127. #define to_user_ptr(p) ptr_to_compat(p)
  128. #define from_user_ptr(p) compat_ptr(p)
  129. static inline int save_general_regs(struct pt_regs *regs,
  130. struct mcontext __user *frame)
  131. {
  132. elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
  133. int i;
  134. WARN_ON(!FULL_REGS(regs));
  135. for (i = 0; i <= PT_RESULT; i ++) {
  136. if (i == 14 && !FULL_REGS(regs))
  137. i = 32;
  138. if (__put_user((unsigned int)gregs[i], &frame->mc_gregs[i]))
  139. return -EFAULT;
  140. }
  141. return 0;
  142. }
  143. static inline int restore_general_regs(struct pt_regs *regs,
  144. struct mcontext __user *sr)
  145. {
  146. elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
  147. int i;
  148. for (i = 0; i <= PT_RESULT; i++) {
  149. if ((i == PT_MSR) || (i == PT_SOFTE))
  150. continue;
  151. if (__get_user(gregs[i], &sr->mc_gregs[i]))
  152. return -EFAULT;
  153. }
  154. return 0;
  155. }
  156. #else /* CONFIG_PPC64 */
  157. #define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
  158. static inline int put_sigset_t(sigset_t __user *uset, sigset_t *set)
  159. {
  160. return copy_to_user(uset, set, sizeof(*uset));
  161. }
  162. static inline int get_sigset_t(sigset_t *set, const sigset_t __user *uset)
  163. {
  164. return copy_from_user(set, uset, sizeof(*uset));
  165. }
  166. static inline int get_old_sigaction(struct k_sigaction *new_ka,
  167. struct old_sigaction __user *act)
  168. {
  169. old_sigset_t mask;
  170. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  171. __get_user(new_ka->sa.sa_handler, &act->sa_handler) ||
  172. __get_user(new_ka->sa.sa_restorer, &act->sa_restorer))
  173. return -EFAULT;
  174. __get_user(new_ka->sa.sa_flags, &act->sa_flags);
  175. __get_user(mask, &act->sa_mask);
  176. siginitset(&new_ka->sa.sa_mask, mask);
  177. return 0;
  178. }
  179. #define to_user_ptr(p) ((unsigned long)(p))
  180. #define from_user_ptr(p) ((void __user *)(p))
  181. static inline int save_general_regs(struct pt_regs *regs,
  182. struct mcontext __user *frame)
  183. {
  184. WARN_ON(!FULL_REGS(regs));
  185. return __copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE);
  186. }
  187. static inline int restore_general_regs(struct pt_regs *regs,
  188. struct mcontext __user *sr)
  189. {
  190. /* copy up to but not including MSR */
  191. if (__copy_from_user(regs, &sr->mc_gregs,
  192. PT_MSR * sizeof(elf_greg_t)))
  193. return -EFAULT;
  194. /* copy from orig_r3 (the word after the MSR) up to the end */
  195. if (__copy_from_user(&regs->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
  196. GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
  197. return -EFAULT;
  198. return 0;
  199. }
  200. #endif /* CONFIG_PPC64 */
  201. /*
  202. * Atomically swap in the new signal mask, and wait for a signal.
  203. */
  204. long sys_sigsuspend(old_sigset_t mask)
  205. {
  206. mask &= _BLOCKABLE;
  207. spin_lock_irq(&current->sighand->siglock);
  208. current->saved_sigmask = current->blocked;
  209. siginitset(&current->blocked, mask);
  210. recalc_sigpending();
  211. spin_unlock_irq(&current->sighand->siglock);
  212. current->state = TASK_INTERRUPTIBLE;
  213. schedule();
  214. set_thread_flag(TIF_RESTORE_SIGMASK);
  215. return -ERESTARTNOHAND;
  216. }
  217. long sys_sigaction(int sig, struct old_sigaction __user *act,
  218. struct old_sigaction __user *oact)
  219. {
  220. struct k_sigaction new_ka, old_ka;
  221. int ret;
  222. #ifdef CONFIG_PPC64
  223. if (sig < 0)
  224. sig = -sig;
  225. #endif
  226. if (act) {
  227. if (get_old_sigaction(&new_ka, act))
  228. return -EFAULT;
  229. }
  230. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  231. if (!ret && oact) {
  232. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  233. __put_user(to_user_ptr(old_ka.sa.sa_handler),
  234. &oact->sa_handler) ||
  235. __put_user(to_user_ptr(old_ka.sa.sa_restorer),
  236. &oact->sa_restorer) ||
  237. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  238. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  239. return -EFAULT;
  240. }
  241. return ret;
  242. }
  243. /*
  244. * When we have signals to deliver, we set up on the
  245. * user stack, going down from the original stack pointer:
  246. * a sigregs struct
  247. * a sigcontext struct
  248. * a gap of __SIGNAL_FRAMESIZE bytes
  249. *
  250. * Each of these things must be a multiple of 16 bytes in size.
  251. *
  252. */
  253. struct sigregs {
  254. struct mcontext mctx; /* all the register values */
  255. /*
  256. * Programs using the rs6000/xcoff abi can save up to 19 gp
  257. * regs and 18 fp regs below sp before decrementing it.
  258. */
  259. int abigap[56];
  260. };
  261. /* We use the mc_pad field for the signal return trampoline. */
  262. #define tramp mc_pad
  263. /*
  264. * When we have rt signals to deliver, we set up on the
  265. * user stack, going down from the original stack pointer:
  266. * one rt_sigframe struct (siginfo + ucontext + ABI gap)
  267. * a gap of __SIGNAL_FRAMESIZE+16 bytes
  268. * (the +16 is to get the siginfo and ucontext in the same
  269. * positions as in older kernels).
  270. *
  271. * Each of these things must be a multiple of 16 bytes in size.
  272. *
  273. */
  274. struct rt_sigframe {
  275. #ifdef CONFIG_PPC64
  276. compat_siginfo_t info;
  277. #else
  278. struct siginfo info;
  279. #endif
  280. struct ucontext uc;
  281. /*
  282. * Programs using the rs6000/xcoff abi can save up to 19 gp
  283. * regs and 18 fp regs below sp before decrementing it.
  284. */
  285. int abigap[56];
  286. };
  287. /*
  288. * Save the current user registers on the user stack.
  289. * We only save the altivec/spe registers if the process has used
  290. * altivec/spe instructions at some point.
  291. */
  292. static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
  293. int sigret)
  294. {
  295. /* Make sure floating point registers are stored in regs */
  296. flush_fp_to_thread(current);
  297. /* save general and floating-point registers */
  298. if (save_general_regs(regs, frame) ||
  299. __copy_to_user(&frame->mc_fregs, current->thread.fpr,
  300. ELF_NFPREG * sizeof(double)))
  301. return 1;
  302. #ifdef CONFIG_ALTIVEC
  303. /* save altivec registers */
  304. if (current->thread.used_vr) {
  305. flush_altivec_to_thread(current);
  306. if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
  307. ELF_NVRREG * sizeof(vector128)))
  308. return 1;
  309. /* set MSR_VEC in the saved MSR value to indicate that
  310. frame->mc_vregs contains valid data */
  311. if (__put_user(regs->msr | MSR_VEC, &frame->mc_gregs[PT_MSR]))
  312. return 1;
  313. }
  314. /* else assert((regs->msr & MSR_VEC) == 0) */
  315. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  316. * use altivec. Since VSCR only contains 32 bits saved in the least
  317. * significant bits of a vector, we "cheat" and stuff VRSAVE in the
  318. * most significant bits of that same vector. --BenH
  319. */
  320. if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
  321. return 1;
  322. #endif /* CONFIG_ALTIVEC */
  323. #ifdef CONFIG_SPE
  324. /* save spe registers */
  325. if (current->thread.used_spe) {
  326. flush_spe_to_thread(current);
  327. if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
  328. ELF_NEVRREG * sizeof(u32)))
  329. return 1;
  330. /* set MSR_SPE in the saved MSR value to indicate that
  331. frame->mc_vregs contains valid data */
  332. if (__put_user(regs->msr | MSR_SPE, &frame->mc_gregs[PT_MSR]))
  333. return 1;
  334. }
  335. /* else assert((regs->msr & MSR_SPE) == 0) */
  336. /* We always copy to/from spefscr */
  337. if (__put_user(current->thread.spefscr, (u32 __user *)&frame->mc_vregs + ELF_NEVRREG))
  338. return 1;
  339. #endif /* CONFIG_SPE */
  340. if (sigret) {
  341. /* Set up the sigreturn trampoline: li r0,sigret; sc */
  342. if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
  343. || __put_user(0x44000002UL, &frame->tramp[1]))
  344. return 1;
  345. flush_icache_range((unsigned long) &frame->tramp[0],
  346. (unsigned long) &frame->tramp[2]);
  347. }
  348. return 0;
  349. }
  350. /*
  351. * Restore the current user register values from the user stack,
  352. * (except for MSR).
  353. */
  354. static long restore_user_regs(struct pt_regs *regs,
  355. struct mcontext __user *sr, int sig)
  356. {
  357. long err;
  358. unsigned int save_r2 = 0;
  359. unsigned long msr;
  360. /*
  361. * restore general registers but not including MSR or SOFTE. Also
  362. * take care of keeping r2 (TLS) intact if not a signal
  363. */
  364. if (!sig)
  365. save_r2 = (unsigned int)regs->gpr[2];
  366. err = restore_general_regs(regs, sr);
  367. err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
  368. if (!sig)
  369. regs->gpr[2] = (unsigned long) save_r2;
  370. if (err)
  371. return 1;
  372. /* if doing signal return, restore the previous little-endian mode */
  373. if (sig)
  374. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  375. /*
  376. * Do this before updating the thread state in
  377. * current->thread.fpr/vr/evr. That way, if we get preempted
  378. * and another task grabs the FPU/Altivec/SPE, it won't be
  379. * tempted to save the current CPU state into the thread_struct
  380. * and corrupt what we are writing there.
  381. */
  382. discard_lazy_cpu_state();
  383. /* force the process to reload the FP registers from
  384. current->thread when it next does FP instructions */
  385. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  386. if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
  387. sizeof(sr->mc_fregs)))
  388. return 1;
  389. #ifdef CONFIG_ALTIVEC
  390. /* force the process to reload the altivec registers from
  391. current->thread when it next does altivec instructions */
  392. regs->msr &= ~MSR_VEC;
  393. if (msr & MSR_VEC) {
  394. /* restore altivec registers from the stack */
  395. if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
  396. sizeof(sr->mc_vregs)))
  397. return 1;
  398. } else if (current->thread.used_vr)
  399. memset(current->thread.vr, 0, ELF_NVRREG * sizeof(vector128));
  400. /* Always get VRSAVE back */
  401. if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
  402. return 1;
  403. #endif /* CONFIG_ALTIVEC */
  404. #ifdef CONFIG_SPE
  405. /* force the process to reload the spe registers from
  406. current->thread when it next does spe instructions */
  407. regs->msr &= ~MSR_SPE;
  408. if (msr & MSR_SPE) {
  409. /* restore spe registers from the stack */
  410. if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
  411. ELF_NEVRREG * sizeof(u32)))
  412. return 1;
  413. } else if (current->thread.used_spe)
  414. memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
  415. /* Always get SPEFSCR back */
  416. if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs + ELF_NEVRREG))
  417. return 1;
  418. #endif /* CONFIG_SPE */
  419. return 0;
  420. }
  421. #ifdef CONFIG_PPC64
  422. long compat_sys_rt_sigaction(int sig, const struct sigaction32 __user *act,
  423. struct sigaction32 __user *oact, size_t sigsetsize)
  424. {
  425. struct k_sigaction new_ka, old_ka;
  426. int ret;
  427. /* XXX: Don't preclude handling different sized sigset_t's. */
  428. if (sigsetsize != sizeof(compat_sigset_t))
  429. return -EINVAL;
  430. if (act) {
  431. compat_uptr_t handler;
  432. ret = get_user(handler, &act->sa_handler);
  433. new_ka.sa.sa_handler = compat_ptr(handler);
  434. ret |= get_sigset_t(&new_ka.sa.sa_mask, &act->sa_mask);
  435. ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  436. if (ret)
  437. return -EFAULT;
  438. }
  439. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  440. if (!ret && oact) {
  441. ret = put_user(to_user_ptr(old_ka.sa.sa_handler), &oact->sa_handler);
  442. ret |= put_sigset_t(&oact->sa_mask, &old_ka.sa.sa_mask);
  443. ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  444. }
  445. return ret;
  446. }
  447. /*
  448. * Note: it is necessary to treat how as an unsigned int, with the
  449. * corresponding cast to a signed int to insure that the proper
  450. * conversion (sign extension) between the register representation
  451. * of a signed int (msr in 32-bit mode) and the register representation
  452. * of a signed int (msr in 64-bit mode) is performed.
  453. */
  454. long compat_sys_rt_sigprocmask(u32 how, compat_sigset_t __user *set,
  455. compat_sigset_t __user *oset, size_t sigsetsize)
  456. {
  457. sigset_t s;
  458. sigset_t __user *up;
  459. int ret;
  460. mm_segment_t old_fs = get_fs();
  461. if (set) {
  462. if (get_sigset_t(&s, set))
  463. return -EFAULT;
  464. }
  465. set_fs(KERNEL_DS);
  466. /* This is valid because of the set_fs() */
  467. up = (sigset_t __user *) &s;
  468. ret = sys_rt_sigprocmask((int)how, set ? up : NULL, oset ? up : NULL,
  469. sigsetsize);
  470. set_fs(old_fs);
  471. if (ret)
  472. return ret;
  473. if (oset) {
  474. if (put_sigset_t(oset, &s))
  475. return -EFAULT;
  476. }
  477. return 0;
  478. }
  479. long compat_sys_rt_sigpending(compat_sigset_t __user *set, compat_size_t sigsetsize)
  480. {
  481. sigset_t s;
  482. int ret;
  483. mm_segment_t old_fs = get_fs();
  484. set_fs(KERNEL_DS);
  485. /* The __user pointer cast is valid because of the set_fs() */
  486. ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
  487. set_fs(old_fs);
  488. if (!ret) {
  489. if (put_sigset_t(set, &s))
  490. return -EFAULT;
  491. }
  492. return ret;
  493. }
  494. int copy_siginfo_to_user32(struct compat_siginfo __user *d, siginfo_t *s)
  495. {
  496. int err;
  497. if (!access_ok (VERIFY_WRITE, d, sizeof(*d)))
  498. return -EFAULT;
  499. /* If you change siginfo_t structure, please be sure
  500. * this code is fixed accordingly.
  501. * It should never copy any pad contained in the structure
  502. * to avoid security leaks, but must copy the generic
  503. * 3 ints plus the relevant union member.
  504. * This routine must convert siginfo from 64bit to 32bit as well
  505. * at the same time.
  506. */
  507. err = __put_user(s->si_signo, &d->si_signo);
  508. err |= __put_user(s->si_errno, &d->si_errno);
  509. err |= __put_user((short)s->si_code, &d->si_code);
  510. if (s->si_code < 0)
  511. err |= __copy_to_user(&d->_sifields._pad, &s->_sifields._pad,
  512. SI_PAD_SIZE32);
  513. else switch(s->si_code >> 16) {
  514. case __SI_CHLD >> 16:
  515. err |= __put_user(s->si_pid, &d->si_pid);
  516. err |= __put_user(s->si_uid, &d->si_uid);
  517. err |= __put_user(s->si_utime, &d->si_utime);
  518. err |= __put_user(s->si_stime, &d->si_stime);
  519. err |= __put_user(s->si_status, &d->si_status);
  520. break;
  521. case __SI_FAULT >> 16:
  522. err |= __put_user((unsigned int)(unsigned long)s->si_addr,
  523. &d->si_addr);
  524. break;
  525. case __SI_POLL >> 16:
  526. err |= __put_user(s->si_band, &d->si_band);
  527. err |= __put_user(s->si_fd, &d->si_fd);
  528. break;
  529. case __SI_TIMER >> 16:
  530. err |= __put_user(s->si_tid, &d->si_tid);
  531. err |= __put_user(s->si_overrun, &d->si_overrun);
  532. err |= __put_user(s->si_int, &d->si_int);
  533. break;
  534. case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
  535. case __SI_MESGQ >> 16:
  536. err |= __put_user(s->si_int, &d->si_int);
  537. /* fallthrough */
  538. case __SI_KILL >> 16:
  539. default:
  540. err |= __put_user(s->si_pid, &d->si_pid);
  541. err |= __put_user(s->si_uid, &d->si_uid);
  542. break;
  543. }
  544. return err;
  545. }
  546. #define copy_siginfo_to_user copy_siginfo_to_user32
  547. /*
  548. * Note: it is necessary to treat pid and sig as unsigned ints, with the
  549. * corresponding cast to a signed int to insure that the proper conversion
  550. * (sign extension) between the register representation of a signed int
  551. * (msr in 32-bit mode) and the register representation of a signed int
  552. * (msr in 64-bit mode) is performed.
  553. */
  554. long compat_sys_rt_sigqueueinfo(u32 pid, u32 sig, compat_siginfo_t __user *uinfo)
  555. {
  556. siginfo_t info;
  557. int ret;
  558. mm_segment_t old_fs = get_fs();
  559. if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
  560. copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE32))
  561. return -EFAULT;
  562. set_fs (KERNEL_DS);
  563. /* The __user pointer cast is valid becasuse of the set_fs() */
  564. ret = sys_rt_sigqueueinfo((int)pid, (int)sig, (siginfo_t __user *) &info);
  565. set_fs (old_fs);
  566. return ret;
  567. }
  568. /*
  569. * Start Alternate signal stack support
  570. *
  571. * System Calls
  572. * sigaltatck compat_sys_sigaltstack
  573. */
  574. int compat_sys_sigaltstack(u32 __new, u32 __old, int r5,
  575. int r6, int r7, int r8, struct pt_regs *regs)
  576. {
  577. stack_32_t __user * newstack = compat_ptr(__new);
  578. stack_32_t __user * oldstack = compat_ptr(__old);
  579. stack_t uss, uoss;
  580. int ret;
  581. mm_segment_t old_fs;
  582. unsigned long sp;
  583. compat_uptr_t ss_sp;
  584. /*
  585. * set sp to the user stack on entry to the system call
  586. * the system call router sets R9 to the saved registers
  587. */
  588. sp = regs->gpr[1];
  589. /* Put new stack info in local 64 bit stack struct */
  590. if (newstack) {
  591. if (get_user(ss_sp, &newstack->ss_sp) ||
  592. __get_user(uss.ss_flags, &newstack->ss_flags) ||
  593. __get_user(uss.ss_size, &newstack->ss_size))
  594. return -EFAULT;
  595. uss.ss_sp = compat_ptr(ss_sp);
  596. }
  597. old_fs = get_fs();
  598. set_fs(KERNEL_DS);
  599. /* The __user pointer casts are valid because of the set_fs() */
  600. ret = do_sigaltstack(
  601. newstack ? (stack_t __user *) &uss : NULL,
  602. oldstack ? (stack_t __user *) &uoss : NULL,
  603. sp);
  604. set_fs(old_fs);
  605. /* Copy the stack information to the user output buffer */
  606. if (!ret && oldstack &&
  607. (put_user(ptr_to_compat(uoss.ss_sp), &oldstack->ss_sp) ||
  608. __put_user(uoss.ss_flags, &oldstack->ss_flags) ||
  609. __put_user(uoss.ss_size, &oldstack->ss_size)))
  610. return -EFAULT;
  611. return ret;
  612. }
  613. #endif /* CONFIG_PPC64 */
  614. /*
  615. * Set up a signal frame for a "real-time" signal handler
  616. * (one which gets siginfo).
  617. */
  618. int handle_rt_signal32(unsigned long sig, struct k_sigaction *ka,
  619. siginfo_t *info, sigset_t *oldset,
  620. struct pt_regs *regs, unsigned long newsp)
  621. {
  622. struct rt_sigframe __user *rt_sf;
  623. struct mcontext __user *frame;
  624. unsigned long origsp = newsp;
  625. /* Set up Signal Frame */
  626. /* Put a Real Time Context onto stack */
  627. newsp -= sizeof(*rt_sf);
  628. rt_sf = (struct rt_sigframe __user *)newsp;
  629. /* create a stack frame for the caller of the handler */
  630. newsp -= __SIGNAL_FRAMESIZE + 16;
  631. if (!access_ok(VERIFY_WRITE, (void __user *)newsp, origsp - newsp))
  632. goto badframe;
  633. /* Put the siginfo & fill in most of the ucontext */
  634. if (copy_siginfo_to_user(&rt_sf->info, info)
  635. || __put_user(0, &rt_sf->uc.uc_flags)
  636. || __put_user(0, &rt_sf->uc.uc_link)
  637. || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
  638. || __put_user(sas_ss_flags(regs->gpr[1]),
  639. &rt_sf->uc.uc_stack.ss_flags)
  640. || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
  641. || __put_user(to_user_ptr(&rt_sf->uc.uc_mcontext),
  642. &rt_sf->uc.uc_regs)
  643. || put_sigset_t(&rt_sf->uc.uc_sigmask, oldset))
  644. goto badframe;
  645. /* Save user registers on the stack */
  646. frame = &rt_sf->uc.uc_mcontext;
  647. if (vdso32_rt_sigtramp && current->mm->context.vdso_base) {
  648. if (save_user_regs(regs, frame, 0))
  649. goto badframe;
  650. regs->link = current->mm->context.vdso_base + vdso32_rt_sigtramp;
  651. } else {
  652. if (save_user_regs(regs, frame, __NR_rt_sigreturn))
  653. goto badframe;
  654. regs->link = (unsigned long) frame->tramp;
  655. }
  656. current->thread.fpscr.val = 0; /* turn off all fp exceptions */
  657. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  658. goto badframe;
  659. regs->gpr[1] = newsp;
  660. regs->gpr[3] = sig;
  661. regs->gpr[4] = (unsigned long) &rt_sf->info;
  662. regs->gpr[5] = (unsigned long) &rt_sf->uc;
  663. regs->gpr[6] = (unsigned long) rt_sf;
  664. regs->nip = (unsigned long) ka->sa.sa_handler;
  665. /* enter the signal handler in big-endian mode */
  666. regs->msr &= ~MSR_LE;
  667. regs->trap = 0;
  668. return 1;
  669. badframe:
  670. #ifdef DEBUG_SIG
  671. printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
  672. regs, frame, newsp);
  673. #endif
  674. force_sigsegv(sig, current);
  675. return 0;
  676. }
  677. static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
  678. {
  679. sigset_t set;
  680. struct mcontext __user *mcp;
  681. if (get_sigset_t(&set, &ucp->uc_sigmask))
  682. return -EFAULT;
  683. #ifdef CONFIG_PPC64
  684. {
  685. u32 cmcp;
  686. if (__get_user(cmcp, &ucp->uc_regs))
  687. return -EFAULT;
  688. mcp = (struct mcontext __user *)(u64)cmcp;
  689. /* no need to check access_ok(mcp), since mcp < 4GB */
  690. }
  691. #else
  692. if (__get_user(mcp, &ucp->uc_regs))
  693. return -EFAULT;
  694. if (!access_ok(VERIFY_READ, mcp, sizeof(*mcp)))
  695. return -EFAULT;
  696. #endif
  697. restore_sigmask(&set);
  698. if (restore_user_regs(regs, mcp, sig))
  699. return -EFAULT;
  700. return 0;
  701. }
  702. long sys_swapcontext(struct ucontext __user *old_ctx,
  703. struct ucontext __user *new_ctx,
  704. int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
  705. {
  706. unsigned char tmp;
  707. /* Context size is for future use. Right now, we only make sure
  708. * we are passed something we understand
  709. */
  710. if (ctx_size < sizeof(struct ucontext))
  711. return -EINVAL;
  712. if (old_ctx != NULL) {
  713. struct mcontext __user *mctx;
  714. /*
  715. * old_ctx might not be 16-byte aligned, in which
  716. * case old_ctx->uc_mcontext won't be either.
  717. * Because we have the old_ctx->uc_pad2 field
  718. * before old_ctx->uc_mcontext, we need to round down
  719. * from &old_ctx->uc_mcontext to a 16-byte boundary.
  720. */
  721. mctx = (struct mcontext __user *)
  722. ((unsigned long) &old_ctx->uc_mcontext & ~0xfUL);
  723. if (!access_ok(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
  724. || save_user_regs(regs, mctx, 0)
  725. || put_sigset_t(&old_ctx->uc_sigmask, &current->blocked)
  726. || __put_user(to_user_ptr(mctx), &old_ctx->uc_regs))
  727. return -EFAULT;
  728. }
  729. if (new_ctx == NULL)
  730. return 0;
  731. if (!access_ok(VERIFY_READ, new_ctx, sizeof(*new_ctx))
  732. || __get_user(tmp, (u8 __user *) new_ctx)
  733. || __get_user(tmp, (u8 __user *) (new_ctx + 1) - 1))
  734. return -EFAULT;
  735. /*
  736. * If we get a fault copying the context into the kernel's
  737. * image of the user's registers, we can't just return -EFAULT
  738. * because the user's registers will be corrupted. For instance
  739. * the NIP value may have been updated but not some of the
  740. * other registers. Given that we have done the access_ok
  741. * and successfully read the first and last bytes of the region
  742. * above, this should only happen in an out-of-memory situation
  743. * or if another thread unmaps the region containing the context.
  744. * We kill the task with a SIGSEGV in this situation.
  745. */
  746. if (do_setcontext(new_ctx, regs, 0))
  747. do_exit(SIGSEGV);
  748. set_thread_flag(TIF_RESTOREALL);
  749. return 0;
  750. }
  751. long sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
  752. struct pt_regs *regs)
  753. {
  754. struct rt_sigframe __user *rt_sf;
  755. /* Always make any pending restarted system calls return -EINTR */
  756. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  757. rt_sf = (struct rt_sigframe __user *)
  758. (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
  759. if (!access_ok(VERIFY_READ, rt_sf, sizeof(*rt_sf)))
  760. goto bad;
  761. if (do_setcontext(&rt_sf->uc, regs, 1))
  762. goto bad;
  763. /*
  764. * It's not clear whether or why it is desirable to save the
  765. * sigaltstack setting on signal delivery and restore it on
  766. * signal return. But other architectures do this and we have
  767. * always done it up until now so it is probably better not to
  768. * change it. -- paulus
  769. */
  770. #ifdef CONFIG_PPC64
  771. /*
  772. * We use the compat_sys_ version that does the 32/64 bits conversion
  773. * and takes userland pointer directly. What about error checking ?
  774. * nobody does any...
  775. */
  776. compat_sys_sigaltstack((u32)(u64)&rt_sf->uc.uc_stack, 0, 0, 0, 0, 0, regs);
  777. #else
  778. do_sigaltstack(&rt_sf->uc.uc_stack, NULL, regs->gpr[1]);
  779. #endif
  780. set_thread_flag(TIF_RESTOREALL);
  781. return 0;
  782. bad:
  783. force_sig(SIGSEGV, current);
  784. return 0;
  785. }
  786. #ifdef CONFIG_PPC32
  787. int sys_debug_setcontext(struct ucontext __user *ctx,
  788. int ndbg, struct sig_dbg_op __user *dbg,
  789. int r6, int r7, int r8,
  790. struct pt_regs *regs)
  791. {
  792. struct sig_dbg_op op;
  793. int i;
  794. unsigned char tmp;
  795. unsigned long new_msr = regs->msr;
  796. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  797. unsigned long new_dbcr0 = current->thread.dbcr0;
  798. #endif
  799. for (i=0; i<ndbg; i++) {
  800. if (copy_from_user(&op, dbg + i, sizeof(op)))
  801. return -EFAULT;
  802. switch (op.dbg_type) {
  803. case SIG_DBG_SINGLE_STEPPING:
  804. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  805. if (op.dbg_value) {
  806. new_msr |= MSR_DE;
  807. new_dbcr0 |= (DBCR0_IDM | DBCR0_IC);
  808. } else {
  809. new_msr &= ~MSR_DE;
  810. new_dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);
  811. }
  812. #else
  813. if (op.dbg_value)
  814. new_msr |= MSR_SE;
  815. else
  816. new_msr &= ~MSR_SE;
  817. #endif
  818. break;
  819. case SIG_DBG_BRANCH_TRACING:
  820. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  821. return -EINVAL;
  822. #else
  823. if (op.dbg_value)
  824. new_msr |= MSR_BE;
  825. else
  826. new_msr &= ~MSR_BE;
  827. #endif
  828. break;
  829. default:
  830. return -EINVAL;
  831. }
  832. }
  833. /* We wait until here to actually install the values in the
  834. registers so if we fail in the above loop, it will not
  835. affect the contents of these registers. After this point,
  836. failure is a problem, anyway, and it's very unlikely unless
  837. the user is really doing something wrong. */
  838. regs->msr = new_msr;
  839. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  840. current->thread.dbcr0 = new_dbcr0;
  841. #endif
  842. if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx))
  843. || __get_user(tmp, (u8 __user *) ctx)
  844. || __get_user(tmp, (u8 __user *) (ctx + 1) - 1))
  845. return -EFAULT;
  846. /*
  847. * If we get a fault copying the context into the kernel's
  848. * image of the user's registers, we can't just return -EFAULT
  849. * because the user's registers will be corrupted. For instance
  850. * the NIP value may have been updated but not some of the
  851. * other registers. Given that we have done the access_ok
  852. * and successfully read the first and last bytes of the region
  853. * above, this should only happen in an out-of-memory situation
  854. * or if another thread unmaps the region containing the context.
  855. * We kill the task with a SIGSEGV in this situation.
  856. */
  857. if (do_setcontext(ctx, regs, 1)) {
  858. force_sig(SIGSEGV, current);
  859. goto out;
  860. }
  861. /*
  862. * It's not clear whether or why it is desirable to save the
  863. * sigaltstack setting on signal delivery and restore it on
  864. * signal return. But other architectures do this and we have
  865. * always done it up until now so it is probably better not to
  866. * change it. -- paulus
  867. */
  868. do_sigaltstack(&ctx->uc_stack, NULL, regs->gpr[1]);
  869. set_thread_flag(TIF_RESTOREALL);
  870. out:
  871. return 0;
  872. }
  873. #endif
  874. /*
  875. * OK, we're invoking a handler
  876. */
  877. int handle_signal32(unsigned long sig, struct k_sigaction *ka,
  878. siginfo_t *info, sigset_t *oldset, struct pt_regs *regs,
  879. unsigned long newsp)
  880. {
  881. struct sigcontext __user *sc;
  882. struct sigregs __user *frame;
  883. unsigned long origsp = newsp;
  884. /* Set up Signal Frame */
  885. newsp -= sizeof(struct sigregs);
  886. frame = (struct sigregs __user *) newsp;
  887. /* Put a sigcontext on the stack */
  888. newsp -= sizeof(*sc);
  889. sc = (struct sigcontext __user *) newsp;
  890. /* create a stack frame for the caller of the handler */
  891. newsp -= __SIGNAL_FRAMESIZE;
  892. if (!access_ok(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
  893. goto badframe;
  894. #if _NSIG != 64
  895. #error "Please adjust handle_signal()"
  896. #endif
  897. if (__put_user(to_user_ptr(ka->sa.sa_handler), &sc->handler)
  898. || __put_user(oldset->sig[0], &sc->oldmask)
  899. #ifdef CONFIG_PPC64
  900. || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
  901. #else
  902. || __put_user(oldset->sig[1], &sc->_unused[3])
  903. #endif
  904. || __put_user(to_user_ptr(frame), &sc->regs)
  905. || __put_user(sig, &sc->signal))
  906. goto badframe;
  907. if (vdso32_sigtramp && current->mm->context.vdso_base) {
  908. if (save_user_regs(regs, &frame->mctx, 0))
  909. goto badframe;
  910. regs->link = current->mm->context.vdso_base + vdso32_sigtramp;
  911. } else {
  912. if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
  913. goto badframe;
  914. regs->link = (unsigned long) frame->mctx.tramp;
  915. }
  916. current->thread.fpscr.val = 0; /* turn off all fp exceptions */
  917. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  918. goto badframe;
  919. regs->gpr[1] = newsp;
  920. regs->gpr[3] = sig;
  921. regs->gpr[4] = (unsigned long) sc;
  922. regs->nip = (unsigned long) ka->sa.sa_handler;
  923. /* enter the signal handler in big-endian mode */
  924. regs->msr &= ~MSR_LE;
  925. regs->trap = 0;
  926. return 1;
  927. badframe:
  928. #ifdef DEBUG_SIG
  929. printk("badframe in handle_signal, regs=%p frame=%p newsp=%lx\n",
  930. regs, frame, newsp);
  931. #endif
  932. force_sigsegv(sig, current);
  933. return 0;
  934. }
  935. /*
  936. * Do a signal return; undo the signal stack.
  937. */
  938. long sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
  939. struct pt_regs *regs)
  940. {
  941. struct sigcontext __user *sc;
  942. struct sigcontext sigctx;
  943. struct mcontext __user *sr;
  944. sigset_t set;
  945. /* Always make any pending restarted system calls return -EINTR */
  946. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  947. sc = (struct sigcontext __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
  948. if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
  949. goto badframe;
  950. #ifdef CONFIG_PPC64
  951. /*
  952. * Note that PPC32 puts the upper 32 bits of the sigmask in the
  953. * unused part of the signal stackframe
  954. */
  955. set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3]) << 32);
  956. #else
  957. set.sig[0] = sigctx.oldmask;
  958. set.sig[1] = sigctx._unused[3];
  959. #endif
  960. restore_sigmask(&set);
  961. sr = (struct mcontext __user *)from_user_ptr(sigctx.regs);
  962. if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
  963. || restore_user_regs(regs, sr, 1))
  964. goto badframe;
  965. set_thread_flag(TIF_RESTOREALL);
  966. return 0;
  967. badframe:
  968. force_sig(SIGSEGV, current);
  969. return 0;
  970. }