signal_32.c 43 KB

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