signal_32.c 43 KB

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