signal32.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /*
  2. * signal32.c: Support 32bit signal syscalls.
  3. *
  4. * Copyright (C) 2001 IBM
  5. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  7. *
  8. * These routines maintain argument size conversion between 32bit and 64bit
  9. * environment.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/config.h>
  17. #include <linux/sched.h>
  18. #include <linux/mm.h>
  19. #include <linux/smp.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/kernel.h>
  22. #include <linux/signal.h>
  23. #include <linux/syscalls.h>
  24. #include <linux/errno.h>
  25. #include <linux/elf.h>
  26. #include <linux/compat.h>
  27. #include <linux/ptrace.h>
  28. #include <asm/ppc32.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/ppcdebug.h>
  31. #include <asm/unistd.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/vdso.h>
  34. #define DEBUG_SIG 0
  35. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  36. #define GP_REGS_SIZE32 min(sizeof(elf_gregset_t32), sizeof(struct pt_regs32))
  37. /*
  38. * When we have signals to deliver, we set up on the
  39. * user stack, going down from the original stack pointer:
  40. * a sigregs32 struct
  41. * a sigcontext32 struct
  42. * a gap of __SIGNAL_FRAMESIZE32 bytes
  43. *
  44. * Each of these things must be a multiple of 16 bytes in size.
  45. *
  46. */
  47. struct sigregs32 {
  48. struct mcontext32 mctx; /* all the register values */
  49. /*
  50. * Programs using the rs6000/xcoff abi can save up to 19 gp
  51. * regs and 18 fp regs below sp before decrementing it.
  52. */
  53. int abigap[56];
  54. };
  55. /* We use the mc_pad field for the signal return trampoline. */
  56. #define tramp mc_pad
  57. /*
  58. * When we have rt signals to deliver, we set up on the
  59. * user stack, going down from the original stack pointer:
  60. * one rt_sigframe32 struct (siginfo + ucontext + ABI gap)
  61. * a gap of __SIGNAL_FRAMESIZE32+16 bytes
  62. * (the +16 is to get the siginfo and ucontext32 in the same
  63. * positions as in older kernels).
  64. *
  65. * Each of these things must be a multiple of 16 bytes in size.
  66. *
  67. */
  68. struct rt_sigframe32 {
  69. compat_siginfo_t info;
  70. struct ucontext32 uc;
  71. /*
  72. * Programs using the rs6000/xcoff abi can save up to 19 gp
  73. * regs and 18 fp regs below sp before decrementing it.
  74. */
  75. int abigap[56];
  76. };
  77. /*
  78. * Common utility functions used by signal and context support
  79. *
  80. */
  81. /*
  82. * Restore the user process's signal mask
  83. * (implemented in signal.c)
  84. */
  85. extern void restore_sigmask(sigset_t *set);
  86. /*
  87. * Functions for flipping sigsets (thanks to brain dead generic
  88. * implementation that makes things simple for little endian only
  89. */
  90. static inline void compat_from_sigset(compat_sigset_t *compat, sigset_t *set)
  91. {
  92. switch (_NSIG_WORDS) {
  93. case 4: compat->sig[5] = set->sig[3] & 0xffffffffull ;
  94. compat->sig[7] = set->sig[3] >> 32;
  95. case 3: compat->sig[4] = set->sig[2] & 0xffffffffull ;
  96. compat->sig[5] = set->sig[2] >> 32;
  97. case 2: compat->sig[2] = set->sig[1] & 0xffffffffull ;
  98. compat->sig[3] = set->sig[1] >> 32;
  99. case 1: compat->sig[0] = set->sig[0] & 0xffffffffull ;
  100. compat->sig[1] = set->sig[0] >> 32;
  101. }
  102. }
  103. static inline void sigset_from_compat(sigset_t *set, compat_sigset_t *compat)
  104. {
  105. switch (_NSIG_WORDS) {
  106. case 4: set->sig[3] = compat->sig[6] | (((long)compat->sig[7]) << 32);
  107. case 3: set->sig[2] = compat->sig[4] | (((long)compat->sig[5]) << 32);
  108. case 2: set->sig[1] = compat->sig[2] | (((long)compat->sig[3]) << 32);
  109. case 1: set->sig[0] = compat->sig[0] | (((long)compat->sig[1]) << 32);
  110. }
  111. }
  112. /*
  113. * Save the current user registers on the user stack.
  114. * We only save the altivec registers if the process has used
  115. * altivec instructions at some point.
  116. */
  117. static int save_user_regs(struct pt_regs *regs, struct mcontext32 __user *frame, int sigret)
  118. {
  119. elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
  120. int i, err = 0;
  121. /* Make sure floating point registers are stored in regs */
  122. flush_fp_to_thread(current);
  123. /* save general and floating-point registers */
  124. for (i = 0; i <= PT_RESULT; i ++)
  125. err |= __put_user((unsigned int)gregs[i], &frame->mc_gregs[i]);
  126. err |= __copy_to_user(&frame->mc_fregs, current->thread.fpr,
  127. ELF_NFPREG * sizeof(double));
  128. if (err)
  129. return 1;
  130. current->thread.fpscr = 0; /* turn off all fp exceptions */
  131. #ifdef CONFIG_ALTIVEC
  132. /* save altivec registers */
  133. if (current->thread.used_vr) {
  134. flush_altivec_to_thread(current);
  135. if (__copy_to_user(&frame->mc_vregs, current->thread.vr,
  136. ELF_NVRREG32 * sizeof(vector128)))
  137. return 1;
  138. /* set MSR_VEC in the saved MSR value to indicate that
  139. frame->mc_vregs contains valid data */
  140. if (__put_user(regs->msr | MSR_VEC, &frame->mc_gregs[PT_MSR]))
  141. return 1;
  142. }
  143. /* else assert((regs->msr & MSR_VEC) == 0) */
  144. /* We always copy to/from vrsave, it's 0 if we don't have or don't
  145. * use altivec. Since VSCR only contains 32 bits saved in the least
  146. * significant bits of a vector, we "cheat" and stuff VRSAVE in the
  147. * most significant bits of that same vector. --BenH
  148. */
  149. if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
  150. return 1;
  151. #endif /* CONFIG_ALTIVEC */
  152. if (sigret) {
  153. /* Set up the sigreturn trampoline: li r0,sigret; sc */
  154. if (__put_user(0x38000000UL + sigret, &frame->tramp[0])
  155. || __put_user(0x44000002UL, &frame->tramp[1]))
  156. return 1;
  157. flush_icache_range((unsigned long) &frame->tramp[0],
  158. (unsigned long) &frame->tramp[2]);
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Restore the current user register values from the user stack,
  164. * (except for MSR).
  165. */
  166. static long restore_user_regs(struct pt_regs *regs,
  167. struct mcontext32 __user *sr, int sig)
  168. {
  169. elf_greg_t64 *gregs = (elf_greg_t64 *)regs;
  170. int i;
  171. long err = 0;
  172. unsigned int save_r2 = 0;
  173. #ifdef CONFIG_ALTIVEC
  174. unsigned long msr;
  175. #endif
  176. /*
  177. * restore general registers but not including MSR or SOFTE. Also
  178. * take care of keeping r2 (TLS) intact if not a signal
  179. */
  180. if (!sig)
  181. save_r2 = (unsigned int)regs->gpr[2];
  182. for (i = 0; i <= PT_RESULT; i++) {
  183. if ((i == PT_MSR) || (i == PT_SOFTE))
  184. continue;
  185. err |= __get_user(gregs[i], &sr->mc_gregs[i]);
  186. }
  187. if (!sig)
  188. regs->gpr[2] = (unsigned long) save_r2;
  189. if (err)
  190. return 1;
  191. /* force the process to reload the FP registers from
  192. current->thread when it next does FP instructions */
  193. regs->msr &= ~(MSR_FP | MSR_FE0 | MSR_FE1);
  194. if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
  195. sizeof(sr->mc_fregs)))
  196. return 1;
  197. #ifdef CONFIG_ALTIVEC
  198. /* force the process to reload the altivec registers from
  199. current->thread when it next does altivec instructions */
  200. regs->msr &= ~MSR_VEC;
  201. if (!__get_user(msr, &sr->mc_gregs[PT_MSR]) && (msr & MSR_VEC) != 0) {
  202. /* restore altivec registers from the stack */
  203. if (__copy_from_user(current->thread.vr, &sr->mc_vregs,
  204. sizeof(sr->mc_vregs)))
  205. return 1;
  206. } else if (current->thread.used_vr)
  207. memset(current->thread.vr, 0, ELF_NVRREG32 * sizeof(vector128));
  208. /* Always get VRSAVE back */
  209. if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
  210. return 1;
  211. #endif /* CONFIG_ALTIVEC */
  212. #ifndef CONFIG_SMP
  213. preempt_disable();
  214. if (last_task_used_math == current)
  215. last_task_used_math = NULL;
  216. if (last_task_used_altivec == current)
  217. last_task_used_altivec = NULL;
  218. preempt_enable();
  219. #endif
  220. return 0;
  221. }
  222. /*
  223. * Start of nonRT signal support
  224. *
  225. * sigset_t is 32 bits for non-rt signals
  226. *
  227. * System Calls
  228. * sigaction sys32_sigaction
  229. * sigreturn sys32_sigreturn
  230. *
  231. * Note sigsuspend has no special 32 bit routine - uses the 64 bit routine
  232. *
  233. * Other routines
  234. * setup_frame32
  235. */
  236. /*
  237. * Atomically swap in the new signal mask, and wait for a signal.
  238. */
  239. long sys32_sigsuspend(old_sigset_t mask, int p2, int p3, int p4, int p6, int p7,
  240. struct pt_regs *regs)
  241. {
  242. sigset_t saveset;
  243. mask &= _BLOCKABLE;
  244. spin_lock_irq(&current->sighand->siglock);
  245. saveset = current->blocked;
  246. siginitset(&current->blocked, mask);
  247. recalc_sigpending();
  248. spin_unlock_irq(&current->sighand->siglock);
  249. regs->result = -EINTR;
  250. regs->gpr[3] = EINTR;
  251. regs->ccr |= 0x10000000;
  252. while (1) {
  253. current->state = TASK_INTERRUPTIBLE;
  254. schedule();
  255. if (do_signal32(&saveset, regs))
  256. /*
  257. * Returning 0 means we return to userspace via
  258. * ret_from_except and thus restore all user
  259. * registers from *regs. This is what we need
  260. * to do when a signal has been delivered.
  261. */
  262. return 0;
  263. }
  264. }
  265. long sys32_sigaction(int sig, struct old_sigaction32 __user *act,
  266. struct old_sigaction32 __user *oact)
  267. {
  268. struct k_sigaction new_ka, old_ka;
  269. int ret;
  270. if (sig < 0)
  271. sig = -sig;
  272. if (act) {
  273. compat_old_sigset_t mask;
  274. compat_uptr_t handler, restorer;
  275. if (get_user(handler, &act->sa_handler) ||
  276. __get_user(restorer, &act->sa_restorer) ||
  277. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  278. __get_user(mask, &act->sa_mask))
  279. return -EFAULT;
  280. new_ka.sa.sa_handler = compat_ptr(handler);
  281. new_ka.sa.sa_restorer = compat_ptr(restorer);
  282. siginitset(&new_ka.sa.sa_mask, mask);
  283. }
  284. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  285. if (!ret && oact) {
  286. if (put_user((long)old_ka.sa.sa_handler, &oact->sa_handler) ||
  287. __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer) ||
  288. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  289. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  290. return -EFAULT;
  291. }
  292. return ret;
  293. }
  294. /*
  295. * Start of RT signal support
  296. *
  297. * sigset_t is 64 bits for rt signals
  298. *
  299. * System Calls
  300. * sigaction sys32_rt_sigaction
  301. * sigpending sys32_rt_sigpending
  302. * sigprocmask sys32_rt_sigprocmask
  303. * sigreturn sys32_rt_sigreturn
  304. * sigqueueinfo sys32_rt_sigqueueinfo
  305. * sigsuspend sys32_rt_sigsuspend
  306. *
  307. * Other routines
  308. * setup_rt_frame32
  309. * copy_siginfo_to_user32
  310. * siginfo32to64
  311. */
  312. long sys32_rt_sigaction(int sig, const struct sigaction32 __user *act,
  313. struct sigaction32 __user *oact, size_t sigsetsize)
  314. {
  315. struct k_sigaction new_ka, old_ka;
  316. int ret;
  317. compat_sigset_t set32;
  318. /* XXX: Don't preclude handling different sized sigset_t's. */
  319. if (sigsetsize != sizeof(compat_sigset_t))
  320. return -EINVAL;
  321. if (act) {
  322. compat_uptr_t handler;
  323. ret = get_user(handler, &act->sa_handler);
  324. new_ka.sa.sa_handler = compat_ptr(handler);
  325. ret |= __copy_from_user(&set32, &act->sa_mask,
  326. sizeof(compat_sigset_t));
  327. sigset_from_compat(&new_ka.sa.sa_mask, &set32);
  328. ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  329. if (ret)
  330. return -EFAULT;
  331. }
  332. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  333. if (!ret && oact) {
  334. compat_from_sigset(&set32, &old_ka.sa.sa_mask);
  335. ret = put_user((long)old_ka.sa.sa_handler, &oact->sa_handler);
  336. ret |= __copy_to_user(&oact->sa_mask, &set32,
  337. sizeof(compat_sigset_t));
  338. ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  339. }
  340. return ret;
  341. }
  342. /*
  343. * Note: it is necessary to treat how as an unsigned int, with the
  344. * corresponding cast to a signed int to insure that the proper
  345. * conversion (sign extension) between the register representation
  346. * of a signed int (msr in 32-bit mode) and the register representation
  347. * of a signed int (msr in 64-bit mode) is performed.
  348. */
  349. long sys32_rt_sigprocmask(u32 how, compat_sigset_t __user *set,
  350. compat_sigset_t __user *oset, size_t sigsetsize)
  351. {
  352. sigset_t s;
  353. sigset_t __user *up;
  354. compat_sigset_t s32;
  355. int ret;
  356. mm_segment_t old_fs = get_fs();
  357. if (set) {
  358. if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
  359. return -EFAULT;
  360. sigset_from_compat(&s, &s32);
  361. }
  362. set_fs(KERNEL_DS);
  363. /* This is valid because of the set_fs() */
  364. up = (sigset_t __user *) &s;
  365. ret = sys_rt_sigprocmask((int)how, set ? up : NULL, oset ? up : NULL,
  366. sigsetsize);
  367. set_fs(old_fs);
  368. if (ret)
  369. return ret;
  370. if (oset) {
  371. compat_from_sigset(&s32, &s);
  372. if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
  373. return -EFAULT;
  374. }
  375. return 0;
  376. }
  377. long sys32_rt_sigpending(compat_sigset_t __user *set, compat_size_t sigsetsize)
  378. {
  379. sigset_t s;
  380. compat_sigset_t s32;
  381. int ret;
  382. mm_segment_t old_fs = get_fs();
  383. set_fs(KERNEL_DS);
  384. /* The __user pointer cast is valid because of the set_fs() */
  385. ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
  386. set_fs(old_fs);
  387. if (!ret) {
  388. compat_from_sigset(&s32, &s);
  389. if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
  390. return -EFAULT;
  391. }
  392. return ret;
  393. }
  394. int copy_siginfo_to_user32(struct compat_siginfo __user *d, siginfo_t *s)
  395. {
  396. int err;
  397. if (!access_ok (VERIFY_WRITE, d, sizeof(*d)))
  398. return -EFAULT;
  399. /* If you change siginfo_t structure, please be sure
  400. * this code is fixed accordingly.
  401. * It should never copy any pad contained in the structure
  402. * to avoid security leaks, but must copy the generic
  403. * 3 ints plus the relevant union member.
  404. * This routine must convert siginfo from 64bit to 32bit as well
  405. * at the same time.
  406. */
  407. err = __put_user(s->si_signo, &d->si_signo);
  408. err |= __put_user(s->si_errno, &d->si_errno);
  409. err |= __put_user((short)s->si_code, &d->si_code);
  410. if (s->si_code < 0)
  411. err |= __copy_to_user(&d->_sifields._pad, &s->_sifields._pad,
  412. SI_PAD_SIZE32);
  413. else switch(s->si_code >> 16) {
  414. case __SI_CHLD >> 16:
  415. err |= __put_user(s->si_pid, &d->si_pid);
  416. err |= __put_user(s->si_uid, &d->si_uid);
  417. err |= __put_user(s->si_utime, &d->si_utime);
  418. err |= __put_user(s->si_stime, &d->si_stime);
  419. err |= __put_user(s->si_status, &d->si_status);
  420. break;
  421. case __SI_FAULT >> 16:
  422. err |= __put_user((unsigned int)(unsigned long)s->si_addr,
  423. &d->si_addr);
  424. break;
  425. case __SI_POLL >> 16:
  426. err |= __put_user(s->si_band, &d->si_band);
  427. err |= __put_user(s->si_fd, &d->si_fd);
  428. break;
  429. case __SI_TIMER >> 16:
  430. err |= __put_user(s->si_tid, &d->si_tid);
  431. err |= __put_user(s->si_overrun, &d->si_overrun);
  432. err |= __put_user(s->si_int, &d->si_int);
  433. break;
  434. case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
  435. case __SI_MESGQ >> 16:
  436. err |= __put_user(s->si_int, &d->si_int);
  437. /* fallthrough */
  438. case __SI_KILL >> 16:
  439. default:
  440. err |= __put_user(s->si_pid, &d->si_pid);
  441. err |= __put_user(s->si_uid, &d->si_uid);
  442. break;
  443. }
  444. return err;
  445. }
  446. /*
  447. * Note: it is necessary to treat pid and sig as unsigned ints, with the
  448. * corresponding cast to a signed int to insure that the proper conversion
  449. * (sign extension) between the register representation of a signed int
  450. * (msr in 32-bit mode) and the register representation of a signed int
  451. * (msr in 64-bit mode) is performed.
  452. */
  453. long sys32_rt_sigqueueinfo(u32 pid, u32 sig, compat_siginfo_t __user *uinfo)
  454. {
  455. siginfo_t info;
  456. int ret;
  457. mm_segment_t old_fs = get_fs();
  458. if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
  459. copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE32))
  460. return -EFAULT;
  461. set_fs (KERNEL_DS);
  462. /* The __user pointer cast is valid becasuse of the set_fs() */
  463. ret = sys_rt_sigqueueinfo((int)pid, (int)sig, (siginfo_t __user *) &info);
  464. set_fs (old_fs);
  465. return ret;
  466. }
  467. int sys32_rt_sigsuspend(compat_sigset_t __user * unewset, size_t sigsetsize, int p3,
  468. int p4, int p6, int p7, struct pt_regs *regs)
  469. {
  470. sigset_t saveset, newset;
  471. compat_sigset_t s32;
  472. /* XXX: Don't preclude handling different sized sigset_t's. */
  473. if (sigsetsize != sizeof(sigset_t))
  474. return -EINVAL;
  475. if (copy_from_user(&s32, unewset, sizeof(s32)))
  476. return -EFAULT;
  477. /*
  478. * Swap the 2 words of the 64-bit sigset_t (they are stored
  479. * in the "wrong" endian in 32-bit user storage).
  480. */
  481. sigset_from_compat(&newset, &s32);
  482. sigdelsetmask(&newset, ~_BLOCKABLE);
  483. spin_lock_irq(&current->sighand->siglock);
  484. saveset = current->blocked;
  485. current->blocked = newset;
  486. recalc_sigpending();
  487. spin_unlock_irq(&current->sighand->siglock);
  488. regs->result = -EINTR;
  489. regs->gpr[3] = EINTR;
  490. regs->ccr |= 0x10000000;
  491. while (1) {
  492. current->state = TASK_INTERRUPTIBLE;
  493. schedule();
  494. if (do_signal32(&saveset, regs))
  495. /*
  496. * Returning 0 means we return to userspace via
  497. * ret_from_except and thus restore all user
  498. * registers from *regs. This is what we need
  499. * to do when a signal has been delivered.
  500. */
  501. return 0;
  502. }
  503. }
  504. /*
  505. * Start Alternate signal stack support
  506. *
  507. * System Calls
  508. * sigaltatck sys32_sigaltstack
  509. */
  510. int sys32_sigaltstack(u32 __new, u32 __old, int r5,
  511. int r6, int r7, int r8, struct pt_regs *regs)
  512. {
  513. stack_32_t __user * newstack = (stack_32_t __user *)(long) __new;
  514. stack_32_t __user * oldstack = (stack_32_t __user *)(long) __old;
  515. stack_t uss, uoss;
  516. int ret;
  517. mm_segment_t old_fs;
  518. unsigned long sp;
  519. compat_uptr_t ss_sp;
  520. /*
  521. * set sp to the user stack on entry to the system call
  522. * the system call router sets R9 to the saved registers
  523. */
  524. sp = regs->gpr[1];
  525. /* Put new stack info in local 64 bit stack struct */
  526. if (newstack) {
  527. if (get_user(ss_sp, &newstack->ss_sp) ||
  528. __get_user(uss.ss_flags, &newstack->ss_flags) ||
  529. __get_user(uss.ss_size, &newstack->ss_size))
  530. return -EFAULT;
  531. uss.ss_sp = compat_ptr(ss_sp);
  532. }
  533. old_fs = get_fs();
  534. set_fs(KERNEL_DS);
  535. /* The __user pointer casts are valid because of the set_fs() */
  536. ret = do_sigaltstack(
  537. newstack ? (stack_t __user *) &uss : NULL,
  538. oldstack ? (stack_t __user *) &uoss : NULL,
  539. sp);
  540. set_fs(old_fs);
  541. /* Copy the stack information to the user output buffer */
  542. if (!ret && oldstack &&
  543. (put_user((long)uoss.ss_sp, &oldstack->ss_sp) ||
  544. __put_user(uoss.ss_flags, &oldstack->ss_flags) ||
  545. __put_user(uoss.ss_size, &oldstack->ss_size)))
  546. return -EFAULT;
  547. return ret;
  548. }
  549. /*
  550. * Set up a signal frame for a "real-time" signal handler
  551. * (one which gets siginfo).
  552. */
  553. static int handle_rt_signal32(unsigned long sig, struct k_sigaction *ka,
  554. siginfo_t *info, sigset_t *oldset,
  555. struct pt_regs * regs, unsigned long newsp)
  556. {
  557. struct rt_sigframe32 __user *rt_sf;
  558. struct mcontext32 __user *frame;
  559. unsigned long origsp = newsp;
  560. compat_sigset_t c_oldset;
  561. /* Set up Signal Frame */
  562. /* Put a Real Time Context onto stack */
  563. newsp -= sizeof(*rt_sf);
  564. rt_sf = (struct rt_sigframe32 __user *)newsp;
  565. /* create a stack frame for the caller of the handler */
  566. newsp -= __SIGNAL_FRAMESIZE32 + 16;
  567. if (!access_ok(VERIFY_WRITE, (void __user *)newsp, origsp - newsp))
  568. goto badframe;
  569. compat_from_sigset(&c_oldset, oldset);
  570. /* Put the siginfo & fill in most of the ucontext */
  571. if (copy_siginfo_to_user32(&rt_sf->info, info)
  572. || __put_user(0, &rt_sf->uc.uc_flags)
  573. || __put_user(0, &rt_sf->uc.uc_link)
  574. || __put_user(current->sas_ss_sp, &rt_sf->uc.uc_stack.ss_sp)
  575. || __put_user(sas_ss_flags(regs->gpr[1]),
  576. &rt_sf->uc.uc_stack.ss_flags)
  577. || __put_user(current->sas_ss_size, &rt_sf->uc.uc_stack.ss_size)
  578. || __put_user((u32)(u64)&rt_sf->uc.uc_mcontext, &rt_sf->uc.uc_regs)
  579. || __copy_to_user(&rt_sf->uc.uc_sigmask, &c_oldset, sizeof(c_oldset)))
  580. goto badframe;
  581. /* Save user registers on the stack */
  582. frame = &rt_sf->uc.uc_mcontext;
  583. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  584. goto badframe;
  585. if (vdso32_rt_sigtramp && current->thread.vdso_base) {
  586. if (save_user_regs(regs, frame, 0))
  587. goto badframe;
  588. regs->link = current->thread.vdso_base + vdso32_rt_sigtramp;
  589. } else {
  590. if (save_user_regs(regs, frame, __NR_rt_sigreturn))
  591. goto badframe;
  592. regs->link = (unsigned long) frame->tramp;
  593. }
  594. regs->gpr[1] = (unsigned long) newsp;
  595. regs->gpr[3] = sig;
  596. regs->gpr[4] = (unsigned long) &rt_sf->info;
  597. regs->gpr[5] = (unsigned long) &rt_sf->uc;
  598. regs->gpr[6] = (unsigned long) rt_sf;
  599. regs->nip = (unsigned long) ka->sa.sa_handler;
  600. regs->trap = 0;
  601. regs->result = 0;
  602. if (test_thread_flag(TIF_SINGLESTEP))
  603. ptrace_notify(SIGTRAP);
  604. return 1;
  605. badframe:
  606. #if DEBUG_SIG
  607. printk("badframe in handle_rt_signal, regs=%p frame=%p newsp=%lx\n",
  608. regs, frame, newsp);
  609. #endif
  610. force_sigsegv(sig, current);
  611. return 0;
  612. }
  613. static long do_setcontext32(struct ucontext32 __user *ucp, struct pt_regs *regs, int sig)
  614. {
  615. compat_sigset_t c_set;
  616. sigset_t set;
  617. u32 mcp;
  618. if (__copy_from_user(&c_set, &ucp->uc_sigmask, sizeof(c_set))
  619. || __get_user(mcp, &ucp->uc_regs))
  620. return -EFAULT;
  621. sigset_from_compat(&set, &c_set);
  622. restore_sigmask(&set);
  623. if (restore_user_regs(regs, (struct mcontext32 __user *)(u64)mcp, sig))
  624. return -EFAULT;
  625. return 0;
  626. }
  627. /*
  628. * Handle {get,set,swap}_context operations for 32 bits processes
  629. */
  630. long sys32_swapcontext(struct ucontext32 __user *old_ctx,
  631. struct ucontext32 __user *new_ctx,
  632. int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
  633. {
  634. unsigned char tmp;
  635. compat_sigset_t c_set;
  636. /* Context size is for future use. Right now, we only make sure
  637. * we are passed something we understand
  638. */
  639. if (ctx_size < sizeof(struct ucontext32))
  640. return -EINVAL;
  641. if (old_ctx != NULL) {
  642. compat_from_sigset(&c_set, &current->blocked);
  643. if (!access_ok(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
  644. || save_user_regs(regs, &old_ctx->uc_mcontext, 0)
  645. || __copy_to_user(&old_ctx->uc_sigmask, &c_set, sizeof(c_set))
  646. || __put_user((u32)(u64)&old_ctx->uc_mcontext, &old_ctx->uc_regs))
  647. return -EFAULT;
  648. }
  649. if (new_ctx == NULL)
  650. return 0;
  651. if (!access_ok(VERIFY_READ, new_ctx, sizeof(*new_ctx))
  652. || __get_user(tmp, (u8 __user *) new_ctx)
  653. || __get_user(tmp, (u8 __user *) (new_ctx + 1) - 1))
  654. return -EFAULT;
  655. /*
  656. * If we get a fault copying the context into the kernel's
  657. * image of the user's registers, we can't just return -EFAULT
  658. * because the user's registers will be corrupted. For instance
  659. * the NIP value may have been updated but not some of the
  660. * other registers. Given that we have done the access_ok
  661. * and successfully read the first and last bytes of the region
  662. * above, this should only happen in an out-of-memory situation
  663. * or if another thread unmaps the region containing the context.
  664. * We kill the task with a SIGSEGV in this situation.
  665. */
  666. if (do_setcontext32(new_ctx, regs, 0))
  667. do_exit(SIGSEGV);
  668. return 0;
  669. }
  670. long sys32_rt_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
  671. struct pt_regs *regs)
  672. {
  673. struct rt_sigframe32 __user *rt_sf;
  674. int ret;
  675. /* Always make any pending restarted system calls return -EINTR */
  676. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  677. rt_sf = (struct rt_sigframe32 __user *)
  678. (regs->gpr[1] + __SIGNAL_FRAMESIZE32 + 16);
  679. if (!access_ok(VERIFY_READ, rt_sf, sizeof(*rt_sf)))
  680. goto bad;
  681. if (do_setcontext32(&rt_sf->uc, regs, 1))
  682. goto bad;
  683. /*
  684. * It's not clear whether or why it is desirable to save the
  685. * sigaltstack setting on signal delivery and restore it on
  686. * signal return. But other architectures do this and we have
  687. * always done it up until now so it is probably better not to
  688. * change it. -- paulus
  689. * We use the sys32_ version that does the 32/64 bits conversion
  690. * and takes userland pointer directly. What about error checking ?
  691. * nobody does any...
  692. */
  693. sys32_sigaltstack((u32)(u64)&rt_sf->uc.uc_stack, 0, 0, 0, 0, 0, regs);
  694. ret = regs->result;
  695. return ret;
  696. bad:
  697. force_sig(SIGSEGV, current);
  698. return 0;
  699. }
  700. /*
  701. * OK, we're invoking a handler
  702. */
  703. static int handle_signal32(unsigned long sig, struct k_sigaction *ka,
  704. siginfo_t *info, sigset_t *oldset,
  705. struct pt_regs * regs, unsigned long newsp)
  706. {
  707. struct sigcontext32 __user *sc;
  708. struct sigregs32 __user *frame;
  709. unsigned long origsp = newsp;
  710. /* Set up Signal Frame */
  711. newsp -= sizeof(struct sigregs32);
  712. frame = (struct sigregs32 __user *) newsp;
  713. /* Put a sigcontext on the stack */
  714. newsp -= sizeof(*sc);
  715. sc = (struct sigcontext32 __user *) newsp;
  716. /* create a stack frame for the caller of the handler */
  717. newsp -= __SIGNAL_FRAMESIZE32;
  718. if (!access_ok(VERIFY_WRITE, (void __user *) newsp, origsp - newsp))
  719. goto badframe;
  720. #if _NSIG != 64
  721. #error "Please adjust handle_signal32()"
  722. #endif
  723. if (__put_user((u32)(u64)ka->sa.sa_handler, &sc->handler)
  724. || __put_user(oldset->sig[0], &sc->oldmask)
  725. || __put_user((oldset->sig[0] >> 32), &sc->_unused[3])
  726. || __put_user((u32)(u64)frame, &sc->regs)
  727. || __put_user(sig, &sc->signal))
  728. goto badframe;
  729. if (vdso32_sigtramp && current->thread.vdso_base) {
  730. if (save_user_regs(regs, &frame->mctx, 0))
  731. goto badframe;
  732. regs->link = current->thread.vdso_base + vdso32_sigtramp;
  733. } else {
  734. if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
  735. goto badframe;
  736. regs->link = (unsigned long) frame->mctx.tramp;
  737. }
  738. if (put_user(regs->gpr[1], (u32 __user *)newsp))
  739. goto badframe;
  740. regs->gpr[1] = (unsigned long) newsp;
  741. regs->gpr[3] = sig;
  742. regs->gpr[4] = (unsigned long) sc;
  743. regs->nip = (unsigned long) ka->sa.sa_handler;
  744. regs->trap = 0;
  745. regs->result = 0;
  746. if (test_thread_flag(TIF_SINGLESTEP))
  747. ptrace_notify(SIGTRAP);
  748. return 1;
  749. badframe:
  750. #if DEBUG_SIG
  751. printk("badframe in handle_signal, regs=%p frame=%x newsp=%x\n",
  752. regs, frame, *newspp);
  753. #endif
  754. force_sigsegv(sig, current);
  755. return 0;
  756. }
  757. /*
  758. * Do a signal return; undo the signal stack.
  759. */
  760. long sys32_sigreturn(int r3, int r4, int r5, int r6, int r7, int r8,
  761. struct pt_regs *regs)
  762. {
  763. struct sigcontext32 __user *sc;
  764. struct sigcontext32 sigctx;
  765. struct mcontext32 __user *sr;
  766. sigset_t set;
  767. int ret;
  768. /* Always make any pending restarted system calls return -EINTR */
  769. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  770. sc = (struct sigcontext32 __user *)(regs->gpr[1] + __SIGNAL_FRAMESIZE32);
  771. if (copy_from_user(&sigctx, sc, sizeof(sigctx)))
  772. goto badframe;
  773. /*
  774. * Note that PPC32 puts the upper 32 bits of the sigmask in the
  775. * unused part of the signal stackframe
  776. */
  777. set.sig[0] = sigctx.oldmask + ((long)(sigctx._unused[3]) << 32);
  778. restore_sigmask(&set);
  779. sr = (struct mcontext32 __user *)(u64)sigctx.regs;
  780. if (!access_ok(VERIFY_READ, sr, sizeof(*sr))
  781. || restore_user_regs(regs, sr, 1))
  782. goto badframe;
  783. ret = regs->result;
  784. return ret;
  785. badframe:
  786. force_sig(SIGSEGV, current);
  787. return 0;
  788. }
  789. /*
  790. * Start of do_signal32 routine
  791. *
  792. * This routine gets control when a pending signal needs to be processed
  793. * in the 32 bit target thread -
  794. *
  795. * It handles both rt and non-rt signals
  796. */
  797. /*
  798. * Note that 'init' is a special process: it doesn't get signals it doesn't
  799. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  800. * mistake.
  801. */
  802. int do_signal32(sigset_t *oldset, struct pt_regs *regs)
  803. {
  804. siginfo_t info;
  805. unsigned int frame, newsp;
  806. int signr, ret;
  807. struct k_sigaction ka;
  808. if (!oldset)
  809. oldset = &current->blocked;
  810. newsp = frame = 0;
  811. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  812. if (TRAP(regs) == 0x0C00 /* System Call! */
  813. && regs->ccr & 0x10000000 /* error signalled */
  814. && ((ret = regs->gpr[3]) == ERESTARTSYS
  815. || ret == ERESTARTNOHAND || ret == ERESTARTNOINTR
  816. || ret == ERESTART_RESTARTBLOCK)) {
  817. if (signr > 0
  818. && (ret == ERESTARTNOHAND || ret == ERESTART_RESTARTBLOCK
  819. || (ret == ERESTARTSYS
  820. && !(ka.sa.sa_flags & SA_RESTART)))) {
  821. /* make the system call return an EINTR error */
  822. regs->result = -EINTR;
  823. regs->gpr[3] = EINTR;
  824. /* note that the cr0.SO bit is already set */
  825. } else {
  826. regs->nip -= 4; /* Back up & retry system call */
  827. regs->result = 0;
  828. regs->trap = 0;
  829. if (ret == ERESTART_RESTARTBLOCK)
  830. regs->gpr[0] = __NR_restart_syscall;
  831. else
  832. regs->gpr[3] = regs->orig_gpr3;
  833. }
  834. }
  835. if (signr == 0)
  836. return 0; /* no signals delivered */
  837. if ((ka.sa.sa_flags & SA_ONSTACK) && current->sas_ss_size
  838. && (!on_sig_stack(regs->gpr[1])))
  839. newsp = (current->sas_ss_sp + current->sas_ss_size);
  840. else
  841. newsp = regs->gpr[1];
  842. newsp &= ~0xfUL;
  843. /* Whee! Actually deliver the signal. */
  844. if (ka.sa.sa_flags & SA_SIGINFO)
  845. ret = handle_rt_signal32(signr, &ka, &info, oldset, regs, newsp);
  846. else
  847. ret = handle_signal32(signr, &ka, &info, oldset, regs, newsp);
  848. if (ret) {
  849. spin_lock_irq(&current->sighand->siglock);
  850. sigorsets(&current->blocked, &current->blocked,
  851. &ka.sa.sa_mask);
  852. if (!(ka.sa.sa_flags & SA_NODEFER))
  853. sigaddset(&current->blocked, signr);
  854. recalc_sigpending();
  855. spin_unlock_irq(&current->sighand->siglock);
  856. }
  857. return ret;
  858. }