signal32.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * Based on arch/arm/kernel/signal.c
  3. *
  4. * Copyright (C) 1995-2009 Russell King
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Modified by Will Deacon <will.deacon@arm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/compat.h>
  21. #include <linux/signal.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/ratelimit.h>
  24. #include <asm/fpsimd.h>
  25. #include <asm/signal32.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/unistd32.h>
  28. struct compat_sigaction {
  29. compat_uptr_t sa_handler;
  30. compat_ulong_t sa_flags;
  31. compat_uptr_t sa_restorer;
  32. compat_sigset_t sa_mask;
  33. };
  34. struct compat_old_sigaction {
  35. compat_uptr_t sa_handler;
  36. compat_old_sigset_t sa_mask;
  37. compat_ulong_t sa_flags;
  38. compat_uptr_t sa_restorer;
  39. };
  40. struct compat_sigcontext {
  41. /* We always set these two fields to 0 */
  42. compat_ulong_t trap_no;
  43. compat_ulong_t error_code;
  44. compat_ulong_t oldmask;
  45. compat_ulong_t arm_r0;
  46. compat_ulong_t arm_r1;
  47. compat_ulong_t arm_r2;
  48. compat_ulong_t arm_r3;
  49. compat_ulong_t arm_r4;
  50. compat_ulong_t arm_r5;
  51. compat_ulong_t arm_r6;
  52. compat_ulong_t arm_r7;
  53. compat_ulong_t arm_r8;
  54. compat_ulong_t arm_r9;
  55. compat_ulong_t arm_r10;
  56. compat_ulong_t arm_fp;
  57. compat_ulong_t arm_ip;
  58. compat_ulong_t arm_sp;
  59. compat_ulong_t arm_lr;
  60. compat_ulong_t arm_pc;
  61. compat_ulong_t arm_cpsr;
  62. compat_ulong_t fault_address;
  63. };
  64. struct compat_ucontext {
  65. compat_ulong_t uc_flags;
  66. struct compat_ucontext *uc_link;
  67. compat_stack_t uc_stack;
  68. struct compat_sigcontext uc_mcontext;
  69. compat_sigset_t uc_sigmask;
  70. int __unused[32 - (sizeof (compat_sigset_t) / sizeof (int))];
  71. compat_ulong_t uc_regspace[128] __attribute__((__aligned__(8)));
  72. };
  73. struct compat_vfp_sigframe {
  74. compat_ulong_t magic;
  75. compat_ulong_t size;
  76. struct compat_user_vfp {
  77. compat_u64 fpregs[32];
  78. compat_ulong_t fpscr;
  79. } ufp;
  80. struct compat_user_vfp_exc {
  81. compat_ulong_t fpexc;
  82. compat_ulong_t fpinst;
  83. compat_ulong_t fpinst2;
  84. } ufp_exc;
  85. } __attribute__((__aligned__(8)));
  86. #define VFP_MAGIC 0x56465001
  87. #define VFP_STORAGE_SIZE sizeof(struct compat_vfp_sigframe)
  88. struct compat_aux_sigframe {
  89. struct compat_vfp_sigframe vfp;
  90. /* Something that isn't a valid magic number for any coprocessor. */
  91. unsigned long end_magic;
  92. } __attribute__((__aligned__(8)));
  93. struct compat_sigframe {
  94. struct compat_ucontext uc;
  95. compat_ulong_t retcode[2];
  96. };
  97. struct compat_rt_sigframe {
  98. struct compat_siginfo info;
  99. struct compat_sigframe sig;
  100. };
  101. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  102. /*
  103. * For ARM syscalls, the syscall number has to be loaded into r7.
  104. * We do not support an OABI userspace.
  105. */
  106. #define MOV_R7_NR_SIGRETURN (0xe3a07000 | __NR_compat_sigreturn)
  107. #define SVC_SYS_SIGRETURN (0xef000000 | __NR_compat_sigreturn)
  108. #define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | __NR_compat_rt_sigreturn)
  109. #define SVC_SYS_RT_SIGRETURN (0xef000000 | __NR_compat_rt_sigreturn)
  110. /*
  111. * For Thumb syscalls, we also pass the syscall number via r7. We therefore
  112. * need two 16-bit instructions.
  113. */
  114. #define SVC_THUMB_SIGRETURN (((0xdf00 | __NR_compat_sigreturn) << 16) | \
  115. 0x2700 | __NR_compat_sigreturn)
  116. #define SVC_THUMB_RT_SIGRETURN (((0xdf00 | __NR_compat_rt_sigreturn) << 16) | \
  117. 0x2700 | __NR_compat_rt_sigreturn)
  118. const compat_ulong_t aarch32_sigret_code[6] = {
  119. /*
  120. * AArch32 sigreturn code.
  121. * We don't construct an OABI SWI - instead we just set the imm24 field
  122. * to the EABI syscall number so that we create a sane disassembly.
  123. */
  124. MOV_R7_NR_SIGRETURN, SVC_SYS_SIGRETURN, SVC_THUMB_SIGRETURN,
  125. MOV_R7_NR_RT_SIGRETURN, SVC_SYS_RT_SIGRETURN, SVC_THUMB_RT_SIGRETURN,
  126. };
  127. static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
  128. {
  129. compat_sigset_t cset;
  130. cset.sig[0] = set->sig[0] & 0xffffffffull;
  131. cset.sig[1] = set->sig[0] >> 32;
  132. return copy_to_user(uset, &cset, sizeof(*uset));
  133. }
  134. static inline int get_sigset_t(sigset_t *set,
  135. const compat_sigset_t __user *uset)
  136. {
  137. compat_sigset_t s32;
  138. if (copy_from_user(&s32, uset, sizeof(*uset)))
  139. return -EFAULT;
  140. set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
  141. return 0;
  142. }
  143. int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
  144. {
  145. int err;
  146. if (!access_ok(VERIFY_WRITE, to, sizeof(*to)))
  147. return -EFAULT;
  148. /* If you change siginfo_t structure, please be sure
  149. * this code is fixed accordingly.
  150. * It should never copy any pad contained in the structure
  151. * to avoid security leaks, but must copy the generic
  152. * 3 ints plus the relevant union member.
  153. * This routine must convert siginfo from 64bit to 32bit as well
  154. * at the same time.
  155. */
  156. err = __put_user(from->si_signo, &to->si_signo);
  157. err |= __put_user(from->si_errno, &to->si_errno);
  158. err |= __put_user((short)from->si_code, &to->si_code);
  159. if (from->si_code < 0)
  160. err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad,
  161. SI_PAD_SIZE);
  162. else switch (from->si_code & __SI_MASK) {
  163. case __SI_KILL:
  164. err |= __put_user(from->si_pid, &to->si_pid);
  165. err |= __put_user(from->si_uid, &to->si_uid);
  166. break;
  167. case __SI_TIMER:
  168. err |= __put_user(from->si_tid, &to->si_tid);
  169. err |= __put_user(from->si_overrun, &to->si_overrun);
  170. err |= __put_user((compat_uptr_t)(unsigned long)from->si_ptr,
  171. &to->si_ptr);
  172. break;
  173. case __SI_POLL:
  174. err |= __put_user(from->si_band, &to->si_band);
  175. err |= __put_user(from->si_fd, &to->si_fd);
  176. break;
  177. case __SI_FAULT:
  178. err |= __put_user((compat_uptr_t)(unsigned long)from->si_addr,
  179. &to->si_addr);
  180. #ifdef BUS_MCEERR_AO
  181. /*
  182. * Other callers might not initialize the si_lsb field,
  183. * so check explicitely for the right codes here.
  184. */
  185. if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
  186. err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
  187. #endif
  188. break;
  189. case __SI_CHLD:
  190. err |= __put_user(from->si_pid, &to->si_pid);
  191. err |= __put_user(from->si_uid, &to->si_uid);
  192. err |= __put_user(from->si_status, &to->si_status);
  193. err |= __put_user(from->si_utime, &to->si_utime);
  194. err |= __put_user(from->si_stime, &to->si_stime);
  195. break;
  196. case __SI_RT: /* This is not generated by the kernel as of now. */
  197. case __SI_MESGQ: /* But this is */
  198. err |= __put_user(from->si_pid, &to->si_pid);
  199. err |= __put_user(from->si_uid, &to->si_uid);
  200. err |= __put_user((compat_uptr_t)(unsigned long)from->si_ptr, &to->si_ptr);
  201. break;
  202. default: /* this is just in case for now ... */
  203. err |= __put_user(from->si_pid, &to->si_pid);
  204. err |= __put_user(from->si_uid, &to->si_uid);
  205. break;
  206. }
  207. return err;
  208. }
  209. int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
  210. {
  211. memset(to, 0, sizeof *to);
  212. if (copy_from_user(to, from, __ARCH_SI_PREAMBLE_SIZE) ||
  213. copy_from_user(to->_sifields._pad,
  214. from->_sifields._pad, SI_PAD_SIZE))
  215. return -EFAULT;
  216. return 0;
  217. }
  218. /*
  219. * VFP save/restore code.
  220. */
  221. static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
  222. {
  223. struct fpsimd_state *fpsimd = &current->thread.fpsimd_state;
  224. compat_ulong_t magic = VFP_MAGIC;
  225. compat_ulong_t size = VFP_STORAGE_SIZE;
  226. compat_ulong_t fpscr, fpexc;
  227. int err = 0;
  228. /*
  229. * Save the hardware registers to the fpsimd_state structure.
  230. * Note that this also saves V16-31, which aren't visible
  231. * in AArch32.
  232. */
  233. fpsimd_save_state(fpsimd);
  234. /* Place structure header on the stack */
  235. __put_user_error(magic, &frame->magic, err);
  236. __put_user_error(size, &frame->size, err);
  237. /*
  238. * Now copy the FP registers. Since the registers are packed,
  239. * we can copy the prefix we want (V0-V15) as it is.
  240. * FIXME: Won't work if big endian.
  241. */
  242. err |= __copy_to_user(&frame->ufp.fpregs, fpsimd->vregs,
  243. sizeof(frame->ufp.fpregs));
  244. /* Create an AArch32 fpscr from the fpsr and the fpcr. */
  245. fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
  246. (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
  247. __put_user_error(fpscr, &frame->ufp.fpscr, err);
  248. /*
  249. * The exception register aren't available so we fake up a
  250. * basic FPEXC and zero everything else.
  251. */
  252. fpexc = (1 << 30);
  253. __put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
  254. __put_user_error(0, &frame->ufp_exc.fpinst, err);
  255. __put_user_error(0, &frame->ufp_exc.fpinst2, err);
  256. return err ? -EFAULT : 0;
  257. }
  258. static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
  259. {
  260. struct fpsimd_state fpsimd;
  261. compat_ulong_t magic = VFP_MAGIC;
  262. compat_ulong_t size = VFP_STORAGE_SIZE;
  263. compat_ulong_t fpscr;
  264. int err = 0;
  265. __get_user_error(magic, &frame->magic, err);
  266. __get_user_error(size, &frame->size, err);
  267. if (err)
  268. return -EFAULT;
  269. if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
  270. return -EINVAL;
  271. /*
  272. * Copy the FP registers into the start of the fpsimd_state.
  273. * FIXME: Won't work if big endian.
  274. */
  275. err |= __copy_from_user(fpsimd.vregs, frame->ufp.fpregs,
  276. sizeof(frame->ufp.fpregs));
  277. /* Extract the fpsr and the fpcr from the fpscr */
  278. __get_user_error(fpscr, &frame->ufp.fpscr, err);
  279. fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
  280. fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
  281. /*
  282. * We don't need to touch the exception register, so
  283. * reload the hardware state.
  284. */
  285. if (!err) {
  286. preempt_disable();
  287. fpsimd_load_state(&fpsimd);
  288. preempt_enable();
  289. }
  290. return err ? -EFAULT : 0;
  291. }
  292. /*
  293. * atomically swap in the new signal mask, and wait for a signal.
  294. */
  295. asmlinkage int compat_sys_sigsuspend(int restart, compat_ulong_t oldmask,
  296. compat_old_sigset_t mask)
  297. {
  298. sigset_t blocked;
  299. siginitset(&current->blocked, mask);
  300. return sigsuspend(&blocked);
  301. }
  302. asmlinkage int compat_sys_sigaction(int sig,
  303. const struct compat_old_sigaction __user *act,
  304. struct compat_old_sigaction __user *oact)
  305. {
  306. struct k_sigaction new_ka, old_ka;
  307. int ret;
  308. compat_old_sigset_t mask;
  309. compat_uptr_t handler, restorer;
  310. if (act) {
  311. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  312. __get_user(handler, &act->sa_handler) ||
  313. __get_user(restorer, &act->sa_restorer) ||
  314. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  315. __get_user(mask, &act->sa_mask))
  316. return -EFAULT;
  317. new_ka.sa.sa_handler = compat_ptr(handler);
  318. new_ka.sa.sa_restorer = compat_ptr(restorer);
  319. siginitset(&new_ka.sa.sa_mask, mask);
  320. }
  321. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  322. if (!ret && oact) {
  323. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  324. __put_user(ptr_to_compat(old_ka.sa.sa_handler),
  325. &oact->sa_handler) ||
  326. __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
  327. &oact->sa_restorer) ||
  328. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  329. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  330. return -EFAULT;
  331. }
  332. return ret;
  333. }
  334. asmlinkage int compat_sys_rt_sigaction(int sig,
  335. const struct compat_sigaction __user *act,
  336. struct compat_sigaction __user *oact,
  337. compat_size_t sigsetsize)
  338. {
  339. struct k_sigaction new_ka, old_ka;
  340. int ret;
  341. /* XXX: Don't preclude handling different sized sigset_t's. */
  342. if (sigsetsize != sizeof(compat_sigset_t))
  343. return -EINVAL;
  344. if (act) {
  345. compat_uptr_t handler, restorer;
  346. ret = get_user(handler, &act->sa_handler);
  347. new_ka.sa.sa_handler = compat_ptr(handler);
  348. ret |= get_user(restorer, &act->sa_restorer);
  349. new_ka.sa.sa_restorer = compat_ptr(restorer);
  350. ret |= get_sigset_t(&new_ka.sa.sa_mask, &act->sa_mask);
  351. ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  352. if (ret)
  353. return -EFAULT;
  354. }
  355. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  356. if (!ret && oact) {
  357. ret = put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler);
  358. ret |= put_sigset_t(&oact->sa_mask, &old_ka.sa.sa_mask);
  359. ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  360. }
  361. return ret;
  362. }
  363. static int compat_restore_sigframe(struct pt_regs *regs,
  364. struct compat_sigframe __user *sf)
  365. {
  366. int err;
  367. sigset_t set;
  368. struct compat_aux_sigframe __user *aux;
  369. err = get_sigset_t(&set, &sf->uc.uc_sigmask);
  370. if (err == 0) {
  371. sigdelsetmask(&set, ~_BLOCKABLE);
  372. set_current_blocked(&set);
  373. }
  374. __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
  375. __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
  376. __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
  377. __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
  378. __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
  379. __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
  380. __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
  381. __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
  382. __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
  383. __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
  384. __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
  385. __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
  386. __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
  387. __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
  388. __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
  389. __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
  390. __get_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
  391. /*
  392. * Avoid compat_sys_sigreturn() restarting.
  393. */
  394. regs->syscallno = ~0UL;
  395. err |= !valid_user_regs(&regs->user_regs);
  396. aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
  397. if (err == 0)
  398. err |= compat_restore_vfp_context(&aux->vfp);
  399. return err;
  400. }
  401. asmlinkage int compat_sys_sigreturn(struct pt_regs *regs)
  402. {
  403. struct compat_sigframe __user *frame;
  404. /* Always make any pending restarted system calls return -EINTR */
  405. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  406. /*
  407. * Since we stacked the signal on a 64-bit boundary,
  408. * then 'sp' should be word aligned here. If it's
  409. * not, then the user is trying to mess with us.
  410. */
  411. if (regs->compat_sp & 7)
  412. goto badframe;
  413. frame = (struct compat_sigframe __user *)regs->compat_sp;
  414. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  415. goto badframe;
  416. if (compat_restore_sigframe(regs, frame))
  417. goto badframe;
  418. return regs->regs[0];
  419. badframe:
  420. if (show_unhandled_signals)
  421. pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
  422. current->comm, task_pid_nr(current), __func__,
  423. regs->pc, regs->sp);
  424. force_sig(SIGSEGV, current);
  425. return 0;
  426. }
  427. asmlinkage int compat_sys_rt_sigreturn(struct pt_regs *regs)
  428. {
  429. struct compat_rt_sigframe __user *frame;
  430. /* Always make any pending restarted system calls return -EINTR */
  431. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  432. /*
  433. * Since we stacked the signal on a 64-bit boundary,
  434. * then 'sp' should be word aligned here. If it's
  435. * not, then the user is trying to mess with us.
  436. */
  437. if (regs->compat_sp & 7)
  438. goto badframe;
  439. frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
  440. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  441. goto badframe;
  442. if (compat_restore_sigframe(regs, &frame->sig))
  443. goto badframe;
  444. if (compat_restore_altstack(&frame->sig.uc.uc_stack))
  445. goto badframe;
  446. return regs->regs[0];
  447. badframe:
  448. if (show_unhandled_signals)
  449. pr_info_ratelimited("%s[%d]: bad frame in %s: pc=%08llx sp=%08llx\n",
  450. current->comm, task_pid_nr(current), __func__,
  451. regs->pc, regs->sp);
  452. force_sig(SIGSEGV, current);
  453. return 0;
  454. }
  455. static void __user *compat_get_sigframe(struct k_sigaction *ka,
  456. struct pt_regs *regs,
  457. int framesize)
  458. {
  459. compat_ulong_t sp = regs->compat_sp;
  460. void __user *frame;
  461. /*
  462. * This is the X/Open sanctioned signal stack switching.
  463. */
  464. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
  465. sp = current->sas_ss_sp + current->sas_ss_size;
  466. /*
  467. * ATPCS B01 mandates 8-byte alignment
  468. */
  469. frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
  470. /*
  471. * Check that we can actually write to the signal frame.
  472. */
  473. if (!access_ok(VERIFY_WRITE, frame, framesize))
  474. frame = NULL;
  475. return frame;
  476. }
  477. static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  478. compat_ulong_t __user *rc, void __user *frame,
  479. int usig)
  480. {
  481. compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
  482. compat_ulong_t retcode;
  483. compat_ulong_t spsr = regs->pstate & ~PSR_f;
  484. int thumb;
  485. /* Check if the handler is written for ARM or Thumb */
  486. thumb = handler & 1;
  487. if (thumb) {
  488. spsr |= COMPAT_PSR_T_BIT;
  489. spsr &= ~COMPAT_PSR_IT_MASK;
  490. } else {
  491. spsr &= ~COMPAT_PSR_T_BIT;
  492. }
  493. if (ka->sa.sa_flags & SA_RESTORER) {
  494. retcode = ptr_to_compat(ka->sa.sa_restorer);
  495. } else {
  496. /* Set up sigreturn pointer */
  497. unsigned int idx = thumb << 1;
  498. if (ka->sa.sa_flags & SA_SIGINFO)
  499. idx += 3;
  500. retcode = AARCH32_VECTORS_BASE +
  501. AARCH32_KERN_SIGRET_CODE_OFFSET +
  502. (idx << 2) + thumb;
  503. }
  504. regs->regs[0] = usig;
  505. regs->compat_sp = ptr_to_compat(frame);
  506. regs->compat_lr = retcode;
  507. regs->pc = handler;
  508. regs->pstate = spsr;
  509. }
  510. static int compat_setup_sigframe(struct compat_sigframe __user *sf,
  511. struct pt_regs *regs, sigset_t *set)
  512. {
  513. struct compat_aux_sigframe __user *aux;
  514. int err = 0;
  515. __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
  516. __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
  517. __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
  518. __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
  519. __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
  520. __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
  521. __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
  522. __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
  523. __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
  524. __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
  525. __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
  526. __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
  527. __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
  528. __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
  529. __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
  530. __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
  531. __put_user_error(regs->pstate, &sf->uc.uc_mcontext.arm_cpsr, err);
  532. __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
  533. __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.error_code, err);
  534. __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
  535. __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
  536. err |= put_sigset_t(&sf->uc.uc_sigmask, set);
  537. aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
  538. if (err == 0)
  539. err |= compat_preserve_vfp_context(&aux->vfp);
  540. __put_user_error(0, &aux->end_magic, err);
  541. return err;
  542. }
  543. /*
  544. * 32-bit signal handling routines called from signal.c
  545. */
  546. int compat_setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
  547. sigset_t *set, struct pt_regs *regs)
  548. {
  549. struct compat_rt_sigframe __user *frame;
  550. compat_stack_t stack;
  551. int err = 0;
  552. frame = compat_get_sigframe(ka, regs, sizeof(*frame));
  553. if (!frame)
  554. return 1;
  555. err |= copy_siginfo_to_user32(&frame->info, info);
  556. __put_user_error(0, &frame->sig.uc.uc_flags, err);
  557. __put_user_error(NULL, &frame->sig.uc.uc_link, err);
  558. err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp);
  559. err |= compat_setup_sigframe(&frame->sig, regs, set);
  560. if (err == 0) {
  561. compat_setup_return(regs, ka, frame->sig.retcode, frame, usig);
  562. regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
  563. regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
  564. }
  565. return err;
  566. }
  567. int compat_setup_frame(int usig, struct k_sigaction *ka, sigset_t *set,
  568. struct pt_regs *regs)
  569. {
  570. struct compat_sigframe __user *frame;
  571. int err = 0;
  572. frame = compat_get_sigframe(ka, regs, sizeof(*frame));
  573. if (!frame)
  574. return 1;
  575. __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
  576. err |= compat_setup_sigframe(frame, regs, set);
  577. if (err == 0)
  578. compat_setup_return(regs, ka, frame->retcode, frame, usig);
  579. return err;
  580. }
  581. /*
  582. * RT signals don't have generic compat wrappers.
  583. * See arch/powerpc/kernel/signal_32.c
  584. */
  585. asmlinkage int compat_sys_rt_sigprocmask(int how, compat_sigset_t __user *set,
  586. compat_sigset_t __user *oset,
  587. compat_size_t sigsetsize)
  588. {
  589. sigset_t s;
  590. sigset_t __user *up;
  591. int ret;
  592. mm_segment_t old_fs = get_fs();
  593. if (set) {
  594. if (get_sigset_t(&s, set))
  595. return -EFAULT;
  596. }
  597. set_fs(KERNEL_DS);
  598. /* This is valid because of the set_fs() */
  599. up = (sigset_t __user *) &s;
  600. ret = sys_rt_sigprocmask(how, set ? up : NULL, oset ? up : NULL,
  601. sigsetsize);
  602. set_fs(old_fs);
  603. if (ret)
  604. return ret;
  605. if (oset) {
  606. if (put_sigset_t(oset, &s))
  607. return -EFAULT;
  608. }
  609. return 0;
  610. }
  611. asmlinkage int compat_sys_rt_sigpending(compat_sigset_t __user *set,
  612. compat_size_t sigsetsize)
  613. {
  614. sigset_t s;
  615. int ret;
  616. mm_segment_t old_fs = get_fs();
  617. set_fs(KERNEL_DS);
  618. /* The __user pointer cast is valid because of the set_fs() */
  619. ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
  620. set_fs(old_fs);
  621. if (!ret) {
  622. if (put_sigset_t(set, &s))
  623. return -EFAULT;
  624. }
  625. return ret;
  626. }
  627. asmlinkage int compat_sys_rt_sigqueueinfo(int pid, int sig,
  628. compat_siginfo_t __user *uinfo)
  629. {
  630. siginfo_t info;
  631. int ret;
  632. mm_segment_t old_fs = get_fs();
  633. ret = copy_siginfo_from_user32(&info, uinfo);
  634. if (unlikely(ret))
  635. return ret;
  636. set_fs (KERNEL_DS);
  637. /* The __user pointer cast is valid because of the set_fs() */
  638. ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *) &info);
  639. set_fs (old_fs);
  640. return ret;
  641. }
  642. void compat_setup_restart_syscall(struct pt_regs *regs)
  643. {
  644. regs->regs[7] = __NR_compat_restart_syscall;
  645. }