signal_32.c 44 KB

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