signal_32.c 44 KB

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