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