signal_32.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. #ifdef CONFIG_PPC64
  45. #include "ppc32.h"
  46. #include <asm/unistd.h>
  47. #else
  48. #include <asm/ucontext.h>
  49. #include <asm/pgtable.h>
  50. #endif
  51. #include "signal.h"
  52. #undef DEBUG_SIG
  53. #ifdef CONFIG_PPC64
  54. #define sys_sigsuspend compat_sys_sigsuspend
  55. #define sys_rt_sigsuspend compat_sys_rt_sigsuspend
  56. #define sys_rt_sigreturn compat_sys_rt_sigreturn
  57. #define sys_sigaction compat_sys_sigaction
  58. #define sys_swapcontext compat_sys_swapcontext
  59. #define sys_sigreturn compat_sys_sigreturn
  60. #define old_sigaction old_sigaction32
  61. #define sigcontext sigcontext32
  62. #define mcontext mcontext32
  63. #define ucontext ucontext32
  64. /*
  65. * Userspace code may pass a ucontext which doesn't include VSX added
  66. * at the end. We need to check for this case.
  67. */
  68. #define UCONTEXTSIZEWITHOUTVSX \
  69. (sizeof(struct ucontext) - sizeof(elf_vsrreghalf_t32))
  70. /*
  71. * Returning 0 means we return to userspace via
  72. * ret_from_except and thus restore all user
  73. * registers from *regs. This is what we need
  74. * to do when a signal has been delivered.
  75. */
  76. #define GP_REGS_SIZE min(sizeof(elf_gregset_t32), sizeof(struct pt_regs32))
  77. #undef __SIGNAL_FRAMESIZE
  78. #define __SIGNAL_FRAMESIZE __SIGNAL_FRAMESIZE32
  79. #undef ELF_NVRREG
  80. #define ELF_NVRREG ELF_NVRREG32
  81. /*
  82. * Functions for flipping sigsets (thanks to brain dead generic
  83. * implementation that makes things simple for little endian only)
  84. */
  85. static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
  86. {
  87. compat_sigset_t cset;
  88. switch (_NSIG_WORDS) {
  89. case 4: cset.sig[6] = set->sig[3] & 0xffffffffull;
  90. cset.sig[7] = set->sig[3] >> 32;
  91. case 3: cset.sig[4] = set->sig[2] & 0xffffffffull;
  92. cset.sig[5] = set->sig[2] >> 32;
  93. case 2: cset.sig[2] = set->sig[1] & 0xffffffffull;
  94. cset.sig[3] = set->sig[1] >> 32;
  95. case 1: cset.sig[0] = set->sig[0] & 0xffffffffull;
  96. cset.sig[1] = set->sig[0] >> 32;
  97. }
  98. return copy_to_user(uset, &cset, sizeof(*uset));
  99. }
  100. static inline int get_sigset_t(sigset_t *set,
  101. const compat_sigset_t __user *uset)
  102. {
  103. compat_sigset_t s32;
  104. if (copy_from_user(&s32, uset, sizeof(*uset)))
  105. return -EFAULT;
  106. /*
  107. * Swap the 2 words of the 64-bit sigset_t (they are stored
  108. * in the "wrong" endian in 32-bit user storage).
  109. */
  110. switch (_NSIG_WORDS) {
  111. case 4: set->sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
  112. case 3: set->sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
  113. case 2: set->sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
  114. case 1: set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
  115. }
  116. return 0;
  117. }
  118. static inline int get_old_sigaction(struct k_sigaction *new_ka,
  119. struct old_sigaction __user *act)
  120. {
  121. compat_old_sigset_t mask;
  122. compat_uptr_t handler, restorer;
  123. if (get_user(handler, &act->sa_handler) ||
  124. __get_user(restorer, &act->sa_restorer) ||
  125. __get_user(new_ka->sa.sa_flags, &act->sa_flags) ||
  126. __get_user(mask, &act->sa_mask))
  127. return -EFAULT;
  128. new_ka->sa.sa_handler = compat_ptr(handler);
  129. new_ka->sa.sa_restorer = compat_ptr(restorer);
  130. siginitset(&new_ka->sa.sa_mask, mask);
  131. return 0;
  132. }
  133. #define to_user_ptr(p) ptr_to_compat(p)
  134. #define from_user_ptr(p) compat_ptr(p)
  135. static inline int save_general_regs(struct pt_regs *regs,
  136. struct mcontext __user *frame)
  137. {
  138. elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
  139. int i;
  140. WARN_ON(!FULL_REGS(regs));
  141. for (i = 0; i <= PT_RESULT; i ++) {
  142. if (i == 14 && !FULL_REGS(regs))
  143. i = 32;
  144. if (__put_user((unsigned int)gregs[i], &frame->mc_gregs[i]))
  145. return -EFAULT;
  146. }
  147. return 0;
  148. }
  149. static inline int restore_general_regs(struct pt_regs *regs,
  150. struct mcontext __user *sr)
  151. {
  152. elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
  153. int i;
  154. for (i = 0; i <= PT_RESULT; i++) {
  155. if ((i == PT_MSR) || (i == PT_SOFTE))
  156. continue;
  157. if (__get_user(gregs[i], &sr->mc_gregs[i]))
  158. return -EFAULT;
  159. }
  160. return 0;
  161. }
  162. #else /* CONFIG_PPC64 */
  163. #define GP_REGS_SIZE min(sizeof(elf_gregset_t), sizeof(struct pt_regs))
  164. static inline int put_sigset_t(sigset_t __user *uset, sigset_t *set)
  165. {
  166. return copy_to_user(uset, set, sizeof(*uset));
  167. }
  168. static inline int get_sigset_t(sigset_t *set, const sigset_t __user *uset)
  169. {
  170. return copy_from_user(set, uset, sizeof(*uset));
  171. }
  172. static inline int get_old_sigaction(struct k_sigaction *new_ka,
  173. struct old_sigaction __user *act)
  174. {
  175. old_sigset_t mask;
  176. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  177. __get_user(new_ka->sa.sa_handler, &act->sa_handler) ||
  178. __get_user(new_ka->sa.sa_restorer, &act->sa_restorer) ||
  179. __get_user(new_ka->sa.sa_flags, &act->sa_flags) ||
  180. __get_user(mask, &act->sa_mask))
  181. return -EFAULT;
  182. siginitset(&new_ka->sa.sa_mask, mask);
  183. return 0;
  184. }
  185. #define to_user_ptr(p) ((unsigned long)(p))
  186. #define from_user_ptr(p) ((void __user *)(p))
  187. static inline int save_general_regs(struct pt_regs *regs,
  188. struct mcontext __user *frame)
  189. {
  190. WARN_ON(!FULL_REGS(regs));
  191. return __copy_to_user(&frame->mc_gregs, regs, GP_REGS_SIZE);
  192. }
  193. static inline int restore_general_regs(struct pt_regs *regs,
  194. struct mcontext __user *sr)
  195. {
  196. /* copy up to but not including MSR */
  197. if (__copy_from_user(regs, &sr->mc_gregs,
  198. PT_MSR * sizeof(elf_greg_t)))
  199. return -EFAULT;
  200. /* copy from orig_r3 (the word after the MSR) up to the end */
  201. if (__copy_from_user(&regs->orig_gpr3, &sr->mc_gregs[PT_ORIG_R3],
  202. GP_REGS_SIZE - PT_ORIG_R3 * sizeof(elf_greg_t)))
  203. return -EFAULT;
  204. return 0;
  205. }
  206. #endif /* CONFIG_PPC64 */
  207. /*
  208. * Atomically swap in the new signal mask, and wait for a signal.
  209. */
  210. long sys_sigsuspend(old_sigset_t mask)
  211. {
  212. sigset_t blocked;
  213. siginitset(&blocked, mask);
  214. return sigsuspend(&blocked);
  215. }
  216. long sys_sigaction(int sig, struct old_sigaction __user *act,
  217. struct old_sigaction __user *oact)
  218. {
  219. struct k_sigaction new_ka, old_ka;
  220. int ret;
  221. #ifdef CONFIG_PPC64
  222. if (sig < 0)
  223. sig = -sig;
  224. #endif
  225. if (act) {
  226. if (get_old_sigaction(&new_ka, act))
  227. return -EFAULT;
  228. }
  229. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  230. if (!ret && oact) {
  231. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  232. __put_user(to_user_ptr(old_ka.sa.sa_handler),
  233. &oact->sa_handler) ||
  234. __put_user(to_user_ptr(old_ka.sa.sa_restorer),
  235. &oact->sa_restorer) ||
  236. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  237. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  238. return -EFAULT;
  239. }
  240. return ret;
  241. }
  242. /*
  243. * When we have signals to deliver, we set up on the
  244. * user stack, going down from the original stack pointer:
  245. * an ABI gap of 56 words
  246. * an mcontext struct
  247. * a sigcontext struct
  248. * a gap of __SIGNAL_FRAMESIZE bytes
  249. *
  250. * Each of these things must be a multiple of 16 bytes in size. The following
  251. * structure represent all of this except the __SIGNAL_FRAMESIZE gap
  252. *
  253. */
  254. struct sigframe {
  255. struct sigcontext sctx; /* the sigcontext */
  256. struct mcontext mctx; /* all the register values */
  257. /*
  258. * Programs using the rs6000/xcoff abi can save up to 19 gp
  259. * regs and 18 fp regs below sp before decrementing it.
  260. */
  261. int abigap[56];
  262. };
  263. /* We use the mc_pad field for the signal return trampoline. */
  264. #define tramp mc_pad
  265. /*
  266. * When we have rt signals to deliver, we set up on the
  267. * user stack, going down from the original stack pointer:
  268. * one rt_sigframe struct (siginfo + ucontext + ABI gap)
  269. * a gap of __SIGNAL_FRAMESIZE+16 bytes
  270. * (the +16 is to get the siginfo and ucontext in the same
  271. * positions as in older kernels).
  272. *
  273. * Each of these things must be a multiple of 16 bytes in size.
  274. *
  275. */
  276. struct rt_sigframe {
  277. #ifdef CONFIG_PPC64
  278. compat_siginfo_t info;
  279. #else
  280. struct siginfo info;
  281. #endif
  282. struct ucontext uc;
  283. /*
  284. * Programs using the rs6000/xcoff abi can save up to 19 gp
  285. * regs and 18 fp regs below sp before decrementing it.
  286. */
  287. int abigap[56];
  288. };
  289. #ifdef CONFIG_VSX
  290. unsigned long copy_fpr_to_user(void __user *to,
  291. struct task_struct *task)
  292. {
  293. double buf[ELF_NFPREG];
  294. int i;
  295. /* save FPR copy to local buffer then write to the thread_struct */
  296. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  297. buf[i] = task->thread.TS_FPR(i);
  298. memcpy(&buf[i], &task->thread.fpscr, sizeof(double));
  299. return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
  300. }
  301. unsigned long copy_fpr_from_user(struct task_struct *task,
  302. void __user *from)
  303. {
  304. double buf[ELF_NFPREG];
  305. int i;
  306. if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
  307. return 1;
  308. for (i = 0; i < (ELF_NFPREG - 1) ; i++)
  309. task->thread.TS_FPR(i) = buf[i];
  310. memcpy(&task->thread.fpscr, &buf[i], sizeof(double));
  311. return 0;
  312. }
  313. unsigned long copy_vsx_to_user(void __user *to,
  314. struct task_struct *task)
  315. {
  316. double buf[ELF_NVSRHALFREG];
  317. int i;
  318. /* save FPR copy to local buffer then write to the thread_struct */
  319. for (i = 0; i < ELF_NVSRHALFREG; i++)
  320. buf[i] = task->thread.fpr[i][TS_VSRLOWOFFSET];
  321. return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
  322. }
  323. unsigned long copy_vsx_from_user(struct task_struct *task,
  324. void __user *from)
  325. {
  326. double buf[ELF_NVSRHALFREG];
  327. int i;
  328. if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
  329. return 1;
  330. for (i = 0; i < ELF_NVSRHALFREG ; i++)
  331. task->thread.fpr[i][TS_VSRLOWOFFSET] = buf[i];
  332. return 0;
  333. }
  334. #else
  335. inline unsigned long copy_fpr_to_user(void __user *to,
  336. struct task_struct *task)
  337. {
  338. return __copy_to_user(to, task->thread.fpr,
  339. ELF_NFPREG * sizeof(double));
  340. }
  341. inline unsigned long copy_fpr_from_user(struct task_struct *task,
  342. void __user *from)
  343. {
  344. return __copy_from_user(task->thread.fpr, from,
  345. ELF_NFPREG * sizeof(double));
  346. }
  347. #endif
  348. /*
  349. * Save the current user registers on the user stack.
  350. * We only save the altivec/spe registers if the process has used
  351. * altivec/spe instructions at some point.
  352. */
  353. static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
  354. int sigret, int ctx_has_vsx_region)
  355. {
  356. unsigned long msr = regs->msr;
  357. /* Make sure floating point registers are stored in regs */
  358. flush_fp_to_thread(current);
  359. /* save general registers */
  360. if (save_general_regs(regs, frame))
  361. return 1;
  362. #ifdef CONFIG_ALTIVEC
  363. /* save altivec registers */
  364. if (current->thread.used_vr) {
  365. flush_altivec_to_thread(current);
  366. if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
  367. ELF_NVRREG * sizeof(vector128)))
  368. return 1;
  369. /* set MSR_VEC in the saved MSR value to indicate that
  370. frame->mc_vregs contains valid data */
  371. msr |= MSR_VEC;
  372. }
  373. /* else assert((regs->msr & MSR_VEC) == 0) */
  374. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  375. * use altivec. Since VSCR only contains 32 bits saved in the least
  376. * significant bits of a vector, we "cheat" and stuff VRSAVE in the
  377. * most significant bits of that same vector. --BenH
  378. */
  379. if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
  380. return 1;
  381. #endif /* CONFIG_ALTIVEC */
  382. if (copy_fpr_to_user(&frame->mc_fregs, current))
  383. return 1;
  384. #ifdef CONFIG_VSX
  385. /*
  386. * Copy VSR 0-31 upper half from thread_struct to local
  387. * buffer, then write that to userspace. Also set MSR_VSX in
  388. * the saved MSR value to indicate that frame->mc_vregs
  389. * contains valid data
  390. */
  391. if (current->thread.used_vsr && ctx_has_vsx_region) {
  392. __giveup_vsx(current);
  393. if (copy_vsx_to_user(&frame->mc_vsregs, current))
  394. return 1;
  395. msr |= MSR_VSX;
  396. }
  397. #endif /* CONFIG_VSX */
  398. #ifdef CONFIG_SPE
  399. /* save spe registers */
  400. if (current->thread.used_spe) {
  401. flush_spe_to_thread(current);
  402. if (__copy_to_user(&frame->mc_vregs, current->thread.evr,
  403. ELF_NEVRREG * sizeof(u32)))
  404. return 1;
  405. /* set MSR_SPE in the saved MSR value to indicate that
  406. frame->mc_vregs contains valid data */
  407. msr |= MSR_SPE;
  408. }
  409. /* else assert((regs->msr & MSR_SPE) == 0) */
  410. /* We always copy to/from spefscr */
  411. if (__put_user(current->thread.spefscr, (u32 __user *)&frame->mc_vregs + ELF_NEVRREG))
  412. return 1;
  413. #endif /* CONFIG_SPE */
  414. if (__put_user(msr, &frame->mc_gregs[PT_MSR]))
  415. return 1;
  416. if (sigret) {
  417. /* Set up the sigreturn trampoline: li r0,sigret; sc */
  418. if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
  419. || __put_user(0x44000002UL, &frame->tramp[1]))
  420. return 1;
  421. flush_icache_range((unsigned long) &frame->tramp[0],
  422. (unsigned long) &frame->tramp[2]);
  423. }
  424. return 0;
  425. }
  426. /*
  427. * Restore the current user register values from the user stack,
  428. * (except for MSR).
  429. */
  430. static long restore_user_regs(struct pt_regs *regs,
  431. struct mcontext __user *sr, int sig)
  432. {
  433. long err;
  434. unsigned int save_r2 = 0;
  435. unsigned long msr;
  436. #ifdef CONFIG_VSX
  437. int i;
  438. #endif
  439. /*
  440. * restore general registers but not including MSR or SOFTE. Also
  441. * take care of keeping r2 (TLS) intact if not a signal
  442. */
  443. if (!sig)
  444. save_r2 = (unsigned int)regs->gpr[2];
  445. err = restore_general_regs(regs, sr);
  446. regs->trap = 0;
  447. err |= __get_user(msr, &sr->mc_gregs[PT_MSR]);
  448. if (!sig)
  449. regs->gpr[2] = (unsigned long) save_r2;
  450. if (err)
  451. return 1;
  452. /* if doing signal return, restore the previous little-endian mode */
  453. if (sig)
  454. regs->msr = (regs->msr & ~MSR_LE) | (msr & MSR_LE);
  455. /*
  456. * Do this before updating the thread state in
  457. * current->thread.fpr/vr/evr. That way, if we get preempted
  458. * and another task grabs the FPU/Altivec/SPE, it won't be
  459. * tempted to save the current CPU state into the thread_struct
  460. * and corrupt what we are writing there.
  461. */
  462. discard_lazy_cpu_state();
  463. #ifdef CONFIG_ALTIVEC
  464. /*
  465. * Force the process to reload the altivec registers from
  466. * current->thread when it next does altivec instructions
  467. */
  468. regs->msr &= ~MSR_VEC;
  469. if (msr & MSR_VEC) {
  470. /* restore altivec registers from the stack */
  471. if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
  472. sizeof(sr->mc_vregs)))
  473. return 1;
  474. } else if (current->thread.used_vr)
  475. memset(current->thread.vr, 0, ELF_NVRREG * sizeof(vector128));
  476. /* Always get VRSAVE back */
  477. if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
  478. return 1;
  479. #endif /* CONFIG_ALTIVEC */
  480. if (copy_fpr_from_user(current, &sr->mc_fregs))
  481. return 1;
  482. #ifdef CONFIG_VSX
  483. /*
  484. * Force the process to reload the VSX registers from
  485. * current->thread when it next does VSX instruction.
  486. */
  487. regs->msr &= ~MSR_VSX;
  488. if (msr & MSR_VSX) {
  489. /*
  490. * Restore altivec registers from the stack to a local
  491. * buffer, then write this out to the thread_struct
  492. */
  493. if (copy_vsx_from_user(current, &sr->mc_vsregs))
  494. return 1;
  495. } else if (current->thread.used_vsr)
  496. for (i = 0; i < 32 ; i++)
  497. current->thread.fpr[i][TS_VSRLOWOFFSET] = 0;
  498. #endif /* CONFIG_VSX */
  499. /*
  500. * force the process to reload the FP registers from
  501. * current->thread when it next does FP instructions
  502. */
  503. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  504. #ifdef CONFIG_SPE
  505. /* force the process to reload the spe registers from
  506. current->thread when it next does spe instructions */
  507. regs->msr &= ~MSR_SPE;
  508. if (msr & MSR_SPE) {
  509. /* restore spe registers from the stack */
  510. if (__copy_from_user(current->thread.evr, &sr->mc_vregs,
  511. ELF_NEVRREG * sizeof(u32)))
  512. return 1;
  513. } else if (current->thread.used_spe)
  514. memset(current->thread.evr, 0, ELF_NEVRREG * sizeof(u32));
  515. /* Always get SPEFSCR back */
  516. if (__get_user(current->thread.spefscr, (u32 __user *)&sr->mc_vregs + ELF_NEVRREG))
  517. return 1;
  518. #endif /* CONFIG_SPE */
  519. return 0;
  520. }
  521. #ifdef CONFIG_PPC64
  522. long compat_sys_rt_sigaction(int sig, const struct sigaction32 __user *act,
  523. struct sigaction32 __user *oact, size_t sigsetsize)
  524. {
  525. struct k_sigaction new_ka, old_ka;
  526. int ret;
  527. /* XXX: Don't preclude handling different sized sigset_t's. */
  528. if (sigsetsize != sizeof(compat_sigset_t))
  529. return -EINVAL;
  530. if (act) {
  531. compat_uptr_t handler;
  532. ret = get_user(handler, &act->sa_handler);
  533. new_ka.sa.sa_handler = compat_ptr(handler);
  534. ret |= get_sigset_t(&new_ka.sa.sa_mask, &act->sa_mask);
  535. ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  536. if (ret)
  537. return -EFAULT;
  538. }
  539. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  540. if (!ret && oact) {
  541. ret = put_user(to_user_ptr(old_ka.sa.sa_handler), &oact->sa_handler);
  542. ret |= put_sigset_t(&oact->sa_mask, &old_ka.sa.sa_mask);
  543. ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  544. }
  545. return ret;
  546. }
  547. /*
  548. * Note: it is necessary to treat how as an unsigned int, with the
  549. * corresponding cast to a signed int to insure that the proper
  550. * conversion (sign extension) between the register representation
  551. * of a signed int (msr in 32-bit mode) and the register representation
  552. * of a signed int (msr in 64-bit mode) is performed.
  553. */
  554. long compat_sys_rt_sigprocmask(u32 how, compat_sigset_t __user *set,
  555. compat_sigset_t __user *oset, size_t sigsetsize)
  556. {
  557. sigset_t s;
  558. sigset_t __user *up;
  559. int ret;
  560. mm_segment_t old_fs = get_fs();
  561. if (set) {
  562. if (get_sigset_t(&s, set))
  563. return -EFAULT;
  564. }
  565. set_fs(KERNEL_DS);
  566. /* This is valid because of the set_fs() */
  567. up = (sigset_t __user *) &s;
  568. ret = sys_rt_sigprocmask((int)how, set ? up : NULL, oset ? up : NULL,
  569. sigsetsize);
  570. set_fs(old_fs);
  571. if (ret)
  572. return ret;
  573. if (oset) {
  574. if (put_sigset_t(oset, &s))
  575. return -EFAULT;
  576. }
  577. return 0;
  578. }
  579. long compat_sys_rt_sigpending(compat_sigset_t __user *set, compat_size_t sigsetsize)
  580. {
  581. sigset_t s;
  582. int ret;
  583. mm_segment_t old_fs = get_fs();
  584. set_fs(KERNEL_DS);
  585. /* The __user pointer cast is valid because of the set_fs() */
  586. ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
  587. set_fs(old_fs);
  588. if (!ret) {
  589. if (put_sigset_t(set, &s))
  590. return -EFAULT;
  591. }
  592. return ret;
  593. }
  594. int copy_siginfo_to_user32(struct compat_siginfo __user *d, siginfo_t *s)
  595. {
  596. int err;
  597. if (!access_ok (VERIFY_WRITE, d, sizeof(*d)))
  598. return -EFAULT;
  599. /* If you change siginfo_t structure, please be sure
  600. * this code is fixed accordingly.
  601. * It should never copy any pad contained in the structure
  602. * to avoid security leaks, but must copy the generic
  603. * 3 ints plus the relevant union member.
  604. * This routine must convert siginfo from 64bit to 32bit as well
  605. * at the same time.
  606. */
  607. err = __put_user(s->si_signo, &d->si_signo);
  608. err |= __put_user(s->si_errno, &d->si_errno);
  609. err |= __put_user((short)s->si_code, &d->si_code);
  610. if (s->si_code < 0)
  611. err |= __copy_to_user(&d->_sifields._pad, &s->_sifields._pad,
  612. SI_PAD_SIZE32);
  613. else switch(s->si_code >> 16) {
  614. case __SI_CHLD >> 16:
  615. err |= __put_user(s->si_pid, &d->si_pid);
  616. err |= __put_user(s->si_uid, &d->si_uid);
  617. err |= __put_user(s->si_utime, &d->si_utime);
  618. err |= __put_user(s->si_stime, &d->si_stime);
  619. err |= __put_user(s->si_status, &d->si_status);
  620. break;
  621. case __SI_FAULT >> 16:
  622. err |= __put_user((unsigned int)(unsigned long)s->si_addr,
  623. &d->si_addr);
  624. break;
  625. case __SI_POLL >> 16:
  626. err |= __put_user(s->si_band, &d->si_band);
  627. err |= __put_user(s->si_fd, &d->si_fd);
  628. break;
  629. case __SI_TIMER >> 16:
  630. err |= __put_user(s->si_tid, &d->si_tid);
  631. err |= __put_user(s->si_overrun, &d->si_overrun);
  632. err |= __put_user(s->si_int, &d->si_int);
  633. break;
  634. case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
  635. case __SI_MESGQ >> 16:
  636. err |= __put_user(s->si_int, &d->si_int);
  637. /* fallthrough */
  638. case __SI_KILL >> 16:
  639. default:
  640. err |= __put_user(s->si_pid, &d->si_pid);
  641. err |= __put_user(s->si_uid, &d->si_uid);
  642. break;
  643. }
  644. return err;
  645. }
  646. #define copy_siginfo_to_user copy_siginfo_to_user32
  647. int copy_siginfo_from_user32(siginfo_t *to, struct compat_siginfo __user *from)
  648. {
  649. memset(to, 0, sizeof *to);
  650. if (copy_from_user(to, from, 3*sizeof(int)) ||
  651. copy_from_user(to->_sifields._pad,
  652. from->_sifields._pad, SI_PAD_SIZE32))
  653. return -EFAULT;
  654. return 0;
  655. }
  656. /*
  657. * Note: it is necessary to treat pid and sig as unsigned ints, with the
  658. * corresponding cast to a signed int to insure that the proper conversion
  659. * (sign extension) between the register representation of a signed int
  660. * (msr in 32-bit mode) and the register representation of a signed int
  661. * (msr in 64-bit mode) is performed.
  662. */
  663. long compat_sys_rt_sigqueueinfo(u32 pid, u32 sig, compat_siginfo_t __user *uinfo)
  664. {
  665. siginfo_t info;
  666. int ret;
  667. mm_segment_t old_fs = get_fs();
  668. ret = copy_siginfo_from_user32(&info, uinfo);
  669. if (unlikely(ret))
  670. return ret;
  671. set_fs (KERNEL_DS);
  672. /* The __user pointer cast is valid becasuse of the set_fs() */
  673. ret = sys_rt_sigqueueinfo((int)pid, (int)sig, (siginfo_t __user *) &info);
  674. set_fs (old_fs);
  675. return ret;
  676. }
  677. /*
  678. * Start Alternate signal stack support
  679. *
  680. * System Calls
  681. * sigaltatck compat_sys_sigaltstack
  682. */
  683. int compat_sys_sigaltstack(u32 __new, u32 __old, int r5,
  684. int r6, int r7, int r8, struct pt_regs *regs)
  685. {
  686. stack_32_t __user * newstack = compat_ptr(__new);
  687. stack_32_t __user * oldstack = compat_ptr(__old);
  688. stack_t uss, uoss;
  689. int ret;
  690. mm_segment_t old_fs;
  691. unsigned long sp;
  692. compat_uptr_t ss_sp;
  693. /*
  694. * set sp to the user stack on entry to the system call
  695. * the system call router sets R9 to the saved registers
  696. */
  697. sp = regs->gpr[1];
  698. /* Put new stack info in local 64 bit stack struct */
  699. if (newstack) {
  700. if (get_user(ss_sp, &newstack->ss_sp) ||
  701. __get_user(uss.ss_flags, &newstack->ss_flags) ||
  702. __get_user(uss.ss_size, &newstack->ss_size))
  703. return -EFAULT;
  704. uss.ss_sp = compat_ptr(ss_sp);
  705. }
  706. old_fs = get_fs();
  707. set_fs(KERNEL_DS);
  708. /* The __user pointer casts are valid because of the set_fs() */
  709. ret = do_sigaltstack(
  710. newstack ? (stack_t __user *) &uss : NULL,
  711. oldstack ? (stack_t __user *) &uoss : NULL,
  712. sp);
  713. set_fs(old_fs);
  714. /* Copy the stack information to the user output buffer */
  715. if (!ret && oldstack &&
  716. (put_user(ptr_to_compat(uoss.ss_sp), &oldstack->ss_sp) ||
  717. __put_user(uoss.ss_flags, &oldstack->ss_flags) ||
  718. __put_user(uoss.ss_size, &oldstack->ss_size)))
  719. return -EFAULT;
  720. return ret;
  721. }
  722. #endif /* CONFIG_PPC64 */
  723. /*
  724. * Set up a signal frame for a "real-time" signal handler
  725. * (one which gets siginfo).
  726. */
  727. int handle_rt_signal32(unsigned long sig, struct k_sigaction *ka,
  728. siginfo_t *info, sigset_t *oldset,
  729. struct pt_regs *regs)
  730. {
  731. struct rt_sigframe __user *rt_sf;
  732. struct mcontext __user *frame;
  733. void __user *addr;
  734. unsigned long newsp = 0;
  735. /* Set up Signal Frame */
  736. /* Put a Real Time Context onto stack */
  737. rt_sf = get_sigframe(ka, regs, sizeof(*rt_sf), 1);
  738. addr = rt_sf;
  739. if (unlikely(rt_sf == NULL))
  740. goto badframe;
  741. /* Put the siginfo & fill in most of the ucontext */
  742. if (copy_siginfo_to_user(&rt_sf->info, info)
  743. || __put_user(0, &rt_sf->uc.uc_flags)
  744. || __put_user(0, &rt_sf->uc.uc_link)
  745. || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
  746. || __put_user(sas_ss_flags(regs->gpr[1]),
  747. &rt_sf->uc.uc_stack.ss_flags)
  748. || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
  749. || __put_user(to_user_ptr(&rt_sf->uc.uc_mcontext),
  750. &rt_sf->uc.uc_regs)
  751. || put_sigset_t(&rt_sf->uc.uc_sigmask, oldset))
  752. goto badframe;
  753. /* Save user registers on the stack */
  754. frame = &rt_sf->uc.uc_mcontext;
  755. addr = frame;
  756. if (vdso32_rt_sigtramp && current->mm->context.vdso_base) {
  757. if (save_user_regs(regs, frame, 0, 1))
  758. goto badframe;
  759. regs->link = current->mm->context.vdso_base + vdso32_rt_sigtramp;
  760. } else {
  761. if (save_user_regs(regs, frame, __NR_rt_sigreturn, 1))
  762. goto badframe;
  763. regs->link = (unsigned long) frame->tramp;
  764. }
  765. current->thread.fpscr.val = 0; /* turn off all fp exceptions */
  766. /* create a stack frame for the caller of the handler */
  767. newsp = ((unsigned long)rt_sf) - (__SIGNAL_FRAMESIZE + 16);
  768. addr = (void __user *)regs->gpr[1];
  769. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  770. goto badframe;
  771. /* Fill registers for signal handler */
  772. regs->gpr[1] = newsp;
  773. regs->gpr[3] = sig;
  774. regs->gpr[4] = (unsigned long) &rt_sf->info;
  775. regs->gpr[5] = (unsigned long) &rt_sf->uc;
  776. regs->gpr[6] = (unsigned long) rt_sf;
  777. regs->nip = (unsigned long) ka->sa.sa_handler;
  778. /* enter the signal handler in big-endian mode */
  779. regs->msr &= ~MSR_LE;
  780. return 1;
  781. badframe:
  782. #ifdef DEBUG_SIG
  783. printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
  784. regs, frame, newsp);
  785. #endif
  786. if (show_unhandled_signals)
  787. printk_ratelimited(KERN_INFO
  788. "%s[%d]: bad frame in handle_rt_signal32: "
  789. "%p nip %08lx lr %08lx\n",
  790. current->comm, current->pid,
  791. addr, regs->nip, regs->link);
  792. force_sigsegv(sig, current);
  793. return 0;
  794. }
  795. static int do_setcontext(struct ucontext __user *ucp, struct pt_regs *regs, int sig)
  796. {
  797. sigset_t set;
  798. struct mcontext __user *mcp;
  799. if (get_sigset_t(&set, &ucp->uc_sigmask))
  800. return -EFAULT;
  801. #ifdef CONFIG_PPC64
  802. {
  803. u32 cmcp;
  804. if (__get_user(cmcp, &ucp->uc_regs))
  805. return -EFAULT;
  806. mcp = (struct mcontext __user *)(u64)cmcp;
  807. /* no need to check access_ok(mcp), since mcp < 4GB */
  808. }
  809. #else
  810. if (__get_user(mcp, &ucp->uc_regs))
  811. return -EFAULT;
  812. if (!access_ok(VERIFY_READ, mcp, sizeof(*mcp)))
  813. return -EFAULT;
  814. #endif
  815. set_current_blocked(&set);
  816. if (restore_user_regs(regs, mcp, sig))
  817. return -EFAULT;
  818. return 0;
  819. }
  820. long sys_swapcontext(struct ucontext __user *old_ctx,
  821. struct ucontext __user *new_ctx,
  822. int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
  823. {
  824. unsigned char tmp;
  825. int ctx_has_vsx_region = 0;
  826. #ifdef CONFIG_PPC64
  827. unsigned long new_msr = 0;
  828. if (new_ctx) {
  829. struct mcontext __user *mcp;
  830. u32 cmcp;
  831. /*
  832. * Get pointer to the real mcontext. No need for
  833. * access_ok since we are dealing with compat
  834. * pointers.
  835. */
  836. if (__get_user(cmcp, &new_ctx->uc_regs))
  837. return -EFAULT;
  838. mcp = (struct mcontext __user *)(u64)cmcp;
  839. if (__get_user(new_msr, &mcp->mc_gregs[PT_MSR]))
  840. return -EFAULT;
  841. }
  842. /*
  843. * Check that the context is not smaller than the original
  844. * size (with VMX but without VSX)
  845. */
  846. if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
  847. return -EINVAL;
  848. /*
  849. * If the new context state sets the MSR VSX bits but
  850. * it doesn't provide VSX state.
  851. */
  852. if ((ctx_size < sizeof(struct ucontext)) &&
  853. (new_msr & MSR_VSX))
  854. return -EINVAL;
  855. /* Does the context have enough room to store VSX data? */
  856. if (ctx_size >= sizeof(struct ucontext))
  857. ctx_has_vsx_region = 1;
  858. #else
  859. /* Context size is for future use. Right now, we only make sure
  860. * we are passed something we understand
  861. */
  862. if (ctx_size < sizeof(struct ucontext))
  863. return -EINVAL;
  864. #endif
  865. if (old_ctx != NULL) {
  866. struct mcontext __user *mctx;
  867. /*
  868. * old_ctx might not be 16-byte aligned, in which
  869. * case old_ctx->uc_mcontext won't be either.
  870. * Because we have the old_ctx->uc_pad2 field
  871. * before old_ctx->uc_mcontext, we need to round down
  872. * from &old_ctx->uc_mcontext to a 16-byte boundary.
  873. */
  874. mctx = (struct mcontext __user *)
  875. ((unsigned long) &old_ctx->uc_mcontext & ~0xfUL);
  876. if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
  877. || save_user_regs(regs, mctx, 0, ctx_has_vsx_region)
  878. || put_sigset_t(&old_ctx->uc_sigmask, &current->blocked)
  879. || __put_user(to_user_ptr(mctx), &old_ctx->uc_regs))
  880. return -EFAULT;
  881. }
  882. if (new_ctx == NULL)
  883. return 0;
  884. if (!access_ok(VERIFY_READ, new_ctx, ctx_size)
  885. || __get_user(tmp, (u8 __user *) new_ctx)
  886. || __get_user(tmp, (u8 __user *) new_ctx + ctx_size - 1))
  887. return -EFAULT;
  888. /*
  889. * If we get a fault copying the context into the kernel's
  890. * image of the user's registers, we can't just return -EFAULT
  891. * because the user's registers will be corrupted. For instance
  892. * the NIP value may have been updated but not some of the
  893. * other registers. Given that we have done the access_ok
  894. * and successfully read the first and last bytes of the region
  895. * above, this should only happen in an out-of-memory situation
  896. * or if another thread unmaps the region containing the context.
  897. * We kill the task with a SIGSEGV in this situation.
  898. */
  899. if (do_setcontext(new_ctx, regs, 0))
  900. do_exit(SIGSEGV);
  901. set_thread_flag(TIF_RESTOREALL);
  902. return 0;
  903. }
  904. long sys_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
  905. struct pt_regs *regs)
  906. {
  907. struct rt_sigframe __user *rt_sf;
  908. /* Always make any pending restarted system calls return -EINTR */
  909. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  910. rt_sf = (struct rt_sigframe __user *)
  911. (regs->gpr[1] + __SIGNAL_FRAMESIZE + 16);
  912. if (!access_ok(VERIFY_READ, rt_sf, sizeof(*rt_sf)))
  913. goto bad;
  914. if (do_setcontext(&rt_sf->uc, regs, 1))
  915. goto bad;
  916. /*
  917. * It's not clear whether or why it is desirable to save the
  918. * sigaltstack setting on signal delivery and restore it on
  919. * signal return. But other architectures do this and we have
  920. * always done it up until now so it is probably better not to
  921. * change it. -- paulus
  922. */
  923. #ifdef CONFIG_PPC64
  924. /*
  925. * We use the compat_sys_ version that does the 32/64 bits conversion
  926. * and takes userland pointer directly. What about error checking ?
  927. * nobody does any...
  928. */
  929. compat_sys_sigaltstack((u32)(u64)&rt_sf->uc.uc_stack, 0, 0, 0, 0, 0, regs);
  930. #else
  931. do_sigaltstack(&rt_sf->uc.uc_stack, NULL, regs->gpr[1]);
  932. #endif
  933. set_thread_flag(TIF_RESTOREALL);
  934. return 0;
  935. bad:
  936. if (show_unhandled_signals)
  937. printk_ratelimited(KERN_INFO
  938. "%s[%d]: bad frame in sys_rt_sigreturn: "
  939. "%p nip %08lx lr %08lx\n",
  940. current->comm, current->pid,
  941. rt_sf, regs->nip, regs->link);
  942. force_sig(SIGSEGV, current);
  943. return 0;
  944. }
  945. #ifdef CONFIG_PPC32
  946. int sys_debug_setcontext(struct ucontext __user *ctx,
  947. int ndbg, struct sig_dbg_op __user *dbg,
  948. int r6, int r7, int r8,
  949. struct pt_regs *regs)
  950. {
  951. struct sig_dbg_op op;
  952. int i;
  953. unsigned char tmp;
  954. unsigned long new_msr = regs->msr;
  955. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  956. unsigned long new_dbcr0 = current->thread.dbcr0;
  957. #endif
  958. for (i=0; i<ndbg; i++) {
  959. if (copy_from_user(&op, dbg + i, sizeof(op)))
  960. return -EFAULT;
  961. switch (op.dbg_type) {
  962. case SIG_DBG_SINGLE_STEPPING:
  963. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  964. if (op.dbg_value) {
  965. new_msr |= MSR_DE;
  966. new_dbcr0 |= (DBCR0_IDM | DBCR0_IC);
  967. } else {
  968. new_dbcr0 &= ~DBCR0_IC;
  969. if (!DBCR_ACTIVE_EVENTS(new_dbcr0,
  970. current->thread.dbcr1)) {
  971. new_msr &= ~MSR_DE;
  972. new_dbcr0 &= ~DBCR0_IDM;
  973. }
  974. }
  975. #else
  976. if (op.dbg_value)
  977. new_msr |= MSR_SE;
  978. else
  979. new_msr &= ~MSR_SE;
  980. #endif
  981. break;
  982. case SIG_DBG_BRANCH_TRACING:
  983. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  984. return -EINVAL;
  985. #else
  986. if (op.dbg_value)
  987. new_msr |= MSR_BE;
  988. else
  989. new_msr &= ~MSR_BE;
  990. #endif
  991. break;
  992. default:
  993. return -EINVAL;
  994. }
  995. }
  996. /* We wait until here to actually install the values in the
  997. registers so if we fail in the above loop, it will not
  998. affect the contents of these registers. After this point,
  999. failure is a problem, anyway, and it's very unlikely unless
  1000. the user is really doing something wrong. */
  1001. regs->msr = new_msr;
  1002. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1003. current->thread.dbcr0 = new_dbcr0;
  1004. #endif
  1005. if (!access_ok(VERIFY_READ, ctx, sizeof(*ctx))
  1006. || __get_user(tmp, (u8 __user *) ctx)
  1007. || __get_user(tmp, (u8 __user *) (ctx + 1) - 1))
  1008. return -EFAULT;
  1009. /*
  1010. * If we get a fault copying the context into the kernel's
  1011. * image of the user's registers, we can't just return -EFAULT
  1012. * because the user's registers will be corrupted. For instance
  1013. * the NIP value may have been updated but not some of the
  1014. * other registers. Given that we have done the access_ok
  1015. * and successfully read the first and last bytes of the region
  1016. * above, this should only happen in an out-of-memory situation
  1017. * or if another thread unmaps the region containing the context.
  1018. * We kill the task with a SIGSEGV in this situation.
  1019. */
  1020. if (do_setcontext(ctx, regs, 1)) {
  1021. if (show_unhandled_signals)
  1022. printk_ratelimited(KERN_INFO "%s[%d]: bad frame in "
  1023. "sys_debug_setcontext: %p nip %08lx "
  1024. "lr %08lx\n",
  1025. current->comm, current->pid,
  1026. ctx, regs->nip, regs->link);
  1027. force_sig(SIGSEGV, current);
  1028. goto out;
  1029. }
  1030. /*
  1031. * It's not clear whether or why it is desirable to save the
  1032. * sigaltstack setting on signal delivery and restore it on
  1033. * signal return. But other architectures do this and we have
  1034. * always done it up until now so it is probably better not to
  1035. * change it. -- paulus
  1036. */
  1037. do_sigaltstack(&ctx->uc_stack, NULL, regs->gpr[1]);
  1038. set_thread_flag(TIF_RESTOREALL);
  1039. out:
  1040. return 0;
  1041. }
  1042. #endif
  1043. /*
  1044. * OK, we're invoking a handler
  1045. */
  1046. int handle_signal32(unsigned long sig, struct k_sigaction *ka,
  1047. siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
  1048. {
  1049. struct sigcontext __user *sc;
  1050. struct sigframe __user *frame;
  1051. unsigned long newsp = 0;
  1052. /* Set up Signal Frame */
  1053. frame = get_sigframe(ka, regs, sizeof(*frame), 1);
  1054. if (unlikely(frame == NULL))
  1055. goto badframe;
  1056. sc = (struct sigcontext __user *) &frame->sctx;
  1057. #if _NSIG != 64
  1058. #error "Please adjust handle_signal()"
  1059. #endif
  1060. if (__put_user(to_user_ptr(ka->sa.sa_handler), &sc->handler)
  1061. || __put_user(oldset->sig[0], &sc->oldmask)
  1062. #ifdef CONFIG_PPC64
  1063. || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
  1064. #else
  1065. || __put_user(oldset->sig[1], &sc->_unused[3])
  1066. #endif
  1067. || __put_user(to_user_ptr(&frame->mctx), &sc->regs)
  1068. || __put_user(sig, &sc->signal))
  1069. goto badframe;
  1070. if (vdso32_sigtramp && current->mm->context.vdso_base) {
  1071. if (save_user_regs(regs, &frame->mctx, 0, 1))
  1072. goto badframe;
  1073. regs->link = current->mm->context.vdso_base + vdso32_sigtramp;
  1074. } else {
  1075. if (save_user_regs(regs, &frame->mctx, __NR_sigreturn, 1))
  1076. goto badframe;
  1077. regs->link = (unsigned long) frame->mctx.tramp;
  1078. }
  1079. current->thread.fpscr.val = 0; /* turn off all fp exceptions */
  1080. /* create a stack frame for the caller of the handler */
  1081. newsp = ((unsigned long)frame) - __SIGNAL_FRAMESIZE;
  1082. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  1083. goto badframe;
  1084. regs->gpr[1] = newsp;
  1085. regs->gpr[3] = sig;
  1086. regs->gpr[4] = (unsigned long) sc;
  1087. regs->nip = (unsigned long) ka->sa.sa_handler;
  1088. /* enter the signal handler in big-endian mode */
  1089. regs->msr &= ~MSR_LE;
  1090. return 1;
  1091. badframe:
  1092. #ifdef DEBUG_SIG
  1093. printk("badframe in handle_signal, regs=%p frame=%p newsp=%lx\n",
  1094. regs, frame, newsp);
  1095. #endif
  1096. if (show_unhandled_signals)
  1097. printk_ratelimited(KERN_INFO
  1098. "%s[%d]: bad frame in handle_signal32: "
  1099. "%p nip %08lx lr %08lx\n",
  1100. current->comm, current->pid,
  1101. frame, regs->nip, regs->link);
  1102. force_sigsegv(sig, current);
  1103. return 0;
  1104. }
  1105. /*
  1106. * Do a signal return; undo the signal stack.
  1107. */
  1108. long sys_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
  1109. struct pt_regs *regs)
  1110. {
  1111. struct sigcontext __user *sc;
  1112. struct sigcontext sigctx;
  1113. struct mcontext __user *sr;
  1114. void __user *addr;
  1115. sigset_t set;
  1116. /* Always make any pending restarted system calls return -EINTR */
  1117. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  1118. sc = (struct sigcontext __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE);
  1119. addr = sc;
  1120. if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
  1121. goto badframe;
  1122. #ifdef CONFIG_PPC64
  1123. /*
  1124. * Note that PPC32 puts the upper 32 bits of the sigmask in the
  1125. * unused part of the signal stackframe
  1126. */
  1127. set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3]) << 32);
  1128. #else
  1129. set.sig[0] = sigctx.oldmask;
  1130. set.sig[1] = sigctx._unused[3];
  1131. #endif
  1132. set_current_blocked(&set);
  1133. sr = (struct mcontext __user *)from_user_ptr(sigctx.regs);
  1134. addr = sr;
  1135. if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
  1136. || restore_user_regs(regs, sr, 1))
  1137. goto badframe;
  1138. set_thread_flag(TIF_RESTOREALL);
  1139. return 0;
  1140. badframe:
  1141. if (show_unhandled_signals)
  1142. printk_ratelimited(KERN_INFO
  1143. "%s[%d]: bad frame in sys_sigreturn: "
  1144. "%p nip %08lx lr %08lx\n",
  1145. current->comm, current->pid,
  1146. addr, regs->nip, regs->link);
  1147. force_sig(SIGSEGV, current);
  1148. return 0;
  1149. }