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