signal_32.c 36 KB

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