signal_32.c 41 KB

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