signal32.c 15 KB

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