signal_32.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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/pagemap.h>
  28. #include <linux/ratelimit.h>
  29. #include <linux/syscalls.h>
  30. #ifdef CONFIG_PPC64
  31. #include <linux/compat.h>
  32. #else
  33. #include <linux/wait.h>
  34. #include <linux/unistd.h>
  35. #include <linux/stddef.h>
  36. #include <linux/tty.h>
  37. #include <linux/binfmts.h>
  38. #endif
  39. #include <linux/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. #include <asm/tm.h>
  46. #include <asm/asm-prototypes.h>
  47. #ifdef CONFIG_PPC64
  48. #include "ppc32.h"
  49. #include <asm/unistd.h>
  50. #else
  51. #include <asm/ucontext.h>
  52. #include <asm/pgtable.h>
  53. #endif
  54. #include "signal.h"
  55. #ifdef CONFIG_PPC64
  56. #define old_sigaction old_sigaction32
  57. #define sigcontext sigcontext32
  58. #define mcontext mcontext32
  59. #define ucontext ucontext32
  60. #define __save_altstack __compat_save_altstack
  61. /*
  62. * Userspace code may pass a ucontext which doesn't include VSX added
  63. * at the end. We need to check for this case.
  64. */
  65. #define UCONTEXTSIZEWITHOUTVSX \
  66. (sizeof(struct ucontext) - sizeof(elf_vsrreghalf_t32))
  67. /*
  68. * Returning 0 means we return to userspace via
  69. * ret_from_except and thus restore all user
  70. * registers from *regs. This is what we need
  71. * to do when a signal has been delivered.
  72. */
  73. #define GP_REGS_SIZE min(sizeof(elf_gregset_t32), sizeof(struct pt_regs32))
  74. #undef __SIGNAL_FRAMESIZE
  75. #define __SIGNAL_FRAMESIZE __SIGNAL_FRAMESIZE32
  76. #undef ELF_NVRREG
  77. #define ELF_NVRREG ELF_NVRREG32
  78. /*
  79. * Functions for flipping sigsets (thanks to brain dead generic
  80. * implementation that makes things simple for little endian only)
  81. */
  82. static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
  83. {
  84. return put_compat_sigset(uset, set, sizeof(*uset));
  85. }
  86. static inline int get_sigset_t(sigset_t *set,
  87. const compat_sigset_t __user *uset)
  88. {
  89. return get_compat_sigset(set, uset);
  90. }
  91. #define to_user_ptr(p) ptr_to_compat(p)
  92. #define from_user_ptr(p) compat_ptr(p)
  93. static inline int save_general_regs(struct pt_regs *regs,
  94. struct mcontext __user *frame)
  95. {
  96. elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
  97. int i;
  98. /* Force usr to alway see softe as 1 (interrupts enabled) */
  99. elf_greg_t64 softe = 0x1;
  100. WARN_ON(!FULL_REGS(regs));
  101. for (i = 0; i <= PT_RESULT; i ++) {
  102. if (i == 14 && !FULL_REGS(regs))
  103. i = 32;
  104. if ( i == PT_SOFTE) {
  105. if(__put_user((unsigned int)softe, &frame->mc_gregs[i]))
  106. return -EFAULT;
  107. else
  108. continue;
  109. }
  110. if (__put_user((unsigned int)gregs[i], &frame->mc_gregs[i]))
  111. return -EFAULT;
  112. }
  113. return 0;
  114. }
  115. static inline int restore_general_regs(struct pt_regs *regs,
  116. struct mcontext __user *sr)
  117. {
  118. elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
  119. int i;
  120. for (i = 0; i <= PT_RESULT; i++) {
  121. if ((i == PT_MSR) || (i == PT_SOFTE))
  122. continue;
  123. if (__get_user(gregs[i], &sr->mc_gregs[i]))
  124. return -EFAULT;
  125. }
  126. return 0;
  127. }
  128. #else /* CONFIG_PPC64 */
  129. #define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
  130. static inline int put_sigset_t(sigset_t __user *uset, sigset_t *set)
  131. {
  132. return copy_to_user(uset, set, sizeof(*uset));
  133. }
  134. static inline int get_sigset_t(sigset_t *set, const sigset_t __user *uset)
  135. {
  136. return copy_from_user(set, uset, sizeof(*uset));
  137. }
  138. #define to_user_ptr(p) ((unsigned long)(p))
  139. #define from_user_ptr(p) ((void __user *)(p))
  140. static inline int save_general_regs(struct pt_regs *regs,
  141. struct mcontext __user *frame)
  142. {
  143. WARN_ON(!FULL_REGS(regs));
  144. return __copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE);
  145. }
  146. static inline int restore_general_regs(struct pt_regs *regs,
  147. struct mcontext __user *sr)
  148. {
  149. /* copy up to but not including MSR */
  150. if (__copy_from_user(regs, &sr->mc_gregs,
  151. PT_MSR * sizeof(elf_greg_t)))
  152. return -EFAULT;
  153. /* copy from orig_r3 (the word after the MSR) up to the end */
  154. if (__copy_from_user(&regs->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
  155. GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
  156. return -EFAULT;
  157. return 0;
  158. }
  159. #endif
  160. /*
  161. * When we have signals to deliver, we set up on the
  162. * user stack, going down from the original stack pointer:
  163. * an ABI gap of 56 words
  164. * an mcontext struct
  165. * a sigcontext struct
  166. * a gap of __SIGNAL_FRAMESIZE bytes
  167. *
  168. * Each of these things must be a multiple of 16 bytes in size. The following
  169. * structure represent all of this except the __SIGNAL_FRAMESIZE gap
  170. *
  171. */
  172. struct sigframe {
  173. struct sigcontext sctx; /* the sigcontext */
  174. struct mcontext mctx; /* all the register values */
  175. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  176. struct sigcontext sctx_transact;
  177. struct mcontext mctx_transact;
  178. #endif
  179. /*
  180. * Programs using the rs6000/xcoff abi can save up to 19 gp
  181. * regs and 18 fp regs below sp before decrementing it.
  182. */
  183. int abigap[56];
  184. };
  185. /* We use the mc_pad field for the signal return trampoline. */
  186. #define tramp mc_pad
  187. /*
  188. * When we have rt signals to deliver, we set up on the
  189. * user stack, going down from the original stack pointer:
  190. * one rt_sigframe struct (siginfo + ucontext + ABI gap)
  191. * a gap of __SIGNAL_FRAMESIZE+16 bytes
  192. * (the +16 is to get the siginfo and ucontext in the same
  193. * positions as in older kernels).
  194. *
  195. * Each of these things must be a multiple of 16 bytes in size.
  196. *
  197. */
  198. struct rt_sigframe {
  199. #ifdef CONFIG_PPC64
  200. compat_siginfo_t info;
  201. #else
  202. struct siginfo info;
  203. #endif
  204. struct ucontext uc;
  205. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  206. struct ucontext uc_transact;
  207. #endif
  208. /*
  209. * Programs using the rs6000/xcoff abi can save up to 19 gp
  210. * regs and 18 fp regs below sp before decrementing it.
  211. */
  212. int abigap[56];
  213. };
  214. #ifdef CONFIG_VSX
  215. unsigned long copy_fpr_to_user(void __user *to,
  216. struct task_struct *task)
  217. {
  218. u64 buf[ELF_NFPREG];
  219. int i;
  220. /* save FPR copy to local buffer then write to the thread_struct */
  221. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  222. buf[i] = task->thread.TS_FPR(i);
  223. buf[i] = task->thread.fp_state.fpscr;
  224. return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
  225. }
  226. unsigned long copy_fpr_from_user(struct task_struct *task,
  227. void __user *from)
  228. {
  229. u64 buf[ELF_NFPREG];
  230. int i;
  231. if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
  232. return 1;
  233. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  234. task->thread.TS_FPR(i) = buf[i];
  235. task->thread.fp_state.fpscr = buf[i];
  236. return 0;
  237. }
  238. unsigned long copy_vsx_to_user(void __user *to,
  239. struct task_struct *task)
  240. {
  241. u64 buf[ELF_NVSRHALFREG];
  242. int i;
  243. /* save FPR copy to local buffer then write to the thread_struct */
  244. for (i = 0; i < ELF_NVSRHALFREG; i++)
  245. buf[i] = task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET];
  246. return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
  247. }
  248. unsigned long copy_vsx_from_user(struct task_struct *task,
  249. void __user *from)
  250. {
  251. u64 buf[ELF_NVSRHALFREG];
  252. int i;
  253. if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
  254. return 1;
  255. for (i = 0; i < ELF_NVSRHALFREG ; i++)
  256. task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  257. return 0;
  258. }
  259. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  260. unsigned long copy_ckfpr_to_user(void __user *to,
  261. struct task_struct *task)
  262. {
  263. u64 buf[ELF_NFPREG];
  264. int i;
  265. /* save FPR copy to local buffer then write to the thread_struct */
  266. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  267. buf[i] = task->thread.TS_CKFPR(i);
  268. buf[i] = task->thread.ckfp_state.fpscr;
  269. return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
  270. }
  271. unsigned long copy_ckfpr_from_user(struct task_struct *task,
  272. void __user *from)
  273. {
  274. u64 buf[ELF_NFPREG];
  275. int i;
  276. if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
  277. return 1;
  278. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  279. task->thread.TS_CKFPR(i) = buf[i];
  280. task->thread.ckfp_state.fpscr = buf[i];
  281. return 0;
  282. }
  283. unsigned long copy_ckvsx_to_user(void __user *to,
  284. struct task_struct *task)
  285. {
  286. u64 buf[ELF_NVSRHALFREG];
  287. int i;
  288. /* save FPR copy to local buffer then write to the thread_struct */
  289. for (i = 0; i < ELF_NVSRHALFREG; i++)
  290. buf[i] = task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET];
  291. return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
  292. }
  293. unsigned long copy_ckvsx_from_user(struct task_struct *task,
  294. void __user *from)
  295. {
  296. u64 buf[ELF_NVSRHALFREG];
  297. int i;
  298. if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
  299. return 1;
  300. for (i = 0; i < ELF_NVSRHALFREG ; i++)
  301. task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  302. return 0;
  303. }
  304. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  305. #else
  306. inline unsigned long copy_fpr_to_user(void __user *to,
  307. struct task_struct *task)
  308. {
  309. return __copy_to_user(to, task->thread.fp_state.fpr,
  310. ELF_NFPREG * sizeof(double));
  311. }
  312. inline unsigned long copy_fpr_from_user(struct task_struct *task,
  313. void __user *from)
  314. {
  315. return __copy_from_user(task->thread.fp_state.fpr, from,
  316. ELF_NFPREG * sizeof(double));
  317. }
  318. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  319. inline unsigned long copy_ckfpr_to_user(void __user *to,
  320. struct task_struct *task)
  321. {
  322. return __copy_to_user(to, task->thread.ckfp_state.fpr,
  323. ELF_NFPREG * sizeof(double));
  324. }
  325. inline unsigned long copy_ckfpr_from_user(struct task_struct *task,
  326. void __user *from)
  327. {
  328. return __copy_from_user(task->thread.ckfp_state.fpr, from,
  329. ELF_NFPREG * sizeof(double));
  330. }
  331. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  332. #endif
  333. /*
  334. * Save the current user registers on the user stack.
  335. * We only save the altivec/spe registers if the process has used
  336. * altivec/spe instructions at some point.
  337. */
  338. static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
  339. struct mcontext __user *tm_frame, int sigret,
  340. int ctx_has_vsx_region)
  341. {
  342. unsigned long msr = regs->msr;
  343. /* Make sure floating point registers are stored in regs */
  344. flush_fp_to_thread(current);
  345. /* save general registers */
  346. if (save_general_regs(regs, frame))
  347. return 1;
  348. #ifdef CONFIG_ALTIVEC
  349. /* save altivec registers */
  350. if (current->thread.used_vr) {
  351. flush_altivec_to_thread(current);
  352. if (__copy_to_user(&frame->mc_vregs, &current->thread.vr_state,
  353. ELF_NVRREG * sizeof(vector128)))
  354. return 1;
  355. /* set MSR_VEC in the saved MSR value to indicate that
  356. frame->mc_vregs contains valid data */
  357. msr |= MSR_VEC;
  358. }
  359. /* else assert((regs->msr & MSR_VEC) == 0) */
  360. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  361. * use altivec. Since VSCR only contains 32 bits saved in the least
  362. * significant bits of a vector, we "cheat" and stuff VRSAVE in the
  363. * most significant bits of that same vector. --BenH
  364. * Note that the current VRSAVE value is in the SPR at this point.
  365. */
  366. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  367. current->thread.vrsave = mfspr(SPRN_VRSAVE);
  368. if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
  369. return 1;
  370. #endif /* CONFIG_ALTIVEC */
  371. if (copy_fpr_to_user(&frame->mc_fregs, current))
  372. return 1;
  373. /*
  374. * Clear the MSR VSX bit to indicate there is no valid state attached
  375. * to this context, except in the specific case below where we set it.
  376. */
  377. msr &= ~MSR_VSX;
  378. #ifdef CONFIG_VSX
  379. /*
  380. * Copy VSR 0-31 upper half from thread_struct to local
  381. * buffer, then write that to userspace. Also set MSR_VSX in
  382. * the saved MSR value to indicate that frame->mc_vregs
  383. * contains valid data
  384. */
  385. if (current->thread.used_vsr && ctx_has_vsx_region) {
  386. flush_vsx_to_thread(current);
  387. if (copy_vsx_to_user(&frame->mc_vsregs, current))
  388. return 1;
  389. msr |= MSR_VSX;
  390. }
  391. #endif /* CONFIG_VSX */
  392. #ifdef CONFIG_SPE
  393. /* save spe registers */
  394. if (current->thread.used_spe) {
  395. flush_spe_to_thread(current);
  396. if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
  397. ELF_NEVRREG * sizeof(u32)))
  398. return 1;
  399. /* set MSR_SPE in the saved MSR value to indicate that
  400. frame->mc_vregs contains valid data */
  401. msr |= MSR_SPE;
  402. }
  403. /* else assert((regs->msr & MSR_SPE) == 0) */
  404. /* We always copy to/from spefscr */
  405. if (__put_user(current->thread.spefscr, (u32 __user *)&frame->mc_vregs + ELF_NEVRREG))
  406. return 1;
  407. #endif /* CONFIG_SPE */
  408. if (__put_user(msr, &frame->mc_gregs[PT_MSR]))
  409. return 1;
  410. /* We need to write 0 the MSR top 32 bits in the tm frame so that we
  411. * can check it on the restore to see if TM is active
  412. */
  413. if (tm_frame && __put_user(0, &tm_frame->mc_gregs[PT_MSR]))
  414. return 1;
  415. if (sigret) {
  416. /* Set up the sigreturn trampoline: li r0,sigret; sc */
  417. if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
  418. || __put_user(0x44000002UL, &frame->tramp[1]))
  419. return 1;
  420. flush_icache_range((unsigned long) &frame->tramp[0],
  421. (unsigned long) &frame->tramp[2]);
  422. }
  423. return 0;
  424. }
  425. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  426. /*
  427. * Save the current user registers on the user stack.
  428. * We only save the altivec/spe registers if the process has used
  429. * altivec/spe instructions at some point.
  430. * We also save the transactional registers to a second ucontext in the
  431. * frame.
  432. *
  433. * See save_user_regs() and signal_64.c:setup_tm_sigcontexts().
  434. */
  435. static int save_tm_user_regs(struct pt_regs *regs,
  436. struct mcontext __user *frame,
  437. struct mcontext __user *tm_frame, int sigret)
  438. {
  439. unsigned long msr = regs->msr;
  440. WARN_ON(tm_suspend_disabled);
  441. /* Remove TM bits from thread's MSR. The MSR in the sigcontext
  442. * just indicates to userland that we were doing a transaction, but we
  443. * don't want to return in transactional state. This also ensures
  444. * that flush_fp_to_thread won't set TIF_RESTORE_TM again.
  445. */
  446. regs->msr &= ~MSR_TS_MASK;
  447. /* Save both sets of general registers */
  448. if (save_general_regs(&current->thread.ckpt_regs, frame)
  449. || save_general_regs(regs, tm_frame))
  450. return 1;
  451. /* Stash the top half of the 64bit MSR into the 32bit MSR word
  452. * of the transactional mcontext. This way we have a backward-compatible
  453. * MSR in the 'normal' (checkpointed) mcontext and additionally one can
  454. * also look at what type of transaction (T or S) was active at the
  455. * time of the signal.
  456. */
  457. if (__put_user((msr >> 32), &tm_frame->mc_gregs[PT_MSR]))
  458. return 1;
  459. #ifdef CONFIG_ALTIVEC
  460. /* save altivec registers */
  461. if (current->thread.used_vr) {
  462. if (__copy_to_user(&frame->mc_vregs, &current->thread.ckvr_state,
  463. ELF_NVRREG * sizeof(vector128)))
  464. return 1;
  465. if (msr & MSR_VEC) {
  466. if (__copy_to_user(&tm_frame->mc_vregs,
  467. &current->thread.vr_state,
  468. ELF_NVRREG * sizeof(vector128)))
  469. return 1;
  470. } else {
  471. if (__copy_to_user(&tm_frame->mc_vregs,
  472. &current->thread.ckvr_state,
  473. ELF_NVRREG * sizeof(vector128)))
  474. return 1;
  475. }
  476. /* set MSR_VEC in the saved MSR value to indicate that
  477. * frame->mc_vregs contains valid data
  478. */
  479. msr |= MSR_VEC;
  480. }
  481. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  482. * use altivec. Since VSCR only contains 32 bits saved in the least
  483. * significant bits of a vector, we "cheat" and stuff VRSAVE in the
  484. * most significant bits of that same vector. --BenH
  485. */
  486. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  487. current->thread.ckvrsave = mfspr(SPRN_VRSAVE);
  488. if (__put_user(current->thread.ckvrsave,
  489. (u32 __user *)&frame->mc_vregs[32]))
  490. return 1;
  491. if (msr & MSR_VEC) {
  492. if (__put_user(current->thread.vrsave,
  493. (u32 __user *)&tm_frame->mc_vregs[32]))
  494. return 1;
  495. } else {
  496. if (__put_user(current->thread.ckvrsave,
  497. (u32 __user *)&tm_frame->mc_vregs[32]))
  498. return 1;
  499. }
  500. #endif /* CONFIG_ALTIVEC */
  501. if (copy_ckfpr_to_user(&frame->mc_fregs, current))
  502. return 1;
  503. if (msr & MSR_FP) {
  504. if (copy_fpr_to_user(&tm_frame->mc_fregs, current))
  505. return 1;
  506. } else {
  507. if (copy_ckfpr_to_user(&tm_frame->mc_fregs, current))
  508. return 1;
  509. }
  510. #ifdef CONFIG_VSX
  511. /*
  512. * Copy VSR 0-31 upper half from thread_struct to local
  513. * buffer, then write that to userspace. Also set MSR_VSX in
  514. * the saved MSR value to indicate that frame->mc_vregs
  515. * contains valid data
  516. */
  517. if (current->thread.used_vsr) {
  518. if (copy_ckvsx_to_user(&frame->mc_vsregs, current))
  519. return 1;
  520. if (msr & MSR_VSX) {
  521. if (copy_vsx_to_user(&tm_frame->mc_vsregs,
  522. current))
  523. return 1;
  524. } else {
  525. if (copy_ckvsx_to_user(&tm_frame->mc_vsregs, current))
  526. return 1;
  527. }
  528. msr |= MSR_VSX;
  529. }
  530. #endif /* CONFIG_VSX */
  531. #ifdef CONFIG_SPE
  532. /* SPE regs are not checkpointed with TM, so this section is
  533. * simply the same as in save_user_regs().
  534. */
  535. if (current->thread.used_spe) {
  536. flush_spe_to_thread(current);
  537. if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
  538. ELF_NEVRREG * sizeof(u32)))
  539. return 1;
  540. /* set MSR_SPE in the saved MSR value to indicate that
  541. * frame->mc_vregs contains valid data */
  542. msr |= MSR_SPE;
  543. }
  544. /* We always copy to/from spefscr */
  545. if (__put_user(current->thread.spefscr, (u32 __user *)&frame->mc_vregs + ELF_NEVRREG))
  546. return 1;
  547. #endif /* CONFIG_SPE */
  548. if (__put_user(msr, &frame->mc_gregs[PT_MSR]))
  549. return 1;
  550. if (sigret) {
  551. /* Set up the sigreturn trampoline: li r0,sigret; sc */
  552. if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
  553. || __put_user(0x44000002UL, &frame->tramp[1]))
  554. return 1;
  555. flush_icache_range((unsigned long) &frame->tramp[0],
  556. (unsigned long) &frame->tramp[2]);
  557. }
  558. return 0;
  559. }
  560. #endif
  561. /*
  562. * Restore the current user register values from the user stack,
  563. * (except for MSR).
  564. */
  565. static long restore_user_regs(struct pt_regs *regs,
  566. struct mcontext __user *sr, int sig)
  567. {
  568. long err;
  569. unsigned int save_r2 = 0;
  570. unsigned long msr;
  571. #ifdef CONFIG_VSX
  572. int i;
  573. #endif
  574. /*
  575. * restore general registers but not including MSR or SOFTE. Also
  576. * take care of keeping r2 (TLS) intact if not a signal
  577. */
  578. if (!sig)
  579. save_r2 = (unsigned int)regs->gpr[2];
  580. err = restore_general_regs(regs, sr);
  581. regs->trap = 0;
  582. err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
  583. if (!sig)
  584. regs->gpr[2] = (unsigned long) save_r2;
  585. if (err)
  586. return 1;
  587. /* if doing signal return, restore the previous little-endian mode */
  588. if (sig)
  589. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  590. #ifdef CONFIG_ALTIVEC
  591. /*
  592. * Force the process to reload the altivec registers from
  593. * current->thread when it next does altivec instructions
  594. */
  595. regs->msr &= ~MSR_VEC;
  596. if (msr & MSR_VEC) {
  597. /* restore altivec registers from the stack */
  598. if (__copy_from_user(&current->thread.vr_state, &sr->mc_vregs,
  599. sizeof(sr->mc_vregs)))
  600. return 1;
  601. current->thread.used_vr = true;
  602. } else if (current->thread.used_vr)
  603. memset(&current->thread.vr_state, 0,
  604. ELF_NVRREG * sizeof(vector128));
  605. /* Always get VRSAVE back */
  606. if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
  607. return 1;
  608. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  609. mtspr(SPRN_VRSAVE, current->thread.vrsave);
  610. #endif /* CONFIG_ALTIVEC */
  611. if (copy_fpr_from_user(current, &sr->mc_fregs))
  612. return 1;
  613. #ifdef CONFIG_VSX
  614. /*
  615. * Force the process to reload the VSX registers from
  616. * current->thread when it next does VSX instruction.
  617. */
  618. regs->msr &= ~MSR_VSX;
  619. if (msr & MSR_VSX) {
  620. /*
  621. * Restore altivec registers from the stack to a local
  622. * buffer, then write this out to the thread_struct
  623. */
  624. if (copy_vsx_from_user(current, &sr->mc_vsregs))
  625. return 1;
  626. current->thread.used_vsr = true;
  627. } else if (current->thread.used_vsr)
  628. for (i = 0; i < 32 ; i++)
  629. current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  630. #endif /* CONFIG_VSX */
  631. /*
  632. * force the process to reload the FP registers from
  633. * current->thread when it next does FP instructions
  634. */
  635. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  636. #ifdef CONFIG_SPE
  637. /* force the process to reload the spe registers from
  638. current->thread when it next does spe instructions */
  639. regs->msr &= ~MSR_SPE;
  640. if (msr & MSR_SPE) {
  641. /* restore spe registers from the stack */
  642. if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
  643. ELF_NEVRREG * sizeof(u32)))
  644. return 1;
  645. current->thread.used_spe = true;
  646. } else if (current->thread.used_spe)
  647. memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
  648. /* Always get SPEFSCR back */
  649. if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs + ELF_NEVRREG))
  650. return 1;
  651. #endif /* CONFIG_SPE */
  652. return 0;
  653. }
  654. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  655. /*
  656. * Restore the current user register values from the user stack, except for
  657. * MSR, and recheckpoint the original checkpointed register state for processes
  658. * in transactions.
  659. */
  660. static long restore_tm_user_regs(struct pt_regs *regs,
  661. struct mcontext __user *sr,
  662. struct mcontext __user *tm_sr)
  663. {
  664. long err;
  665. unsigned long msr, msr_hi;
  666. #ifdef CONFIG_VSX
  667. int i;
  668. #endif
  669. if (tm_suspend_disabled)
  670. return 1;
  671. /*
  672. * restore general registers but not including MSR or SOFTE. Also
  673. * take care of keeping r2 (TLS) intact if not a signal.
  674. * See comment in signal_64.c:restore_tm_sigcontexts();
  675. * TFHAR is restored from the checkpointed NIP; TEXASR and TFIAR
  676. * were set by the signal delivery.
  677. */
  678. err = restore_general_regs(regs, tm_sr);
  679. err |= restore_general_regs(&current->thread.ckpt_regs, sr);
  680. err |= __get_user(current->thread.tm_tfhar, &sr->mc_gregs[PT_NIP]);
  681. err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
  682. if (err)
  683. return 1;
  684. /* Restore the previous little-endian mode */
  685. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  686. #ifdef CONFIG_ALTIVEC
  687. regs->msr &= ~MSR_VEC;
  688. if (msr & MSR_VEC) {
  689. /* restore altivec registers from the stack */
  690. if (__copy_from_user(&current->thread.ckvr_state, &sr->mc_vregs,
  691. sizeof(sr->mc_vregs)) ||
  692. __copy_from_user(&current->thread.vr_state,
  693. &tm_sr->mc_vregs,
  694. sizeof(sr->mc_vregs)))
  695. return 1;
  696. current->thread.used_vr = true;
  697. } else if (current->thread.used_vr) {
  698. memset(&current->thread.vr_state, 0,
  699. ELF_NVRREG * sizeof(vector128));
  700. memset(&current->thread.ckvr_state, 0,
  701. ELF_NVRREG * sizeof(vector128));
  702. }
  703. /* Always get VRSAVE back */
  704. if (__get_user(current->thread.ckvrsave,
  705. (u32 __user *)&sr->mc_vregs[32]) ||
  706. __get_user(current->thread.vrsave,
  707. (u32 __user *)&tm_sr->mc_vregs[32]))
  708. return 1;
  709. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  710. mtspr(SPRN_VRSAVE, current->thread.ckvrsave);
  711. #endif /* CONFIG_ALTIVEC */
  712. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  713. if (copy_fpr_from_user(current, &sr->mc_fregs) ||
  714. copy_ckfpr_from_user(current, &tm_sr->mc_fregs))
  715. return 1;
  716. #ifdef CONFIG_VSX
  717. regs->msr &= ~MSR_VSX;
  718. if (msr & MSR_VSX) {
  719. /*
  720. * Restore altivec registers from the stack to a local
  721. * buffer, then write this out to the thread_struct
  722. */
  723. if (copy_vsx_from_user(current, &tm_sr->mc_vsregs) ||
  724. copy_ckvsx_from_user(current, &sr->mc_vsregs))
  725. return 1;
  726. current->thread.used_vsr = true;
  727. } else if (current->thread.used_vsr)
  728. for (i = 0; i < 32 ; i++) {
  729. current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  730. current->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  731. }
  732. #endif /* CONFIG_VSX */
  733. #ifdef CONFIG_SPE
  734. /* SPE regs are not checkpointed with TM, so this section is
  735. * simply the same as in restore_user_regs().
  736. */
  737. regs->msr &= ~MSR_SPE;
  738. if (msr & MSR_SPE) {
  739. if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
  740. ELF_NEVRREG * sizeof(u32)))
  741. return 1;
  742. current->thread.used_spe = true;
  743. } else if (current->thread.used_spe)
  744. memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
  745. /* Always get SPEFSCR back */
  746. if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs
  747. + ELF_NEVRREG))
  748. return 1;
  749. #endif /* CONFIG_SPE */
  750. /* Get the top half of the MSR from the user context */
  751. if (__get_user(msr_hi, &tm_sr->mc_gregs[PT_MSR]))
  752. return 1;
  753. msr_hi <<= 32;
  754. /* If TM bits are set to the reserved value, it's an invalid context */
  755. if (MSR_TM_RESV(msr_hi))
  756. return 1;
  757. /* Pull in the MSR TM bits from the user context */
  758. regs->msr = (regs->msr & ~MSR_TS_MASK) | (msr_hi & MSR_TS_MASK);
  759. /* Now, recheckpoint. This loads up all of the checkpointed (older)
  760. * registers, including FP and V[S]Rs. After recheckpointing, the
  761. * transactional versions should be loaded.
  762. */
  763. tm_enable();
  764. /* Make sure the transaction is marked as failed */
  765. current->thread.tm_texasr |= TEXASR_FS;
  766. /* This loads the checkpointed FP/VEC state, if used */
  767. tm_recheckpoint(&current->thread);
  768. /* This loads the speculative FP/VEC state, if used */
  769. msr_check_and_set(msr & (MSR_FP | MSR_VEC));
  770. if (msr & MSR_FP) {
  771. load_fp_state(&current->thread.fp_state);
  772. regs->msr |= (MSR_FP | current->thread.fpexc_mode);
  773. }
  774. #ifdef CONFIG_ALTIVEC
  775. if (msr & MSR_VEC) {
  776. load_vr_state(&current->thread.vr_state);
  777. regs->msr |= MSR_VEC;
  778. }
  779. #endif
  780. return 0;
  781. }
  782. #endif
  783. #ifdef CONFIG_PPC64
  784. #define copy_siginfo_to_user copy_siginfo_to_user32
  785. #endif /* CONFIG_PPC64 */
  786. /*
  787. * Set up a signal frame for a "real-time" signal handler
  788. * (one which gets siginfo).
  789. */
  790. int handle_rt_signal32(struct ksignal *ksig, sigset_t *oldset,
  791. struct task_struct *tsk)
  792. {
  793. struct rt_sigframe __user *rt_sf;
  794. struct mcontext __user *frame;
  795. struct mcontext __user *tm_frame = NULL;
  796. void __user *addr;
  797. unsigned long newsp = 0;
  798. int sigret;
  799. unsigned long tramp;
  800. struct pt_regs *regs = tsk->thread.regs;
  801. BUG_ON(tsk != current);
  802. /* Set up Signal Frame */
  803. /* Put a Real Time Context onto stack */
  804. rt_sf = get_sigframe(ksig, get_tm_stackpointer(tsk), sizeof(*rt_sf), 1);
  805. addr = rt_sf;
  806. if (unlikely(rt_sf == NULL))
  807. goto badframe;
  808. /* Put the siginfo & fill in most of the ucontext */
  809. if (copy_siginfo_to_user(&rt_sf->info, &ksig->info)
  810. || __put_user(0, &rt_sf->uc.uc_flags)
  811. || __save_altstack(&rt_sf->uc.uc_stack, regs->gpr[1])
  812. || __put_user(to_user_ptr(&rt_sf->uc.uc_mcontext),
  813. &rt_sf->uc.uc_regs)
  814. || put_sigset_t(&rt_sf->uc.uc_sigmask, oldset))
  815. goto badframe;
  816. /* Save user registers on the stack */
  817. frame = &rt_sf->uc.uc_mcontext;
  818. addr = frame;
  819. if (vdso32_rt_sigtramp && tsk->mm->context.vdso_base) {
  820. sigret = 0;
  821. tramp = tsk->mm->context.vdso_base + vdso32_rt_sigtramp;
  822. } else {
  823. sigret = __NR_rt_sigreturn;
  824. tramp = (unsigned long) frame->tramp;
  825. }
  826. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  827. tm_frame = &rt_sf->uc_transact.uc_mcontext;
  828. if (MSR_TM_ACTIVE(regs->msr)) {
  829. if (__put_user((unsigned long)&rt_sf->uc_transact,
  830. &rt_sf->uc.uc_link) ||
  831. __put_user((unsigned long)tm_frame,
  832. &rt_sf->uc_transact.uc_regs))
  833. goto badframe;
  834. if (save_tm_user_regs(regs, frame, tm_frame, sigret))
  835. goto badframe;
  836. }
  837. else
  838. #endif
  839. {
  840. if (__put_user(0, &rt_sf->uc.uc_link))
  841. goto badframe;
  842. if (save_user_regs(regs, frame, tm_frame, sigret, 1))
  843. goto badframe;
  844. }
  845. regs->link = tramp;
  846. tsk->thread.fp_state.fpscr = 0; /* turn off all fp exceptions */
  847. /* create a stack frame for the caller of the handler */
  848. newsp = ((unsigned long)rt_sf) - (__SIGNAL_FRAMESIZE + 16);
  849. addr = (void __user *)regs->gpr[1];
  850. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  851. goto badframe;
  852. /* Fill registers for signal handler */
  853. regs->gpr[1] = newsp;
  854. regs->gpr[3] = ksig->sig;
  855. regs->gpr[4] = (unsigned long) &rt_sf->info;
  856. regs->gpr[5] = (unsigned long) &rt_sf->uc;
  857. regs->gpr[6] = (unsigned long) rt_sf;
  858. regs->nip = (unsigned long) ksig->ka.sa.sa_handler;
  859. /* enter the signal handler in native-endian mode */
  860. regs->msr &= ~MSR_LE;
  861. regs->msr |= (MSR_KERNEL & MSR_LE);
  862. return 0;
  863. badframe:
  864. if (show_unhandled_signals)
  865. printk_ratelimited(KERN_INFO
  866. "%s[%d]: bad frame in handle_rt_signal32: "
  867. "%p nip %08lx lr %08lx\n",
  868. tsk->comm, tsk->pid,
  869. addr, regs->nip, regs->link);
  870. return 1;
  871. }
  872. static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
  873. {
  874. sigset_t set;
  875. struct mcontext __user *mcp;
  876. if (get_sigset_t(&set, &ucp->uc_sigmask))
  877. return -EFAULT;
  878. #ifdef CONFIG_PPC64
  879. {
  880. u32 cmcp;
  881. if (__get_user(cmcp, &ucp->uc_regs))
  882. return -EFAULT;
  883. mcp = (struct mcontext __user *)(u64)cmcp;
  884. /* no need to check access_ok(mcp), since mcp < 4GB */
  885. }
  886. #else
  887. if (__get_user(mcp, &ucp->uc_regs))
  888. return -EFAULT;
  889. if (!access_ok(VERIFY_READ, mcp, sizeof(*mcp)))
  890. return -EFAULT;
  891. #endif
  892. set_current_blocked(&set);
  893. if (restore_user_regs(regs, mcp, sig))
  894. return -EFAULT;
  895. return 0;
  896. }
  897. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  898. static int do_setcontext_tm(struct ucontext __user *ucp,
  899. struct ucontext __user *tm_ucp,
  900. struct pt_regs *regs)
  901. {
  902. sigset_t set;
  903. struct mcontext __user *mcp;
  904. struct mcontext __user *tm_mcp;
  905. u32 cmcp;
  906. u32 tm_cmcp;
  907. if (get_sigset_t(&set, &ucp->uc_sigmask))
  908. return -EFAULT;
  909. if (__get_user(cmcp, &ucp->uc_regs) ||
  910. __get_user(tm_cmcp, &tm_ucp->uc_regs))
  911. return -EFAULT;
  912. mcp = (struct mcontext __user *)(u64)cmcp;
  913. tm_mcp = (struct mcontext __user *)(u64)tm_cmcp;
  914. /* no need to check access_ok(mcp), since mcp < 4GB */
  915. set_current_blocked(&set);
  916. if (restore_tm_user_regs(regs, mcp, tm_mcp))
  917. return -EFAULT;
  918. return 0;
  919. }
  920. #endif
  921. #ifdef CONFIG_PPC64
  922. COMPAT_SYSCALL_DEFINE3(swapcontext, struct ucontext __user *, old_ctx,
  923. struct ucontext __user *, new_ctx, int, ctx_size)
  924. #else
  925. SYSCALL_DEFINE3(swapcontext, struct ucontext __user *, old_ctx,
  926. struct ucontext __user *, new_ctx, long, ctx_size)
  927. #endif
  928. {
  929. struct pt_regs *regs = current_pt_regs();
  930. int ctx_has_vsx_region = 0;
  931. #ifdef CONFIG_PPC64
  932. unsigned long new_msr = 0;
  933. if (new_ctx) {
  934. struct mcontext __user *mcp;
  935. u32 cmcp;
  936. /*
  937. * Get pointer to the real mcontext. No need for
  938. * access_ok since we are dealing with compat
  939. * pointers.
  940. */
  941. if (__get_user(cmcp, &new_ctx->uc_regs))
  942. return -EFAULT;
  943. mcp = (struct mcontext __user *)(u64)cmcp;
  944. if (__get_user(new_msr, &mcp->mc_gregs[PT_MSR]))
  945. return -EFAULT;
  946. }
  947. /*
  948. * Check that the context is not smaller than the original
  949. * size (with VMX but without VSX)
  950. */
  951. if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
  952. return -EINVAL;
  953. /*
  954. * If the new context state sets the MSR VSX bits but
  955. * it doesn't provide VSX state.
  956. */
  957. if ((ctx_size < sizeof(struct ucontext)) &&
  958. (new_msr & MSR_VSX))
  959. return -EINVAL;
  960. /* Does the context have enough room to store VSX data? */
  961. if (ctx_size >= sizeof(struct ucontext))
  962. ctx_has_vsx_region = 1;
  963. #else
  964. /* Context size is for future use. Right now, we only make sure
  965. * we are passed something we understand
  966. */
  967. if (ctx_size < sizeof(struct ucontext))
  968. return -EINVAL;
  969. #endif
  970. if (old_ctx != NULL) {
  971. struct mcontext __user *mctx;
  972. /*
  973. * old_ctx might not be 16-byte aligned, in which
  974. * case old_ctx->uc_mcontext won't be either.
  975. * Because we have the old_ctx->uc_pad2 field
  976. * before old_ctx->uc_mcontext, we need to round down
  977. * from &old_ctx->uc_mcontext to a 16-byte boundary.
  978. */
  979. mctx = (struct mcontext __user *)
  980. ((unsigned long) &old_ctx->uc_mcontext & ~0xfUL);
  981. if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
  982. || save_user_regs(regs, mctx, NULL, 0, ctx_has_vsx_region)
  983. || put_sigset_t(&old_ctx->uc_sigmask, &current->blocked)
  984. || __put_user(to_user_ptr(mctx), &old_ctx->uc_regs))
  985. return -EFAULT;
  986. }
  987. if (new_ctx == NULL)
  988. return 0;
  989. if (!access_ok(VERIFY_READ, new_ctx, ctx_size) ||
  990. fault_in_pages_readable((u8 __user *)new_ctx, ctx_size))
  991. return -EFAULT;
  992. /*
  993. * If we get a fault copying the context into the kernel's
  994. * image of the user's registers, we can't just return -EFAULT
  995. * because the user's registers will be corrupted. For instance
  996. * the NIP value may have been updated but not some of the
  997. * other registers. Given that we have done the access_ok
  998. * and successfully read the first and last bytes of the region
  999. * above, this should only happen in an out-of-memory situation
  1000. * or if another thread unmaps the region containing the context.
  1001. * We kill the task with a SIGSEGV in this situation.
  1002. */
  1003. if (do_setcontext(new_ctx, regs, 0))
  1004. do_exit(SIGSEGV);
  1005. set_thread_flag(TIF_RESTOREALL);
  1006. return 0;
  1007. }
  1008. #ifdef CONFIG_PPC64
  1009. COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
  1010. #else
  1011. SYSCALL_DEFINE0(rt_sigreturn)
  1012. #endif
  1013. {
  1014. struct rt_sigframe __user *rt_sf;
  1015. struct pt_regs *regs = current_pt_regs();
  1016. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1017. struct ucontext __user *uc_transact;
  1018. unsigned long msr_hi;
  1019. unsigned long tmp;
  1020. int tm_restore = 0;
  1021. #endif
  1022. /* Always make any pending restarted system calls return -EINTR */
  1023. current->restart_block.fn = do_no_restart_syscall;
  1024. rt_sf = (struct rt_sigframe __user *)
  1025. (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
  1026. if (!access_ok(VERIFY_READ, rt_sf, sizeof(*rt_sf)))
  1027. goto bad;
  1028. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1029. /*
  1030. * If there is a transactional state then throw it away.
  1031. * The purpose of a sigreturn is to destroy all traces of the
  1032. * signal frame, this includes any transactional state created
  1033. * within in. We only check for suspended as we can never be
  1034. * active in the kernel, we are active, there is nothing better to
  1035. * do than go ahead and Bad Thing later.
  1036. * The cause is not important as there will never be a
  1037. * recheckpoint so it's not user visible.
  1038. */
  1039. if (MSR_TM_SUSPENDED(mfmsr()))
  1040. tm_reclaim_current(0);
  1041. if (__get_user(tmp, &rt_sf->uc.uc_link))
  1042. goto bad;
  1043. uc_transact = (struct ucontext __user *)(uintptr_t)tmp;
  1044. if (uc_transact) {
  1045. u32 cmcp;
  1046. struct mcontext __user *mcp;
  1047. if (__get_user(cmcp, &uc_transact->uc_regs))
  1048. return -EFAULT;
  1049. mcp = (struct mcontext __user *)(u64)cmcp;
  1050. /* The top 32 bits of the MSR are stashed in the transactional
  1051. * ucontext. */
  1052. if (__get_user(msr_hi, &mcp->mc_gregs[PT_MSR]))
  1053. goto bad;
  1054. if (MSR_TM_ACTIVE(msr_hi<<32)) {
  1055. /* We only recheckpoint on return if we're
  1056. * transaction.
  1057. */
  1058. tm_restore = 1;
  1059. if (do_setcontext_tm(&rt_sf->uc, uc_transact, regs))
  1060. goto bad;
  1061. }
  1062. }
  1063. if (!tm_restore)
  1064. /* Fall through, for non-TM restore */
  1065. #endif
  1066. if (do_setcontext(&rt_sf->uc, regs, 1))
  1067. goto bad;
  1068. /*
  1069. * It's not clear whether or why it is desirable to save the
  1070. * sigaltstack setting on signal delivery and restore it on
  1071. * signal return. But other architectures do this and we have
  1072. * always done it up until now so it is probably better not to
  1073. * change it. -- paulus
  1074. */
  1075. #ifdef CONFIG_PPC64
  1076. if (compat_restore_altstack(&rt_sf->uc.uc_stack))
  1077. goto bad;
  1078. #else
  1079. if (restore_altstack(&rt_sf->uc.uc_stack))
  1080. goto bad;
  1081. #endif
  1082. set_thread_flag(TIF_RESTOREALL);
  1083. return 0;
  1084. bad:
  1085. if (show_unhandled_signals)
  1086. printk_ratelimited(KERN_INFO
  1087. "%s[%d]: bad frame in sys_rt_sigreturn: "
  1088. "%p nip %08lx lr %08lx\n",
  1089. current->comm, current->pid,
  1090. rt_sf, regs->nip, regs->link);
  1091. force_sig(SIGSEGV, current);
  1092. return 0;
  1093. }
  1094. #ifdef CONFIG_PPC32
  1095. SYSCALL_DEFINE3(debug_setcontext, struct ucontext __user *, ctx,
  1096. int, ndbg, struct sig_dbg_op __user *, dbg)
  1097. {
  1098. struct pt_regs *regs = current_pt_regs();
  1099. struct sig_dbg_op op;
  1100. int i;
  1101. unsigned long new_msr = regs->msr;
  1102. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1103. unsigned long new_dbcr0 = current->thread.debug.dbcr0;
  1104. #endif
  1105. for (i=0; i<ndbg; i++) {
  1106. if (copy_from_user(&op, dbg + i, sizeof(op)))
  1107. return -EFAULT;
  1108. switch (op.dbg_type) {
  1109. case SIG_DBG_SINGLE_STEPPING:
  1110. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1111. if (op.dbg_value) {
  1112. new_msr |= MSR_DE;
  1113. new_dbcr0 |= (DBCR0_IDM | DBCR0_IC);
  1114. } else {
  1115. new_dbcr0 &= ~DBCR0_IC;
  1116. if (!DBCR_ACTIVE_EVENTS(new_dbcr0,
  1117. current->thread.debug.dbcr1)) {
  1118. new_msr &= ~MSR_DE;
  1119. new_dbcr0 &= ~DBCR0_IDM;
  1120. }
  1121. }
  1122. #else
  1123. if (op.dbg_value)
  1124. new_msr |= MSR_SE;
  1125. else
  1126. new_msr &= ~MSR_SE;
  1127. #endif
  1128. break;
  1129. case SIG_DBG_BRANCH_TRACING:
  1130. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1131. return -EINVAL;
  1132. #else
  1133. if (op.dbg_value)
  1134. new_msr |= MSR_BE;
  1135. else
  1136. new_msr &= ~MSR_BE;
  1137. #endif
  1138. break;
  1139. default:
  1140. return -EINVAL;
  1141. }
  1142. }
  1143. /* We wait until here to actually install the values in the
  1144. registers so if we fail in the above loop, it will not
  1145. affect the contents of these registers. After this point,
  1146. failure is a problem, anyway, and it's very unlikely unless
  1147. the user is really doing something wrong. */
  1148. regs->msr = new_msr;
  1149. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1150. current->thread.debug.dbcr0 = new_dbcr0;
  1151. #endif
  1152. if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)) ||
  1153. fault_in_pages_readable((u8 __user *)ctx, sizeof(*ctx)))
  1154. return -EFAULT;
  1155. /*
  1156. * If we get a fault copying the context into the kernel's
  1157. * image of the user's registers, we can't just return -EFAULT
  1158. * because the user's registers will be corrupted. For instance
  1159. * the NIP value may have been updated but not some of the
  1160. * other registers. Given that we have done the access_ok
  1161. * and successfully read the first and last bytes of the region
  1162. * above, this should only happen in an out-of-memory situation
  1163. * or if another thread unmaps the region containing the context.
  1164. * We kill the task with a SIGSEGV in this situation.
  1165. */
  1166. if (do_setcontext(ctx, regs, 1)) {
  1167. if (show_unhandled_signals)
  1168. printk_ratelimited(KERN_INFO "%s[%d]: bad frame in "
  1169. "sys_debug_setcontext: %p nip %08lx "
  1170. "lr %08lx\n",
  1171. current->comm, current->pid,
  1172. ctx, regs->nip, regs->link);
  1173. force_sig(SIGSEGV, current);
  1174. goto out;
  1175. }
  1176. /*
  1177. * It's not clear whether or why it is desirable to save the
  1178. * sigaltstack setting on signal delivery and restore it on
  1179. * signal return. But other architectures do this and we have
  1180. * always done it up until now so it is probably better not to
  1181. * change it. -- paulus
  1182. */
  1183. restore_altstack(&ctx->uc_stack);
  1184. set_thread_flag(TIF_RESTOREALL);
  1185. out:
  1186. return 0;
  1187. }
  1188. #endif
  1189. /*
  1190. * OK, we're invoking a handler
  1191. */
  1192. int handle_signal32(struct ksignal *ksig, sigset_t *oldset,
  1193. struct task_struct *tsk)
  1194. {
  1195. struct sigcontext __user *sc;
  1196. struct sigframe __user *frame;
  1197. struct mcontext __user *tm_mctx = NULL;
  1198. unsigned long newsp = 0;
  1199. int sigret;
  1200. unsigned long tramp;
  1201. struct pt_regs *regs = tsk->thread.regs;
  1202. BUG_ON(tsk != current);
  1203. /* Set up Signal Frame */
  1204. frame = get_sigframe(ksig, get_tm_stackpointer(tsk), sizeof(*frame), 1);
  1205. if (unlikely(frame == NULL))
  1206. goto badframe;
  1207. sc = (struct sigcontext __user *) &frame->sctx;
  1208. #if _NSIG != 64
  1209. #error "Please adjust handle_signal()"
  1210. #endif
  1211. if (__put_user(to_user_ptr(ksig->ka.sa.sa_handler), &sc->handler)
  1212. || __put_user(oldset->sig[0], &sc->oldmask)
  1213. #ifdef CONFIG_PPC64
  1214. || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
  1215. #else
  1216. || __put_user(oldset->sig[1], &sc->_unused[3])
  1217. #endif
  1218. || __put_user(to_user_ptr(&frame->mctx), &sc->regs)
  1219. || __put_user(ksig->sig, &sc->signal))
  1220. goto badframe;
  1221. if (vdso32_sigtramp && tsk->mm->context.vdso_base) {
  1222. sigret = 0;
  1223. tramp = tsk->mm->context.vdso_base + vdso32_sigtramp;
  1224. } else {
  1225. sigret = __NR_sigreturn;
  1226. tramp = (unsigned long) frame->mctx.tramp;
  1227. }
  1228. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1229. tm_mctx = &frame->mctx_transact;
  1230. if (MSR_TM_ACTIVE(regs->msr)) {
  1231. if (save_tm_user_regs(regs, &frame->mctx, &frame->mctx_transact,
  1232. sigret))
  1233. goto badframe;
  1234. }
  1235. else
  1236. #endif
  1237. {
  1238. if (save_user_regs(regs, &frame->mctx, tm_mctx, sigret, 1))
  1239. goto badframe;
  1240. }
  1241. regs->link = tramp;
  1242. tsk->thread.fp_state.fpscr = 0; /* turn off all fp exceptions */
  1243. /* create a stack frame for the caller of the handler */
  1244. newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
  1245. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  1246. goto badframe;
  1247. regs->gpr[1] = newsp;
  1248. regs->gpr[3] = ksig->sig;
  1249. regs->gpr[4] = (unsigned long) sc;
  1250. regs->nip = (unsigned long) (unsigned long)ksig->ka.sa.sa_handler;
  1251. /* enter the signal handler in big-endian mode */
  1252. regs->msr &= ~MSR_LE;
  1253. return 0;
  1254. badframe:
  1255. if (show_unhandled_signals)
  1256. printk_ratelimited(KERN_INFO
  1257. "%s[%d]: bad frame in handle_signal32: "
  1258. "%p nip %08lx lr %08lx\n",
  1259. tsk->comm, tsk->pid,
  1260. frame, regs->nip, regs->link);
  1261. return 1;
  1262. }
  1263. /*
  1264. * Do a signal return; undo the signal stack.
  1265. */
  1266. #ifdef CONFIG_PPC64
  1267. COMPAT_SYSCALL_DEFINE0(sigreturn)
  1268. #else
  1269. SYSCALL_DEFINE0(sigreturn)
  1270. #endif
  1271. {
  1272. struct pt_regs *regs = current_pt_regs();
  1273. struct sigframe __user *sf;
  1274. struct sigcontext __user *sc;
  1275. struct sigcontext sigctx;
  1276. struct mcontext __user *sr;
  1277. void __user *addr;
  1278. sigset_t set;
  1279. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1280. struct mcontext __user *mcp, *tm_mcp;
  1281. unsigned long msr_hi;
  1282. #endif
  1283. /* Always make any pending restarted system calls return -EINTR */
  1284. current->restart_block.fn = do_no_restart_syscall;
  1285. sf = (struct sigframe __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
  1286. sc = &sf->sctx;
  1287. addr = sc;
  1288. if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
  1289. goto badframe;
  1290. #ifdef CONFIG_PPC64
  1291. /*
  1292. * Note that PPC32 puts the upper 32 bits of the sigmask in the
  1293. * unused part of the signal stackframe
  1294. */
  1295. set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3]) << 32);
  1296. #else
  1297. set.sig[0] = sigctx.oldmask;
  1298. set.sig[1] = sigctx._unused[3];
  1299. #endif
  1300. set_current_blocked(&set);
  1301. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1302. mcp = (struct mcontext __user *)&sf->mctx;
  1303. tm_mcp = (struct mcontext __user *)&sf->mctx_transact;
  1304. if (__get_user(msr_hi, &tm_mcp->mc_gregs[PT_MSR]))
  1305. goto badframe;
  1306. if (MSR_TM_ACTIVE(msr_hi<<32)) {
  1307. if (!cpu_has_feature(CPU_FTR_TM))
  1308. goto badframe;
  1309. if (restore_tm_user_regs(regs, mcp, tm_mcp))
  1310. goto badframe;
  1311. } else
  1312. #endif
  1313. {
  1314. sr = (struct mcontext __user *)from_user_ptr(sigctx.regs);
  1315. addr = sr;
  1316. if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
  1317. || restore_user_regs(regs, sr, 1))
  1318. goto badframe;
  1319. }
  1320. set_thread_flag(TIF_RESTOREALL);
  1321. return 0;
  1322. badframe:
  1323. if (show_unhandled_signals)
  1324. printk_ratelimited(KERN_INFO
  1325. "%s[%d]: bad frame in sys_sigreturn: "
  1326. "%p nip %08lx lr %08lx\n",
  1327. current->comm, current->pid,
  1328. addr, regs->nip, regs->link);
  1329. force_sig(SIGSEGV, current);
  1330. return 0;
  1331. }