signal_32.c 38 KB

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