signal32.c 24 KB

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