signal_32.c 44 KB

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