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