signal_32.c 36 KB

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