signal32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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/esr.h>
  25. #include <asm/fpsimd.h>
  26. #include <asm/signal32.h>
  27. #include <asm/traps.h>
  28. #include <linux/uaccess.h>
  29. #include <asm/unistd.h>
  30. struct compat_sigcontext {
  31. /* We always set these two fields to 0 */
  32. compat_ulong_t trap_no;
  33. compat_ulong_t error_code;
  34. compat_ulong_t oldmask;
  35. compat_ulong_t arm_r0;
  36. compat_ulong_t arm_r1;
  37. compat_ulong_t arm_r2;
  38. compat_ulong_t arm_r3;
  39. compat_ulong_t arm_r4;
  40. compat_ulong_t arm_r5;
  41. compat_ulong_t arm_r6;
  42. compat_ulong_t arm_r7;
  43. compat_ulong_t arm_r8;
  44. compat_ulong_t arm_r9;
  45. compat_ulong_t arm_r10;
  46. compat_ulong_t arm_fp;
  47. compat_ulong_t arm_ip;
  48. compat_ulong_t arm_sp;
  49. compat_ulong_t arm_lr;
  50. compat_ulong_t arm_pc;
  51. compat_ulong_t arm_cpsr;
  52. compat_ulong_t fault_address;
  53. };
  54. struct compat_ucontext {
  55. compat_ulong_t uc_flags;
  56. compat_uptr_t uc_link;
  57. compat_stack_t uc_stack;
  58. struct compat_sigcontext uc_mcontext;
  59. compat_sigset_t uc_sigmask;
  60. int __unused[32 - (sizeof (compat_sigset_t) / sizeof (int))];
  61. compat_ulong_t uc_regspace[128] __attribute__((__aligned__(8)));
  62. };
  63. struct compat_vfp_sigframe {
  64. compat_ulong_t magic;
  65. compat_ulong_t size;
  66. struct compat_user_vfp {
  67. compat_u64 fpregs[32];
  68. compat_ulong_t fpscr;
  69. } ufp;
  70. struct compat_user_vfp_exc {
  71. compat_ulong_t fpexc;
  72. compat_ulong_t fpinst;
  73. compat_ulong_t fpinst2;
  74. } ufp_exc;
  75. } __attribute__((__aligned__(8)));
  76. #define VFP_MAGIC 0x56465001
  77. #define VFP_STORAGE_SIZE sizeof(struct compat_vfp_sigframe)
  78. #define FSR_WRITE_SHIFT (11)
  79. struct compat_aux_sigframe {
  80. struct compat_vfp_sigframe vfp;
  81. /* Something that isn't a valid magic number for any coprocessor. */
  82. unsigned long end_magic;
  83. } __attribute__((__aligned__(8)));
  84. struct compat_sigframe {
  85. struct compat_ucontext uc;
  86. compat_ulong_t retcode[2];
  87. };
  88. struct compat_rt_sigframe {
  89. struct compat_siginfo info;
  90. struct compat_sigframe sig;
  91. };
  92. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  93. static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
  94. {
  95. compat_sigset_t cset;
  96. cset.sig[0] = set->sig[0] & 0xffffffffull;
  97. cset.sig[1] = set->sig[0] >> 32;
  98. return copy_to_user(uset, &cset, sizeof(*uset));
  99. }
  100. static inline int get_sigset_t(sigset_t *set,
  101. const compat_sigset_t __user *uset)
  102. {
  103. compat_sigset_t s32;
  104. if (copy_from_user(&s32, uset, sizeof(*uset)))
  105. return -EFAULT;
  106. set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
  107. return 0;
  108. }
  109. /*
  110. * VFP save/restore code.
  111. *
  112. * We have to be careful with endianness, since the fpsimd context-switch
  113. * code operates on 128-bit (Q) register values whereas the compat ABI
  114. * uses an array of 64-bit (D) registers. Consequently, we need to swap
  115. * the two halves of each Q register when running on a big-endian CPU.
  116. */
  117. union __fpsimd_vreg {
  118. __uint128_t raw;
  119. struct {
  120. #ifdef __AARCH64EB__
  121. u64 hi;
  122. u64 lo;
  123. #else
  124. u64 lo;
  125. u64 hi;
  126. #endif
  127. };
  128. };
  129. static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
  130. {
  131. struct user_fpsimd_state const *fpsimd =
  132. &current->thread.uw.fpsimd_state;
  133. compat_ulong_t magic = VFP_MAGIC;
  134. compat_ulong_t size = VFP_STORAGE_SIZE;
  135. compat_ulong_t fpscr, fpexc;
  136. int i, err = 0;
  137. /*
  138. * Save the hardware registers to the fpsimd_state structure.
  139. * Note that this also saves V16-31, which aren't visible
  140. * in AArch32.
  141. */
  142. fpsimd_signal_preserve_current_state();
  143. /* Place structure header on the stack */
  144. __put_user_error(magic, &frame->magic, err);
  145. __put_user_error(size, &frame->size, err);
  146. /*
  147. * Now copy the FP registers. Since the registers are packed,
  148. * we can copy the prefix we want (V0-V15) as it is.
  149. */
  150. for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
  151. union __fpsimd_vreg vreg = {
  152. .raw = fpsimd->vregs[i >> 1],
  153. };
  154. __put_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
  155. __put_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
  156. }
  157. /* Create an AArch32 fpscr from the fpsr and the fpcr. */
  158. fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
  159. (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
  160. __put_user_error(fpscr, &frame->ufp.fpscr, err);
  161. /*
  162. * The exception register aren't available so we fake up a
  163. * basic FPEXC and zero everything else.
  164. */
  165. fpexc = (1 << 30);
  166. __put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
  167. __put_user_error(0, &frame->ufp_exc.fpinst, err);
  168. __put_user_error(0, &frame->ufp_exc.fpinst2, err);
  169. return err ? -EFAULT : 0;
  170. }
  171. static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
  172. {
  173. struct user_fpsimd_state fpsimd;
  174. compat_ulong_t magic = VFP_MAGIC;
  175. compat_ulong_t size = VFP_STORAGE_SIZE;
  176. compat_ulong_t fpscr;
  177. int i, err = 0;
  178. __get_user_error(magic, &frame->magic, err);
  179. __get_user_error(size, &frame->size, err);
  180. if (err)
  181. return -EFAULT;
  182. if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
  183. return -EINVAL;
  184. /* Copy the FP registers into the start of the fpsimd_state. */
  185. for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
  186. union __fpsimd_vreg vreg;
  187. __get_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
  188. __get_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
  189. fpsimd.vregs[i >> 1] = vreg.raw;
  190. }
  191. /* Extract the fpsr and the fpcr from the fpscr */
  192. __get_user_error(fpscr, &frame->ufp.fpscr, err);
  193. fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
  194. fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
  195. /*
  196. * We don't need to touch the exception register, so
  197. * reload the hardware state.
  198. */
  199. if (!err)
  200. fpsimd_update_current_state(&fpsimd);
  201. return err ? -EFAULT : 0;
  202. }
  203. static int compat_restore_sigframe(struct pt_regs *regs,
  204. struct compat_sigframe __user *sf)
  205. {
  206. int err;
  207. sigset_t set;
  208. struct compat_aux_sigframe __user *aux;
  209. unsigned long psr;
  210. err = get_sigset_t(&set, &sf->uc.uc_sigmask);
  211. if (err == 0) {
  212. sigdelsetmask(&set, ~_BLOCKABLE);
  213. set_current_blocked(&set);
  214. }
  215. __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
  216. __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
  217. __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
  218. __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
  219. __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
  220. __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
  221. __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
  222. __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
  223. __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
  224. __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
  225. __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
  226. __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
  227. __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
  228. __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
  229. __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
  230. __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
  231. __get_user_error(psr, &sf->uc.uc_mcontext.arm_cpsr, err);
  232. regs->pstate = compat_psr_to_pstate(psr);
  233. /*
  234. * Avoid compat_sys_sigreturn() restarting.
  235. */
  236. forget_syscall(regs);
  237. err |= !valid_user_regs(&regs->user_regs, current);
  238. aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
  239. if (err == 0)
  240. err |= compat_restore_vfp_context(&aux->vfp);
  241. return err;
  242. }
  243. COMPAT_SYSCALL_DEFINE0(sigreturn)
  244. {
  245. struct pt_regs *regs = current_pt_regs();
  246. struct compat_sigframe __user *frame;
  247. /* Always make any pending restarted system calls return -EINTR */
  248. current->restart_block.fn = do_no_restart_syscall;
  249. /*
  250. * Since we stacked the signal on a 64-bit boundary,
  251. * then 'sp' should be word aligned here. If it's
  252. * not, then the user is trying to mess with us.
  253. */
  254. if (regs->compat_sp & 7)
  255. goto badframe;
  256. frame = (struct compat_sigframe __user *)regs->compat_sp;
  257. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  258. goto badframe;
  259. if (compat_restore_sigframe(regs, frame))
  260. goto badframe;
  261. return regs->regs[0];
  262. badframe:
  263. arm64_notify_segfault(regs->compat_sp);
  264. return 0;
  265. }
  266. COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
  267. {
  268. struct pt_regs *regs = current_pt_regs();
  269. struct compat_rt_sigframe __user *frame;
  270. /* Always make any pending restarted system calls return -EINTR */
  271. current->restart_block.fn = do_no_restart_syscall;
  272. /*
  273. * Since we stacked the signal on a 64-bit boundary,
  274. * then 'sp' should be word aligned here. If it's
  275. * not, then the user is trying to mess with us.
  276. */
  277. if (regs->compat_sp & 7)
  278. goto badframe;
  279. frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
  280. if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
  281. goto badframe;
  282. if (compat_restore_sigframe(regs, &frame->sig))
  283. goto badframe;
  284. if (compat_restore_altstack(&frame->sig.uc.uc_stack))
  285. goto badframe;
  286. return regs->regs[0];
  287. badframe:
  288. arm64_notify_segfault(regs->compat_sp);
  289. return 0;
  290. }
  291. static void __user *compat_get_sigframe(struct ksignal *ksig,
  292. struct pt_regs *regs,
  293. int framesize)
  294. {
  295. compat_ulong_t sp = sigsp(regs->compat_sp, ksig);
  296. void __user *frame;
  297. /*
  298. * ATPCS B01 mandates 8-byte alignment
  299. */
  300. frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
  301. /*
  302. * Check that we can actually write to the signal frame.
  303. */
  304. if (!access_ok(VERIFY_WRITE, frame, framesize))
  305. frame = NULL;
  306. return frame;
  307. }
  308. static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  309. compat_ulong_t __user *rc, void __user *frame,
  310. int usig)
  311. {
  312. compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
  313. compat_ulong_t retcode;
  314. compat_ulong_t spsr = regs->pstate & ~(PSR_f | PSR_AA32_E_BIT);
  315. int thumb;
  316. /* Check if the handler is written for ARM or Thumb */
  317. thumb = handler & 1;
  318. if (thumb)
  319. spsr |= PSR_AA32_T_BIT;
  320. else
  321. spsr &= ~PSR_AA32_T_BIT;
  322. /* The IT state must be cleared for both ARM and Thumb-2 */
  323. spsr &= ~PSR_AA32_IT_MASK;
  324. /* Restore the original endianness */
  325. spsr |= PSR_AA32_ENDSTATE;
  326. if (ka->sa.sa_flags & SA_RESTORER) {
  327. retcode = ptr_to_compat(ka->sa.sa_restorer);
  328. } else {
  329. /* Set up sigreturn pointer */
  330. unsigned int idx = thumb << 1;
  331. if (ka->sa.sa_flags & SA_SIGINFO)
  332. idx += 3;
  333. retcode = AARCH32_VECTORS_BASE +
  334. AARCH32_KERN_SIGRET_CODE_OFFSET +
  335. (idx << 2) + thumb;
  336. }
  337. regs->regs[0] = usig;
  338. regs->compat_sp = ptr_to_compat(frame);
  339. regs->compat_lr = retcode;
  340. regs->pc = handler;
  341. regs->pstate = spsr;
  342. }
  343. static int compat_setup_sigframe(struct compat_sigframe __user *sf,
  344. struct pt_regs *regs, sigset_t *set)
  345. {
  346. struct compat_aux_sigframe __user *aux;
  347. unsigned long psr = pstate_to_compat_psr(regs->pstate);
  348. int err = 0;
  349. __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
  350. __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
  351. __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
  352. __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
  353. __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
  354. __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
  355. __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
  356. __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
  357. __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
  358. __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
  359. __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
  360. __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
  361. __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
  362. __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
  363. __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
  364. __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
  365. __put_user_error(psr, &sf->uc.uc_mcontext.arm_cpsr, err);
  366. __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
  367. /* set the compat FSR WnR */
  368. __put_user_error(!!(current->thread.fault_code & ESR_ELx_WNR) <<
  369. FSR_WRITE_SHIFT, &sf->uc.uc_mcontext.error_code, err);
  370. __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
  371. __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
  372. err |= put_sigset_t(&sf->uc.uc_sigmask, set);
  373. aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
  374. if (err == 0)
  375. err |= compat_preserve_vfp_context(&aux->vfp);
  376. __put_user_error(0, &aux->end_magic, err);
  377. return err;
  378. }
  379. /*
  380. * 32-bit signal handling routines called from signal.c
  381. */
  382. int compat_setup_rt_frame(int usig, struct ksignal *ksig,
  383. sigset_t *set, struct pt_regs *regs)
  384. {
  385. struct compat_rt_sigframe __user *frame;
  386. int err = 0;
  387. frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
  388. if (!frame)
  389. return 1;
  390. err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
  391. __put_user_error(0, &frame->sig.uc.uc_flags, err);
  392. __put_user_error(0, &frame->sig.uc.uc_link, err);
  393. err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp);
  394. err |= compat_setup_sigframe(&frame->sig, regs, set);
  395. if (err == 0) {
  396. compat_setup_return(regs, &ksig->ka, frame->sig.retcode, frame, usig);
  397. regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
  398. regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
  399. }
  400. return err;
  401. }
  402. int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set,
  403. struct pt_regs *regs)
  404. {
  405. struct compat_sigframe __user *frame;
  406. int err = 0;
  407. frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
  408. if (!frame)
  409. return 1;
  410. __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
  411. err |= compat_setup_sigframe(frame, regs, set);
  412. if (err == 0)
  413. compat_setup_return(regs, &ksig->ka, frame->retcode, frame, usig);
  414. return err;
  415. }
  416. void compat_setup_restart_syscall(struct pt_regs *regs)
  417. {
  418. regs->regs[7] = __NR_compat_restart_syscall;
  419. }