signal_32.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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. unsigned long msr)
  439. {
  440. WARN_ON(tm_suspend_disabled);
  441. /* Save both sets of general registers */
  442. if (save_general_regs(&current->thread.ckpt_regs, frame)
  443. || save_general_regs(regs, tm_frame))
  444. return 1;
  445. /* Stash the top half of the 64bit MSR into the 32bit MSR word
  446. * of the transactional mcontext. This way we have a backward-compatible
  447. * MSR in the 'normal' (checkpointed) mcontext and additionally one can
  448. * also look at what type of transaction (T or S) was active at the
  449. * time of the signal.
  450. */
  451. if (__put_user((msr >> 32), &tm_frame->mc_gregs[PT_MSR]))
  452. return 1;
  453. #ifdef CONFIG_ALTIVEC
  454. /* save altivec registers */
  455. if (current->thread.used_vr) {
  456. if (__copy_to_user(&frame->mc_vregs, &current->thread.ckvr_state,
  457. ELF_NVRREG * sizeof(vector128)))
  458. return 1;
  459. if (msr & MSR_VEC) {
  460. if (__copy_to_user(&tm_frame->mc_vregs,
  461. &current->thread.vr_state,
  462. ELF_NVRREG * sizeof(vector128)))
  463. return 1;
  464. } else {
  465. if (__copy_to_user(&tm_frame->mc_vregs,
  466. &current->thread.ckvr_state,
  467. ELF_NVRREG * sizeof(vector128)))
  468. return 1;
  469. }
  470. /* set MSR_VEC in the saved MSR value to indicate that
  471. * frame->mc_vregs contains valid data
  472. */
  473. msr |= MSR_VEC;
  474. }
  475. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  476. * use altivec. Since VSCR only contains 32 bits saved in the least
  477. * significant bits of a vector, we "cheat" and stuff VRSAVE in the
  478. * most significant bits of that same vector. --BenH
  479. */
  480. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  481. current->thread.ckvrsave = mfspr(SPRN_VRSAVE);
  482. if (__put_user(current->thread.ckvrsave,
  483. (u32 __user *)&frame->mc_vregs[32]))
  484. return 1;
  485. if (msr & MSR_VEC) {
  486. if (__put_user(current->thread.vrsave,
  487. (u32 __user *)&tm_frame->mc_vregs[32]))
  488. return 1;
  489. } else {
  490. if (__put_user(current->thread.ckvrsave,
  491. (u32 __user *)&tm_frame->mc_vregs[32]))
  492. return 1;
  493. }
  494. #endif /* CONFIG_ALTIVEC */
  495. if (copy_ckfpr_to_user(&frame->mc_fregs, current))
  496. return 1;
  497. if (msr & MSR_FP) {
  498. if (copy_fpr_to_user(&tm_frame->mc_fregs, current))
  499. return 1;
  500. } else {
  501. if (copy_ckfpr_to_user(&tm_frame->mc_fregs, current))
  502. return 1;
  503. }
  504. #ifdef CONFIG_VSX
  505. /*
  506. * Copy VSR 0-31 upper half from thread_struct to local
  507. * buffer, then write that to userspace. Also set MSR_VSX in
  508. * the saved MSR value to indicate that frame->mc_vregs
  509. * contains valid data
  510. */
  511. if (current->thread.used_vsr) {
  512. if (copy_ckvsx_to_user(&frame->mc_vsregs, current))
  513. return 1;
  514. if (msr & MSR_VSX) {
  515. if (copy_vsx_to_user(&tm_frame->mc_vsregs,
  516. current))
  517. return 1;
  518. } else {
  519. if (copy_ckvsx_to_user(&tm_frame->mc_vsregs, current))
  520. return 1;
  521. }
  522. msr |= MSR_VSX;
  523. }
  524. #endif /* CONFIG_VSX */
  525. #ifdef CONFIG_SPE
  526. /* SPE regs are not checkpointed with TM, so this section is
  527. * simply the same as in save_user_regs().
  528. */
  529. if (current->thread.used_spe) {
  530. flush_spe_to_thread(current);
  531. if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
  532. ELF_NEVRREG * sizeof(u32)))
  533. return 1;
  534. /* set MSR_SPE in the saved MSR value to indicate that
  535. * frame->mc_vregs contains valid data */
  536. msr |= MSR_SPE;
  537. }
  538. /* We always copy to/from spefscr */
  539. if (__put_user(current->thread.spefscr, (u32 __user *)&frame->mc_vregs + ELF_NEVRREG))
  540. return 1;
  541. #endif /* CONFIG_SPE */
  542. if (__put_user(msr, &frame->mc_gregs[PT_MSR]))
  543. return 1;
  544. if (sigret) {
  545. /* Set up the sigreturn trampoline: li r0,sigret; sc */
  546. if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
  547. || __put_user(0x44000002UL, &frame->tramp[1]))
  548. return 1;
  549. flush_icache_range((unsigned long) &frame->tramp[0],
  550. (unsigned long) &frame->tramp[2]);
  551. }
  552. return 0;
  553. }
  554. #endif
  555. /*
  556. * Restore the current user register values from the user stack,
  557. * (except for MSR).
  558. */
  559. static long restore_user_regs(struct pt_regs *regs,
  560. struct mcontext __user *sr, int sig)
  561. {
  562. long err;
  563. unsigned int save_r2 = 0;
  564. unsigned long msr;
  565. #ifdef CONFIG_VSX
  566. int i;
  567. #endif
  568. /*
  569. * restore general registers but not including MSR or SOFTE. Also
  570. * take care of keeping r2 (TLS) intact if not a signal
  571. */
  572. if (!sig)
  573. save_r2 = (unsigned int)regs->gpr[2];
  574. err = restore_general_regs(regs, sr);
  575. regs->trap = 0;
  576. err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
  577. if (!sig)
  578. regs->gpr[2] = (unsigned long) save_r2;
  579. if (err)
  580. return 1;
  581. /* if doing signal return, restore the previous little-endian mode */
  582. if (sig)
  583. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  584. #ifdef CONFIG_ALTIVEC
  585. /*
  586. * Force the process to reload the altivec registers from
  587. * current->thread when it next does altivec instructions
  588. */
  589. regs->msr &= ~MSR_VEC;
  590. if (msr & MSR_VEC) {
  591. /* restore altivec registers from the stack */
  592. if (__copy_from_user(&current->thread.vr_state, &sr->mc_vregs,
  593. sizeof(sr->mc_vregs)))
  594. return 1;
  595. current->thread.used_vr = true;
  596. } else if (current->thread.used_vr)
  597. memset(&current->thread.vr_state, 0,
  598. ELF_NVRREG * sizeof(vector128));
  599. /* Always get VRSAVE back */
  600. if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
  601. return 1;
  602. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  603. mtspr(SPRN_VRSAVE, current->thread.vrsave);
  604. #endif /* CONFIG_ALTIVEC */
  605. if (copy_fpr_from_user(current, &sr->mc_fregs))
  606. return 1;
  607. #ifdef CONFIG_VSX
  608. /*
  609. * Force the process to reload the VSX registers from
  610. * current->thread when it next does VSX instruction.
  611. */
  612. regs->msr &= ~MSR_VSX;
  613. if (msr & MSR_VSX) {
  614. /*
  615. * Restore altivec registers from the stack to a local
  616. * buffer, then write this out to the thread_struct
  617. */
  618. if (copy_vsx_from_user(current, &sr->mc_vsregs))
  619. return 1;
  620. current->thread.used_vsr = true;
  621. } else if (current->thread.used_vsr)
  622. for (i = 0; i < 32 ; i++)
  623. current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  624. #endif /* CONFIG_VSX */
  625. /*
  626. * force the process to reload the FP registers from
  627. * current->thread when it next does FP instructions
  628. */
  629. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  630. #ifdef CONFIG_SPE
  631. /* force the process to reload the spe registers from
  632. current->thread when it next does spe instructions */
  633. regs->msr &= ~MSR_SPE;
  634. if (msr & MSR_SPE) {
  635. /* restore spe registers from the stack */
  636. if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
  637. ELF_NEVRREG * sizeof(u32)))
  638. return 1;
  639. current->thread.used_spe = true;
  640. } else if (current->thread.used_spe)
  641. memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
  642. /* Always get SPEFSCR back */
  643. if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs + ELF_NEVRREG))
  644. return 1;
  645. #endif /* CONFIG_SPE */
  646. return 0;
  647. }
  648. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  649. /*
  650. * Restore the current user register values from the user stack, except for
  651. * MSR, and recheckpoint the original checkpointed register state for processes
  652. * in transactions.
  653. */
  654. static long restore_tm_user_regs(struct pt_regs *regs,
  655. struct mcontext __user *sr,
  656. struct mcontext __user *tm_sr)
  657. {
  658. long err;
  659. unsigned long msr, msr_hi;
  660. #ifdef CONFIG_VSX
  661. int i;
  662. #endif
  663. if (tm_suspend_disabled)
  664. return 1;
  665. /*
  666. * restore general registers but not including MSR or SOFTE. Also
  667. * take care of keeping r2 (TLS) intact if not a signal.
  668. * See comment in signal_64.c:restore_tm_sigcontexts();
  669. * TFHAR is restored from the checkpointed NIP; TEXASR and TFIAR
  670. * were set by the signal delivery.
  671. */
  672. err = restore_general_regs(regs, tm_sr);
  673. err |= restore_general_regs(&current->thread.ckpt_regs, sr);
  674. err |= __get_user(current->thread.tm_tfhar, &sr->mc_gregs[PT_NIP]);
  675. err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
  676. if (err)
  677. return 1;
  678. /* Restore the previous little-endian mode */
  679. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  680. #ifdef CONFIG_ALTIVEC
  681. regs->msr &= ~MSR_VEC;
  682. if (msr & MSR_VEC) {
  683. /* restore altivec registers from the stack */
  684. if (__copy_from_user(&current->thread.ckvr_state, &sr->mc_vregs,
  685. sizeof(sr->mc_vregs)) ||
  686. __copy_from_user(&current->thread.vr_state,
  687. &tm_sr->mc_vregs,
  688. sizeof(sr->mc_vregs)))
  689. return 1;
  690. current->thread.used_vr = true;
  691. } else if (current->thread.used_vr) {
  692. memset(&current->thread.vr_state, 0,
  693. ELF_NVRREG * sizeof(vector128));
  694. memset(&current->thread.ckvr_state, 0,
  695. ELF_NVRREG * sizeof(vector128));
  696. }
  697. /* Always get VRSAVE back */
  698. if (__get_user(current->thread.ckvrsave,
  699. (u32 __user *)&sr->mc_vregs[32]) ||
  700. __get_user(current->thread.vrsave,
  701. (u32 __user *)&tm_sr->mc_vregs[32]))
  702. return 1;
  703. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  704. mtspr(SPRN_VRSAVE, current->thread.ckvrsave);
  705. #endif /* CONFIG_ALTIVEC */
  706. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  707. if (copy_fpr_from_user(current, &sr->mc_fregs) ||
  708. copy_ckfpr_from_user(current, &tm_sr->mc_fregs))
  709. return 1;
  710. #ifdef CONFIG_VSX
  711. regs->msr &= ~MSR_VSX;
  712. if (msr & MSR_VSX) {
  713. /*
  714. * Restore altivec registers from the stack to a local
  715. * buffer, then write this out to the thread_struct
  716. */
  717. if (copy_vsx_from_user(current, &tm_sr->mc_vsregs) ||
  718. copy_ckvsx_from_user(current, &sr->mc_vsregs))
  719. return 1;
  720. current->thread.used_vsr = true;
  721. } else if (current->thread.used_vsr)
  722. for (i = 0; i < 32 ; i++) {
  723. current->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  724. current->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = 0;
  725. }
  726. #endif /* CONFIG_VSX */
  727. #ifdef CONFIG_SPE
  728. /* SPE regs are not checkpointed with TM, so this section is
  729. * simply the same as in restore_user_regs().
  730. */
  731. regs->msr &= ~MSR_SPE;
  732. if (msr & MSR_SPE) {
  733. if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
  734. ELF_NEVRREG * sizeof(u32)))
  735. return 1;
  736. current->thread.used_spe = true;
  737. } else if (current->thread.used_spe)
  738. memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
  739. /* Always get SPEFSCR back */
  740. if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs
  741. + ELF_NEVRREG))
  742. return 1;
  743. #endif /* CONFIG_SPE */
  744. /* Get the top half of the MSR from the user context */
  745. if (__get_user(msr_hi, &tm_sr->mc_gregs[PT_MSR]))
  746. return 1;
  747. msr_hi <<= 32;
  748. /* If TM bits are set to the reserved value, it's an invalid context */
  749. if (MSR_TM_RESV(msr_hi))
  750. return 1;
  751. /*
  752. * Disabling preemption, since it is unsafe to be preempted
  753. * with MSR[TS] set without recheckpointing.
  754. */
  755. preempt_disable();
  756. /*
  757. * CAUTION:
  758. * After regs->MSR[TS] being updated, make sure that get_user(),
  759. * put_user() or similar functions are *not* called. These
  760. * functions can generate page faults which will cause the process
  761. * to be de-scheduled with MSR[TS] set but without calling
  762. * tm_recheckpoint(). This can cause a bug.
  763. *
  764. * Pull in the MSR TM bits from the user context
  765. */
  766. regs->msr = (regs->msr & ~MSR_TS_MASK) | (msr_hi & MSR_TS_MASK);
  767. /* Now, recheckpoint. This loads up all of the checkpointed (older)
  768. * registers, including FP and V[S]Rs. After recheckpointing, the
  769. * transactional versions should be loaded.
  770. */
  771. tm_enable();
  772. /* Make sure the transaction is marked as failed */
  773. current->thread.tm_texasr |= TEXASR_FS;
  774. /* This loads the checkpointed FP/VEC state, if used */
  775. tm_recheckpoint(&current->thread);
  776. /* This loads the speculative FP/VEC state, if used */
  777. msr_check_and_set(msr & (MSR_FP | MSR_VEC));
  778. if (msr & MSR_FP) {
  779. load_fp_state(&current->thread.fp_state);
  780. regs->msr |= (MSR_FP | current->thread.fpexc_mode);
  781. }
  782. #ifdef CONFIG_ALTIVEC
  783. if (msr & MSR_VEC) {
  784. load_vr_state(&current->thread.vr_state);
  785. regs->msr |= MSR_VEC;
  786. }
  787. #endif
  788. preempt_enable();
  789. return 0;
  790. }
  791. #endif
  792. #ifdef CONFIG_PPC64
  793. #define copy_siginfo_to_user copy_siginfo_to_user32
  794. #endif /* CONFIG_PPC64 */
  795. /*
  796. * Set up a signal frame for a "real-time" signal handler
  797. * (one which gets siginfo).
  798. */
  799. int handle_rt_signal32(struct ksignal *ksig, sigset_t *oldset,
  800. struct task_struct *tsk)
  801. {
  802. struct rt_sigframe __user *rt_sf;
  803. struct mcontext __user *frame;
  804. struct mcontext __user *tm_frame = NULL;
  805. void __user *addr;
  806. unsigned long newsp = 0;
  807. int sigret;
  808. unsigned long tramp;
  809. struct pt_regs *regs = tsk->thread.regs;
  810. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  811. /* Save the thread's msr before get_tm_stackpointer() changes it */
  812. unsigned long msr = regs->msr;
  813. #endif
  814. BUG_ON(tsk != current);
  815. /* Set up Signal Frame */
  816. /* Put a Real Time Context onto stack */
  817. rt_sf = get_sigframe(ksig, get_tm_stackpointer(tsk), sizeof(*rt_sf), 1);
  818. addr = rt_sf;
  819. if (unlikely(rt_sf == NULL))
  820. goto badframe;
  821. /* Put the siginfo & fill in most of the ucontext */
  822. if (copy_siginfo_to_user(&rt_sf->info, &ksig->info)
  823. || __put_user(0, &rt_sf->uc.uc_flags)
  824. || __save_altstack(&rt_sf->uc.uc_stack, regs->gpr[1])
  825. || __put_user(to_user_ptr(&rt_sf->uc.uc_mcontext),
  826. &rt_sf->uc.uc_regs)
  827. || put_sigset_t(&rt_sf->uc.uc_sigmask, oldset))
  828. goto badframe;
  829. /* Save user registers on the stack */
  830. frame = &rt_sf->uc.uc_mcontext;
  831. addr = frame;
  832. if (vdso32_rt_sigtramp && tsk->mm->context.vdso_base) {
  833. sigret = 0;
  834. tramp = tsk->mm->context.vdso_base + vdso32_rt_sigtramp;
  835. } else {
  836. sigret = __NR_rt_sigreturn;
  837. tramp = (unsigned long) frame->tramp;
  838. }
  839. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  840. tm_frame = &rt_sf->uc_transact.uc_mcontext;
  841. if (MSR_TM_ACTIVE(msr)) {
  842. if (__put_user((unsigned long)&rt_sf->uc_transact,
  843. &rt_sf->uc.uc_link) ||
  844. __put_user((unsigned long)tm_frame,
  845. &rt_sf->uc_transact.uc_regs))
  846. goto badframe;
  847. if (save_tm_user_regs(regs, frame, tm_frame, sigret, msr))
  848. goto badframe;
  849. }
  850. else
  851. #endif
  852. {
  853. if (__put_user(0, &rt_sf->uc.uc_link))
  854. goto badframe;
  855. if (save_user_regs(regs, frame, tm_frame, sigret, 1))
  856. goto badframe;
  857. }
  858. regs->link = tramp;
  859. tsk->thread.fp_state.fpscr = 0; /* turn off all fp exceptions */
  860. /* create a stack frame for the caller of the handler */
  861. newsp = ((unsigned long)rt_sf) - (__SIGNAL_FRAMESIZE + 16);
  862. addr = (void __user *)regs->gpr[1];
  863. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  864. goto badframe;
  865. /* Fill registers for signal handler */
  866. regs->gpr[1] = newsp;
  867. regs->gpr[3] = ksig->sig;
  868. regs->gpr[4] = (unsigned long) &rt_sf->info;
  869. regs->gpr[5] = (unsigned long) &rt_sf->uc;
  870. regs->gpr[6] = (unsigned long) rt_sf;
  871. regs->nip = (unsigned long) ksig->ka.sa.sa_handler;
  872. /* enter the signal handler in native-endian mode */
  873. regs->msr &= ~MSR_LE;
  874. regs->msr |= (MSR_KERNEL & MSR_LE);
  875. return 0;
  876. badframe:
  877. if (show_unhandled_signals)
  878. printk_ratelimited(KERN_INFO
  879. "%s[%d]: bad frame in handle_rt_signal32: "
  880. "%p nip %08lx lr %08lx\n",
  881. tsk->comm, tsk->pid,
  882. addr, regs->nip, regs->link);
  883. return 1;
  884. }
  885. static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
  886. {
  887. sigset_t set;
  888. struct mcontext __user *mcp;
  889. if (get_sigset_t(&set, &ucp->uc_sigmask))
  890. return -EFAULT;
  891. #ifdef CONFIG_PPC64
  892. {
  893. u32 cmcp;
  894. if (__get_user(cmcp, &ucp->uc_regs))
  895. return -EFAULT;
  896. mcp = (struct mcontext __user *)(u64)cmcp;
  897. /* no need to check access_ok(mcp), since mcp < 4GB */
  898. }
  899. #else
  900. if (__get_user(mcp, &ucp->uc_regs))
  901. return -EFAULT;
  902. if (!access_ok(VERIFY_READ, mcp, sizeof(*mcp)))
  903. return -EFAULT;
  904. #endif
  905. set_current_blocked(&set);
  906. if (restore_user_regs(regs, mcp, sig))
  907. return -EFAULT;
  908. return 0;
  909. }
  910. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  911. static int do_setcontext_tm(struct ucontext __user *ucp,
  912. struct ucontext __user *tm_ucp,
  913. struct pt_regs *regs)
  914. {
  915. sigset_t set;
  916. struct mcontext __user *mcp;
  917. struct mcontext __user *tm_mcp;
  918. u32 cmcp;
  919. u32 tm_cmcp;
  920. if (get_sigset_t(&set, &ucp->uc_sigmask))
  921. return -EFAULT;
  922. if (__get_user(cmcp, &ucp->uc_regs) ||
  923. __get_user(tm_cmcp, &tm_ucp->uc_regs))
  924. return -EFAULT;
  925. mcp = (struct mcontext __user *)(u64)cmcp;
  926. tm_mcp = (struct mcontext __user *)(u64)tm_cmcp;
  927. /* no need to check access_ok(mcp), since mcp < 4GB */
  928. set_current_blocked(&set);
  929. if (restore_tm_user_regs(regs, mcp, tm_mcp))
  930. return -EFAULT;
  931. return 0;
  932. }
  933. #endif
  934. #ifdef CONFIG_PPC64
  935. COMPAT_SYSCALL_DEFINE3(swapcontext, struct ucontext __user *, old_ctx,
  936. struct ucontext __user *, new_ctx, int, ctx_size)
  937. #else
  938. SYSCALL_DEFINE3(swapcontext, struct ucontext __user *, old_ctx,
  939. struct ucontext __user *, new_ctx, long, ctx_size)
  940. #endif
  941. {
  942. struct pt_regs *regs = current_pt_regs();
  943. int ctx_has_vsx_region = 0;
  944. #ifdef CONFIG_PPC64
  945. unsigned long new_msr = 0;
  946. if (new_ctx) {
  947. struct mcontext __user *mcp;
  948. u32 cmcp;
  949. /*
  950. * Get pointer to the real mcontext. No need for
  951. * access_ok since we are dealing with compat
  952. * pointers.
  953. */
  954. if (__get_user(cmcp, &new_ctx->uc_regs))
  955. return -EFAULT;
  956. mcp = (struct mcontext __user *)(u64)cmcp;
  957. if (__get_user(new_msr, &mcp->mc_gregs[PT_MSR]))
  958. return -EFAULT;
  959. }
  960. /*
  961. * Check that the context is not smaller than the original
  962. * size (with VMX but without VSX)
  963. */
  964. if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
  965. return -EINVAL;
  966. /*
  967. * If the new context state sets the MSR VSX bits but
  968. * it doesn't provide VSX state.
  969. */
  970. if ((ctx_size < sizeof(struct ucontext)) &&
  971. (new_msr & MSR_VSX))
  972. return -EINVAL;
  973. /* Does the context have enough room to store VSX data? */
  974. if (ctx_size >= sizeof(struct ucontext))
  975. ctx_has_vsx_region = 1;
  976. #else
  977. /* Context size is for future use. Right now, we only make sure
  978. * we are passed something we understand
  979. */
  980. if (ctx_size < sizeof(struct ucontext))
  981. return -EINVAL;
  982. #endif
  983. if (old_ctx != NULL) {
  984. struct mcontext __user *mctx;
  985. /*
  986. * old_ctx might not be 16-byte aligned, in which
  987. * case old_ctx->uc_mcontext won't be either.
  988. * Because we have the old_ctx->uc_pad2 field
  989. * before old_ctx->uc_mcontext, we need to round down
  990. * from &old_ctx->uc_mcontext to a 16-byte boundary.
  991. */
  992. mctx = (struct mcontext __user *)
  993. ((unsigned long) &old_ctx->uc_mcontext & ~0xfUL);
  994. if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
  995. || save_user_regs(regs, mctx, NULL, 0, ctx_has_vsx_region)
  996. || put_sigset_t(&old_ctx->uc_sigmask, &current->blocked)
  997. || __put_user(to_user_ptr(mctx), &old_ctx->uc_regs))
  998. return -EFAULT;
  999. }
  1000. if (new_ctx == NULL)
  1001. return 0;
  1002. if (!access_ok(VERIFY_READ, new_ctx, ctx_size) ||
  1003. fault_in_pages_readable((u8 __user *)new_ctx, ctx_size))
  1004. return -EFAULT;
  1005. /*
  1006. * If we get a fault copying the context into the kernel's
  1007. * image of the user's registers, we can't just return -EFAULT
  1008. * because the user's registers will be corrupted. For instance
  1009. * the NIP value may have been updated but not some of the
  1010. * other registers. Given that we have done the access_ok
  1011. * and successfully read the first and last bytes of the region
  1012. * above, this should only happen in an out-of-memory situation
  1013. * or if another thread unmaps the region containing the context.
  1014. * We kill the task with a SIGSEGV in this situation.
  1015. */
  1016. if (do_setcontext(new_ctx, regs, 0))
  1017. do_exit(SIGSEGV);
  1018. set_thread_flag(TIF_RESTOREALL);
  1019. return 0;
  1020. }
  1021. #ifdef CONFIG_PPC64
  1022. COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
  1023. #else
  1024. SYSCALL_DEFINE0(rt_sigreturn)
  1025. #endif
  1026. {
  1027. struct rt_sigframe __user *rt_sf;
  1028. struct pt_regs *regs = current_pt_regs();
  1029. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1030. struct ucontext __user *uc_transact;
  1031. unsigned long msr_hi;
  1032. unsigned long tmp;
  1033. int tm_restore = 0;
  1034. #endif
  1035. /* Always make any pending restarted system calls return -EINTR */
  1036. current->restart_block.fn = do_no_restart_syscall;
  1037. rt_sf = (struct rt_sigframe __user *)
  1038. (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
  1039. if (!access_ok(VERIFY_READ, rt_sf, sizeof(*rt_sf)))
  1040. goto bad;
  1041. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1042. /*
  1043. * If there is a transactional state then throw it away.
  1044. * The purpose of a sigreturn is to destroy all traces of the
  1045. * signal frame, this includes any transactional state created
  1046. * within in. We only check for suspended as we can never be
  1047. * active in the kernel, we are active, there is nothing better to
  1048. * do than go ahead and Bad Thing later.
  1049. * The cause is not important as there will never be a
  1050. * recheckpoint so it's not user visible.
  1051. */
  1052. if (MSR_TM_SUSPENDED(mfmsr()))
  1053. tm_reclaim_current(0);
  1054. if (__get_user(tmp, &rt_sf->uc.uc_link))
  1055. goto bad;
  1056. uc_transact = (struct ucontext __user *)(uintptr_t)tmp;
  1057. if (uc_transact) {
  1058. u32 cmcp;
  1059. struct mcontext __user *mcp;
  1060. if (__get_user(cmcp, &uc_transact->uc_regs))
  1061. return -EFAULT;
  1062. mcp = (struct mcontext __user *)(u64)cmcp;
  1063. /* The top 32 bits of the MSR are stashed in the transactional
  1064. * ucontext. */
  1065. if (__get_user(msr_hi, &mcp->mc_gregs[PT_MSR]))
  1066. goto bad;
  1067. if (MSR_TM_ACTIVE(msr_hi<<32)) {
  1068. /* Trying to start TM on non TM system */
  1069. if (!cpu_has_feature(CPU_FTR_TM))
  1070. goto bad;
  1071. /* We only recheckpoint on return if we're
  1072. * transaction.
  1073. */
  1074. tm_restore = 1;
  1075. if (do_setcontext_tm(&rt_sf->uc, uc_transact, regs))
  1076. goto bad;
  1077. }
  1078. }
  1079. if (!tm_restore)
  1080. /* Fall through, for non-TM restore */
  1081. #endif
  1082. if (do_setcontext(&rt_sf->uc, regs, 1))
  1083. goto bad;
  1084. /*
  1085. * It's not clear whether or why it is desirable to save the
  1086. * sigaltstack setting on signal delivery and restore it on
  1087. * signal return. But other architectures do this and we have
  1088. * always done it up until now so it is probably better not to
  1089. * change it. -- paulus
  1090. */
  1091. #ifdef CONFIG_PPC64
  1092. if (compat_restore_altstack(&rt_sf->uc.uc_stack))
  1093. goto bad;
  1094. #else
  1095. if (restore_altstack(&rt_sf->uc.uc_stack))
  1096. goto bad;
  1097. #endif
  1098. set_thread_flag(TIF_RESTOREALL);
  1099. return 0;
  1100. bad:
  1101. if (show_unhandled_signals)
  1102. printk_ratelimited(KERN_INFO
  1103. "%s[%d]: bad frame in sys_rt_sigreturn: "
  1104. "%p nip %08lx lr %08lx\n",
  1105. current->comm, current->pid,
  1106. rt_sf, regs->nip, regs->link);
  1107. force_sig(SIGSEGV, current);
  1108. return 0;
  1109. }
  1110. #ifdef CONFIG_PPC32
  1111. SYSCALL_DEFINE3(debug_setcontext, struct ucontext __user *, ctx,
  1112. int, ndbg, struct sig_dbg_op __user *, dbg)
  1113. {
  1114. struct pt_regs *regs = current_pt_regs();
  1115. struct sig_dbg_op op;
  1116. int i;
  1117. unsigned long new_msr = regs->msr;
  1118. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1119. unsigned long new_dbcr0 = current->thread.debug.dbcr0;
  1120. #endif
  1121. for (i=0; i<ndbg; i++) {
  1122. if (copy_from_user(&op, dbg + i, sizeof(op)))
  1123. return -EFAULT;
  1124. switch (op.dbg_type) {
  1125. case SIG_DBG_SINGLE_STEPPING:
  1126. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1127. if (op.dbg_value) {
  1128. new_msr |= MSR_DE;
  1129. new_dbcr0 |= (DBCR0_IDM | DBCR0_IC);
  1130. } else {
  1131. new_dbcr0 &= ~DBCR0_IC;
  1132. if (!DBCR_ACTIVE_EVENTS(new_dbcr0,
  1133. current->thread.debug.dbcr1)) {
  1134. new_msr &= ~MSR_DE;
  1135. new_dbcr0 &= ~DBCR0_IDM;
  1136. }
  1137. }
  1138. #else
  1139. if (op.dbg_value)
  1140. new_msr |= MSR_SE;
  1141. else
  1142. new_msr &= ~MSR_SE;
  1143. #endif
  1144. break;
  1145. case SIG_DBG_BRANCH_TRACING:
  1146. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1147. return -EINVAL;
  1148. #else
  1149. if (op.dbg_value)
  1150. new_msr |= MSR_BE;
  1151. else
  1152. new_msr &= ~MSR_BE;
  1153. #endif
  1154. break;
  1155. default:
  1156. return -EINVAL;
  1157. }
  1158. }
  1159. /* We wait until here to actually install the values in the
  1160. registers so if we fail in the above loop, it will not
  1161. affect the contents of these registers. After this point,
  1162. failure is a problem, anyway, and it's very unlikely unless
  1163. the user is really doing something wrong. */
  1164. regs->msr = new_msr;
  1165. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1166. current->thread.debug.dbcr0 = new_dbcr0;
  1167. #endif
  1168. if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx)) ||
  1169. fault_in_pages_readable((u8 __user *)ctx, sizeof(*ctx)))
  1170. return -EFAULT;
  1171. /*
  1172. * If we get a fault copying the context into the kernel's
  1173. * image of the user's registers, we can't just return -EFAULT
  1174. * because the user's registers will be corrupted. For instance
  1175. * the NIP value may have been updated but not some of the
  1176. * other registers. Given that we have done the access_ok
  1177. * and successfully read the first and last bytes of the region
  1178. * above, this should only happen in an out-of-memory situation
  1179. * or if another thread unmaps the region containing the context.
  1180. * We kill the task with a SIGSEGV in this situation.
  1181. */
  1182. if (do_setcontext(ctx, regs, 1)) {
  1183. if (show_unhandled_signals)
  1184. printk_ratelimited(KERN_INFO "%s[%d]: bad frame in "
  1185. "sys_debug_setcontext: %p nip %08lx "
  1186. "lr %08lx\n",
  1187. current->comm, current->pid,
  1188. ctx, regs->nip, regs->link);
  1189. force_sig(SIGSEGV, current);
  1190. goto out;
  1191. }
  1192. /*
  1193. * It's not clear whether or why it is desirable to save the
  1194. * sigaltstack setting on signal delivery and restore it on
  1195. * signal return. But other architectures do this and we have
  1196. * always done it up until now so it is probably better not to
  1197. * change it. -- paulus
  1198. */
  1199. restore_altstack(&ctx->uc_stack);
  1200. set_thread_flag(TIF_RESTOREALL);
  1201. out:
  1202. return 0;
  1203. }
  1204. #endif
  1205. /*
  1206. * OK, we're invoking a handler
  1207. */
  1208. int handle_signal32(struct ksignal *ksig, sigset_t *oldset,
  1209. struct task_struct *tsk)
  1210. {
  1211. struct sigcontext __user *sc;
  1212. struct sigframe __user *frame;
  1213. struct mcontext __user *tm_mctx = NULL;
  1214. unsigned long newsp = 0;
  1215. int sigret;
  1216. unsigned long tramp;
  1217. struct pt_regs *regs = tsk->thread.regs;
  1218. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1219. /* Save the thread's msr before get_tm_stackpointer() changes it */
  1220. unsigned long msr = regs->msr;
  1221. #endif
  1222. BUG_ON(tsk != current);
  1223. /* Set up Signal Frame */
  1224. frame = get_sigframe(ksig, get_tm_stackpointer(tsk), sizeof(*frame), 1);
  1225. if (unlikely(frame == NULL))
  1226. goto badframe;
  1227. sc = (struct sigcontext __user *) &frame->sctx;
  1228. #if _NSIG != 64
  1229. #error "Please adjust handle_signal()"
  1230. #endif
  1231. if (__put_user(to_user_ptr(ksig->ka.sa.sa_handler), &sc->handler)
  1232. || __put_user(oldset->sig[0], &sc->oldmask)
  1233. #ifdef CONFIG_PPC64
  1234. || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
  1235. #else
  1236. || __put_user(oldset->sig[1], &sc->_unused[3])
  1237. #endif
  1238. || __put_user(to_user_ptr(&frame->mctx), &sc->regs)
  1239. || __put_user(ksig->sig, &sc->signal))
  1240. goto badframe;
  1241. if (vdso32_sigtramp && tsk->mm->context.vdso_base) {
  1242. sigret = 0;
  1243. tramp = tsk->mm->context.vdso_base + vdso32_sigtramp;
  1244. } else {
  1245. sigret = __NR_sigreturn;
  1246. tramp = (unsigned long) frame->mctx.tramp;
  1247. }
  1248. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1249. tm_mctx = &frame->mctx_transact;
  1250. if (MSR_TM_ACTIVE(msr)) {
  1251. if (save_tm_user_regs(regs, &frame->mctx, &frame->mctx_transact,
  1252. sigret, msr))
  1253. goto badframe;
  1254. }
  1255. else
  1256. #endif
  1257. {
  1258. if (save_user_regs(regs, &frame->mctx, tm_mctx, sigret, 1))
  1259. goto badframe;
  1260. }
  1261. regs->link = tramp;
  1262. tsk->thread.fp_state.fpscr = 0; /* turn off all fp exceptions */
  1263. /* create a stack frame for the caller of the handler */
  1264. newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
  1265. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  1266. goto badframe;
  1267. regs->gpr[1] = newsp;
  1268. regs->gpr[3] = ksig->sig;
  1269. regs->gpr[4] = (unsigned long) sc;
  1270. regs->nip = (unsigned long) (unsigned long)ksig->ka.sa.sa_handler;
  1271. /* enter the signal handler in big-endian mode */
  1272. regs->msr &= ~MSR_LE;
  1273. return 0;
  1274. badframe:
  1275. if (show_unhandled_signals)
  1276. printk_ratelimited(KERN_INFO
  1277. "%s[%d]: bad frame in handle_signal32: "
  1278. "%p nip %08lx lr %08lx\n",
  1279. tsk->comm, tsk->pid,
  1280. frame, regs->nip, regs->link);
  1281. return 1;
  1282. }
  1283. /*
  1284. * Do a signal return; undo the signal stack.
  1285. */
  1286. #ifdef CONFIG_PPC64
  1287. COMPAT_SYSCALL_DEFINE0(sigreturn)
  1288. #else
  1289. SYSCALL_DEFINE0(sigreturn)
  1290. #endif
  1291. {
  1292. struct pt_regs *regs = current_pt_regs();
  1293. struct sigframe __user *sf;
  1294. struct sigcontext __user *sc;
  1295. struct sigcontext sigctx;
  1296. struct mcontext __user *sr;
  1297. void __user *addr;
  1298. sigset_t set;
  1299. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1300. struct mcontext __user *mcp, *tm_mcp;
  1301. unsigned long msr_hi;
  1302. #endif
  1303. /* Always make any pending restarted system calls return -EINTR */
  1304. current->restart_block.fn = do_no_restart_syscall;
  1305. sf = (struct sigframe __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
  1306. sc = &sf->sctx;
  1307. addr = sc;
  1308. if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
  1309. goto badframe;
  1310. #ifdef CONFIG_PPC64
  1311. /*
  1312. * Note that PPC32 puts the upper 32 bits of the sigmask in the
  1313. * unused part of the signal stackframe
  1314. */
  1315. set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3]) << 32);
  1316. #else
  1317. set.sig[0] = sigctx.oldmask;
  1318. set.sig[1] = sigctx._unused[3];
  1319. #endif
  1320. set_current_blocked(&set);
  1321. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1322. mcp = (struct mcontext __user *)&sf->mctx;
  1323. tm_mcp = (struct mcontext __user *)&sf->mctx_transact;
  1324. if (__get_user(msr_hi, &tm_mcp->mc_gregs[PT_MSR]))
  1325. goto badframe;
  1326. if (MSR_TM_ACTIVE(msr_hi<<32)) {
  1327. if (!cpu_has_feature(CPU_FTR_TM))
  1328. goto badframe;
  1329. if (restore_tm_user_regs(regs, mcp, tm_mcp))
  1330. goto badframe;
  1331. } else
  1332. #endif
  1333. {
  1334. sr = (struct mcontext __user *)from_user_ptr(sigctx.regs);
  1335. addr = sr;
  1336. if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
  1337. || restore_user_regs(regs, sr, 1))
  1338. goto badframe;
  1339. }
  1340. set_thread_flag(TIF_RESTOREALL);
  1341. return 0;
  1342. badframe:
  1343. if (show_unhandled_signals)
  1344. printk_ratelimited(KERN_INFO
  1345. "%s[%d]: bad frame in sys_sigreturn: "
  1346. "%p nip %08lx lr %08lx\n",
  1347. current->comm, current->pid,
  1348. addr, regs->nip, regs->link);
  1349. force_sig(SIGSEGV, current);
  1350. return 0;
  1351. }