0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/compat.h>
0011 #include <linux/signal.h>
0012 #include <linux/syscalls.h>
0013 #include <linux/ratelimit.h>
0014
0015 #include <asm/esr.h>
0016 #include <asm/fpsimd.h>
0017 #include <asm/signal32.h>
0018 #include <asm/traps.h>
0019 #include <linux/uaccess.h>
0020 #include <asm/unistd.h>
0021 #include <asm/vdso.h>
0022
0023 struct compat_vfp_sigframe {
0024 compat_ulong_t magic;
0025 compat_ulong_t size;
0026 struct compat_user_vfp {
0027 compat_u64 fpregs[32];
0028 compat_ulong_t fpscr;
0029 } ufp;
0030 struct compat_user_vfp_exc {
0031 compat_ulong_t fpexc;
0032 compat_ulong_t fpinst;
0033 compat_ulong_t fpinst2;
0034 } ufp_exc;
0035 } __attribute__((__aligned__(8)));
0036
0037 #define VFP_MAGIC 0x56465001
0038 #define VFP_STORAGE_SIZE sizeof(struct compat_vfp_sigframe)
0039
0040 #define FSR_WRITE_SHIFT (11)
0041
0042 struct compat_aux_sigframe {
0043 struct compat_vfp_sigframe vfp;
0044
0045
0046 unsigned long end_magic;
0047 } __attribute__((__aligned__(8)));
0048
0049 static inline int put_sigset_t(compat_sigset_t __user *uset, sigset_t *set)
0050 {
0051 compat_sigset_t cset;
0052
0053 cset.sig[0] = set->sig[0] & 0xffffffffull;
0054 cset.sig[1] = set->sig[0] >> 32;
0055
0056 return copy_to_user(uset, &cset, sizeof(*uset));
0057 }
0058
0059 static inline int get_sigset_t(sigset_t *set,
0060 const compat_sigset_t __user *uset)
0061 {
0062 compat_sigset_t s32;
0063
0064 if (copy_from_user(&s32, uset, sizeof(*uset)))
0065 return -EFAULT;
0066
0067 set->sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
0068 return 0;
0069 }
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 union __fpsimd_vreg {
0080 __uint128_t raw;
0081 struct {
0082 #ifdef __AARCH64EB__
0083 u64 hi;
0084 u64 lo;
0085 #else
0086 u64 lo;
0087 u64 hi;
0088 #endif
0089 };
0090 };
0091
0092 static int compat_preserve_vfp_context(struct compat_vfp_sigframe __user *frame)
0093 {
0094 struct user_fpsimd_state const *fpsimd =
0095 ¤t->thread.uw.fpsimd_state;
0096 compat_ulong_t magic = VFP_MAGIC;
0097 compat_ulong_t size = VFP_STORAGE_SIZE;
0098 compat_ulong_t fpscr, fpexc;
0099 int i, err = 0;
0100
0101
0102
0103
0104
0105
0106 fpsimd_signal_preserve_current_state();
0107
0108
0109 __put_user_error(magic, &frame->magic, err);
0110 __put_user_error(size, &frame->size, err);
0111
0112
0113
0114
0115
0116 for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
0117 union __fpsimd_vreg vreg = {
0118 .raw = fpsimd->vregs[i >> 1],
0119 };
0120
0121 __put_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
0122 __put_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
0123 }
0124
0125
0126 fpscr = (fpsimd->fpsr & VFP_FPSCR_STAT_MASK) |
0127 (fpsimd->fpcr & VFP_FPSCR_CTRL_MASK);
0128 __put_user_error(fpscr, &frame->ufp.fpscr, err);
0129
0130
0131
0132
0133
0134 fpexc = (1 << 30);
0135 __put_user_error(fpexc, &frame->ufp_exc.fpexc, err);
0136 __put_user_error(0, &frame->ufp_exc.fpinst, err);
0137 __put_user_error(0, &frame->ufp_exc.fpinst2, err);
0138
0139 return err ? -EFAULT : 0;
0140 }
0141
0142 static int compat_restore_vfp_context(struct compat_vfp_sigframe __user *frame)
0143 {
0144 struct user_fpsimd_state fpsimd;
0145 compat_ulong_t magic = VFP_MAGIC;
0146 compat_ulong_t size = VFP_STORAGE_SIZE;
0147 compat_ulong_t fpscr;
0148 int i, err = 0;
0149
0150 __get_user_error(magic, &frame->magic, err);
0151 __get_user_error(size, &frame->size, err);
0152
0153 if (err)
0154 return -EFAULT;
0155 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
0156 return -EINVAL;
0157
0158
0159 for (i = 0; i < ARRAY_SIZE(frame->ufp.fpregs); i += 2) {
0160 union __fpsimd_vreg vreg;
0161
0162 __get_user_error(vreg.lo, &frame->ufp.fpregs[i], err);
0163 __get_user_error(vreg.hi, &frame->ufp.fpregs[i + 1], err);
0164 fpsimd.vregs[i >> 1] = vreg.raw;
0165 }
0166
0167
0168 __get_user_error(fpscr, &frame->ufp.fpscr, err);
0169 fpsimd.fpsr = fpscr & VFP_FPSCR_STAT_MASK;
0170 fpsimd.fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
0171
0172
0173
0174
0175
0176 if (!err)
0177 fpsimd_update_current_state(&fpsimd);
0178
0179 return err ? -EFAULT : 0;
0180 }
0181
0182 static int compat_restore_sigframe(struct pt_regs *regs,
0183 struct compat_sigframe __user *sf)
0184 {
0185 int err;
0186 sigset_t set;
0187 struct compat_aux_sigframe __user *aux;
0188 unsigned long psr;
0189
0190 err = get_sigset_t(&set, &sf->uc.uc_sigmask);
0191 if (err == 0)
0192 set_current_blocked(&set);
0193
0194 __get_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
0195 __get_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
0196 __get_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
0197 __get_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
0198 __get_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
0199 __get_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
0200 __get_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
0201 __get_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
0202 __get_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
0203 __get_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
0204 __get_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
0205 __get_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
0206 __get_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
0207 __get_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
0208 __get_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
0209 __get_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
0210 __get_user_error(psr, &sf->uc.uc_mcontext.arm_cpsr, err);
0211
0212 regs->pstate = compat_psr_to_pstate(psr);
0213
0214
0215
0216
0217 forget_syscall(regs);
0218
0219 err |= !valid_user_regs(®s->user_regs, current);
0220
0221 aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
0222 if (err == 0 && system_supports_fpsimd())
0223 err |= compat_restore_vfp_context(&aux->vfp);
0224
0225 return err;
0226 }
0227
0228 COMPAT_SYSCALL_DEFINE0(sigreturn)
0229 {
0230 struct pt_regs *regs = current_pt_regs();
0231 struct compat_sigframe __user *frame;
0232
0233
0234 current->restart_block.fn = do_no_restart_syscall;
0235
0236
0237
0238
0239
0240
0241 if (regs->compat_sp & 7)
0242 goto badframe;
0243
0244 frame = (struct compat_sigframe __user *)regs->compat_sp;
0245
0246 if (!access_ok(frame, sizeof (*frame)))
0247 goto badframe;
0248
0249 if (compat_restore_sigframe(regs, frame))
0250 goto badframe;
0251
0252 return regs->regs[0];
0253
0254 badframe:
0255 arm64_notify_segfault(regs->compat_sp);
0256 return 0;
0257 }
0258
0259 COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
0260 {
0261 struct pt_regs *regs = current_pt_regs();
0262 struct compat_rt_sigframe __user *frame;
0263
0264
0265 current->restart_block.fn = do_no_restart_syscall;
0266
0267
0268
0269
0270
0271
0272 if (regs->compat_sp & 7)
0273 goto badframe;
0274
0275 frame = (struct compat_rt_sigframe __user *)regs->compat_sp;
0276
0277 if (!access_ok(frame, sizeof (*frame)))
0278 goto badframe;
0279
0280 if (compat_restore_sigframe(regs, &frame->sig))
0281 goto badframe;
0282
0283 if (compat_restore_altstack(&frame->sig.uc.uc_stack))
0284 goto badframe;
0285
0286 return regs->regs[0];
0287
0288 badframe:
0289 arm64_notify_segfault(regs->compat_sp);
0290 return 0;
0291 }
0292
0293 static void __user *compat_get_sigframe(struct ksignal *ksig,
0294 struct pt_regs *regs,
0295 int framesize)
0296 {
0297 compat_ulong_t sp = sigsp(regs->compat_sp, ksig);
0298 void __user *frame;
0299
0300
0301
0302
0303 frame = compat_ptr((compat_uptr_t)((sp - framesize) & ~7));
0304
0305
0306
0307
0308 if (!access_ok(frame, framesize))
0309 frame = NULL;
0310
0311 return frame;
0312 }
0313
0314 static void compat_setup_return(struct pt_regs *regs, struct k_sigaction *ka,
0315 compat_ulong_t __user *rc, void __user *frame,
0316 int usig)
0317 {
0318 compat_ulong_t handler = ptr_to_compat(ka->sa.sa_handler);
0319 compat_ulong_t retcode;
0320 compat_ulong_t spsr = regs->pstate & ~(PSR_f | PSR_AA32_E_BIT);
0321 int thumb;
0322
0323
0324 thumb = handler & 1;
0325
0326 if (thumb)
0327 spsr |= PSR_AA32_T_BIT;
0328 else
0329 spsr &= ~PSR_AA32_T_BIT;
0330
0331
0332 spsr &= ~PSR_AA32_IT_MASK;
0333
0334
0335 spsr |= PSR_AA32_ENDSTATE;
0336
0337 if (ka->sa.sa_flags & SA_RESTORER) {
0338 retcode = ptr_to_compat(ka->sa.sa_restorer);
0339 } else {
0340
0341 unsigned int idx = thumb << 1;
0342
0343 if (ka->sa.sa_flags & SA_SIGINFO)
0344 idx += 3;
0345
0346 retcode = (unsigned long)current->mm->context.sigpage +
0347 (idx << 2) + thumb;
0348 }
0349
0350 regs->regs[0] = usig;
0351 regs->compat_sp = ptr_to_compat(frame);
0352 regs->compat_lr = retcode;
0353 regs->pc = handler;
0354 regs->pstate = spsr;
0355 }
0356
0357 static int compat_setup_sigframe(struct compat_sigframe __user *sf,
0358 struct pt_regs *regs, sigset_t *set)
0359 {
0360 struct compat_aux_sigframe __user *aux;
0361 unsigned long psr = pstate_to_compat_psr(regs->pstate);
0362 int err = 0;
0363
0364 __put_user_error(regs->regs[0], &sf->uc.uc_mcontext.arm_r0, err);
0365 __put_user_error(regs->regs[1], &sf->uc.uc_mcontext.arm_r1, err);
0366 __put_user_error(regs->regs[2], &sf->uc.uc_mcontext.arm_r2, err);
0367 __put_user_error(regs->regs[3], &sf->uc.uc_mcontext.arm_r3, err);
0368 __put_user_error(regs->regs[4], &sf->uc.uc_mcontext.arm_r4, err);
0369 __put_user_error(regs->regs[5], &sf->uc.uc_mcontext.arm_r5, err);
0370 __put_user_error(regs->regs[6], &sf->uc.uc_mcontext.arm_r6, err);
0371 __put_user_error(regs->regs[7], &sf->uc.uc_mcontext.arm_r7, err);
0372 __put_user_error(regs->regs[8], &sf->uc.uc_mcontext.arm_r8, err);
0373 __put_user_error(regs->regs[9], &sf->uc.uc_mcontext.arm_r9, err);
0374 __put_user_error(regs->regs[10], &sf->uc.uc_mcontext.arm_r10, err);
0375 __put_user_error(regs->regs[11], &sf->uc.uc_mcontext.arm_fp, err);
0376 __put_user_error(regs->regs[12], &sf->uc.uc_mcontext.arm_ip, err);
0377 __put_user_error(regs->compat_sp, &sf->uc.uc_mcontext.arm_sp, err);
0378 __put_user_error(regs->compat_lr, &sf->uc.uc_mcontext.arm_lr, err);
0379 __put_user_error(regs->pc, &sf->uc.uc_mcontext.arm_pc, err);
0380 __put_user_error(psr, &sf->uc.uc_mcontext.arm_cpsr, err);
0381
0382 __put_user_error((compat_ulong_t)0, &sf->uc.uc_mcontext.trap_no, err);
0383
0384 __put_user_error(!!(current->thread.fault_code & ESR_ELx_WNR) <<
0385 FSR_WRITE_SHIFT, &sf->uc.uc_mcontext.error_code, err);
0386 __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
0387 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
0388
0389 err |= put_sigset_t(&sf->uc.uc_sigmask, set);
0390
0391 aux = (struct compat_aux_sigframe __user *) sf->uc.uc_regspace;
0392
0393 if (err == 0 && system_supports_fpsimd())
0394 err |= compat_preserve_vfp_context(&aux->vfp);
0395 __put_user_error(0, &aux->end_magic, err);
0396
0397 return err;
0398 }
0399
0400
0401
0402
0403 int compat_setup_rt_frame(int usig, struct ksignal *ksig,
0404 sigset_t *set, struct pt_regs *regs)
0405 {
0406 struct compat_rt_sigframe __user *frame;
0407 int err = 0;
0408
0409 frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
0410
0411 if (!frame)
0412 return 1;
0413
0414 err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
0415
0416 __put_user_error(0, &frame->sig.uc.uc_flags, err);
0417 __put_user_error(0, &frame->sig.uc.uc_link, err);
0418
0419 err |= __compat_save_altstack(&frame->sig.uc.uc_stack, regs->compat_sp);
0420
0421 err |= compat_setup_sigframe(&frame->sig, regs, set);
0422
0423 if (err == 0) {
0424 compat_setup_return(regs, &ksig->ka, frame->sig.retcode, frame, usig);
0425 regs->regs[1] = (compat_ulong_t)(unsigned long)&frame->info;
0426 regs->regs[2] = (compat_ulong_t)(unsigned long)&frame->sig.uc;
0427 }
0428
0429 return err;
0430 }
0431
0432 int compat_setup_frame(int usig, struct ksignal *ksig, sigset_t *set,
0433 struct pt_regs *regs)
0434 {
0435 struct compat_sigframe __user *frame;
0436 int err = 0;
0437
0438 frame = compat_get_sigframe(ksig, regs, sizeof(*frame));
0439
0440 if (!frame)
0441 return 1;
0442
0443 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
0444
0445 err |= compat_setup_sigframe(frame, regs, set);
0446 if (err == 0)
0447 compat_setup_return(regs, &ksig->ka, frame->retcode, frame, usig);
0448
0449 return err;
0450 }
0451
0452 void compat_setup_restart_syscall(struct pt_regs *regs)
0453 {
0454 regs->regs[7] = __NR_compat_restart_syscall;
0455 }
0456
0457
0458
0459
0460
0461 static_assert(NSIGILL == 11);
0462 static_assert(NSIGFPE == 15);
0463 static_assert(NSIGSEGV == 9);
0464 static_assert(NSIGBUS == 5);
0465 static_assert(NSIGTRAP == 6);
0466 static_assert(NSIGCHLD == 6);
0467 static_assert(NSIGSYS == 2);
0468 static_assert(sizeof(compat_siginfo_t) == 128);
0469 static_assert(__alignof__(compat_siginfo_t) == 4);
0470 static_assert(offsetof(compat_siginfo_t, si_signo) == 0x00);
0471 static_assert(offsetof(compat_siginfo_t, si_errno) == 0x04);
0472 static_assert(offsetof(compat_siginfo_t, si_code) == 0x08);
0473 static_assert(offsetof(compat_siginfo_t, si_pid) == 0x0c);
0474 static_assert(offsetof(compat_siginfo_t, si_uid) == 0x10);
0475 static_assert(offsetof(compat_siginfo_t, si_tid) == 0x0c);
0476 static_assert(offsetof(compat_siginfo_t, si_overrun) == 0x10);
0477 static_assert(offsetof(compat_siginfo_t, si_status) == 0x14);
0478 static_assert(offsetof(compat_siginfo_t, si_utime) == 0x18);
0479 static_assert(offsetof(compat_siginfo_t, si_stime) == 0x1c);
0480 static_assert(offsetof(compat_siginfo_t, si_value) == 0x14);
0481 static_assert(offsetof(compat_siginfo_t, si_int) == 0x14);
0482 static_assert(offsetof(compat_siginfo_t, si_ptr) == 0x14);
0483 static_assert(offsetof(compat_siginfo_t, si_addr) == 0x0c);
0484 static_assert(offsetof(compat_siginfo_t, si_addr_lsb) == 0x10);
0485 static_assert(offsetof(compat_siginfo_t, si_lower) == 0x14);
0486 static_assert(offsetof(compat_siginfo_t, si_upper) == 0x18);
0487 static_assert(offsetof(compat_siginfo_t, si_pkey) == 0x14);
0488 static_assert(offsetof(compat_siginfo_t, si_perf_data) == 0x10);
0489 static_assert(offsetof(compat_siginfo_t, si_perf_type) == 0x14);
0490 static_assert(offsetof(compat_siginfo_t, si_perf_flags) == 0x18);
0491 static_assert(offsetof(compat_siginfo_t, si_band) == 0x0c);
0492 static_assert(offsetof(compat_siginfo_t, si_fd) == 0x10);
0493 static_assert(offsetof(compat_siginfo_t, si_call_addr) == 0x0c);
0494 static_assert(offsetof(compat_siginfo_t, si_syscall) == 0x10);
0495 static_assert(offsetof(compat_siginfo_t, si_arch) == 0x14);