linux/arch/sparc/kernel/signal_32.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*  linux/arch/sparc/kernel/signal.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 *  Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
   6 *  Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
   7 *  Copyright (C) 1997 Eddie C. Dost   (ecd@skynet.be)
   8 */
   9
  10#include <linux/sched.h>
  11#include <linux/kernel.h>
  12#include <linux/signal.h>
  13#include <linux/errno.h>
  14#include <linux/wait.h>
  15#include <linux/ptrace.h>
  16#include <linux/unistd.h>
  17#include <linux/mm.h>
  18#include <linux/tty.h>
  19#include <linux/smp.h>
  20#include <linux/binfmts.h>      /* do_coredum */
  21#include <linux/bitops.h>
  22#include <linux/tracehook.h>
  23
  24#include <linux/uaccess.h>
  25#include <asm/ptrace.h>
  26#include <asm/pgalloc.h>
  27#include <asm/pgtable.h>
  28#include <asm/cacheflush.h>     /* flush_sig_insns */
  29#include <asm/switch_to.h>
  30
  31#include "sigutil.h"
  32#include "kernel.h"
  33
  34extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
  35                   void *fpqueue, unsigned long *fpqdepth);
  36extern void fpload(unsigned long *fpregs, unsigned long *fsr);
  37
  38struct signal_frame {
  39        struct sparc_stackf     ss;
  40        __siginfo32_t           info;
  41        __siginfo_fpu_t __user  *fpu_save;
  42        unsigned long           insns[2] __attribute__ ((aligned (8)));
  43        unsigned int            extramask[_NSIG_WORDS - 1];
  44        unsigned int            extra_size; /* Should be 0 */
  45        __siginfo_rwin_t __user *rwin_save;
  46} __attribute__((aligned(8)));
  47
  48struct rt_signal_frame {
  49        struct sparc_stackf     ss;
  50        siginfo_t               info;
  51        struct pt_regs          regs;
  52        sigset_t                mask;
  53        __siginfo_fpu_t __user  *fpu_save;
  54        unsigned int            insns[2];
  55        stack_t                 stack;
  56        unsigned int            extra_size; /* Should be 0 */
  57        __siginfo_rwin_t __user *rwin_save;
  58} __attribute__((aligned(8)));
  59
  60/* Align macros */
  61#define SF_ALIGNEDSZ  (((sizeof(struct signal_frame) + 7) & (~7)))
  62#define RT_ALIGNEDSZ  (((sizeof(struct rt_signal_frame) + 7) & (~7)))
  63
  64/* Checks if the fp is valid.  We always build signal frames which are
  65 * 16-byte aligned, therefore we can always enforce that the restore
  66 * frame has that property as well.
  67 */
  68static inline bool invalid_frame_pointer(void __user *fp, int fplen)
  69{
  70        if ((((unsigned long) fp) & 15) || !__access_ok((unsigned long)fp, fplen))
  71                return true;
  72
  73        return false;
  74}
  75
  76asmlinkage void do_sigreturn(struct pt_regs *regs)
  77{
  78        unsigned long up_psr, pc, npc, ufp;
  79        struct signal_frame __user *sf;
  80        sigset_t set;
  81        __siginfo_fpu_t __user *fpu_save;
  82        __siginfo_rwin_t __user *rwin_save;
  83        int err;
  84
  85        /* Always make any pending restarted system calls return -EINTR */
  86        current->restart_block.fn = do_no_restart_syscall;
  87
  88        synchronize_user_stack();
  89
  90        sf = (struct signal_frame __user *) regs->u_regs[UREG_FP];
  91
  92        /* 1. Make sure we are not getting garbage from the user */
  93        if (invalid_frame_pointer(sf, sizeof(*sf)))
  94                goto segv_and_exit;
  95
  96        if (get_user(ufp, &sf->info.si_regs.u_regs[UREG_FP]))
  97                goto segv_and_exit;
  98
  99        if (ufp & 0x7)
 100                goto segv_and_exit;
 101
 102        err = __get_user(pc,  &sf->info.si_regs.pc);
 103        err |= __get_user(npc, &sf->info.si_regs.npc);
 104
 105        if ((pc | npc) & 3)
 106                goto segv_and_exit;
 107
 108        /* 2. Restore the state */
 109        up_psr = regs->psr;
 110        err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs));
 111
 112        /* User can only change condition codes and FPU enabling in %psr. */
 113        regs->psr = (up_psr & ~(PSR_ICC | PSR_EF))
 114                  | (regs->psr & (PSR_ICC | PSR_EF));
 115
 116        /* Prevent syscall restart.  */
 117        pt_regs_clear_syscall(regs);
 118
 119        err |= __get_user(fpu_save, &sf->fpu_save);
 120        if (fpu_save)
 121                err |= restore_fpu_state(regs, fpu_save);
 122        err |= __get_user(rwin_save, &sf->rwin_save);
 123        if (rwin_save)
 124                err |= restore_rwin_state(rwin_save);
 125
 126        /* This is pretty much atomic, no amount locking would prevent
 127         * the races which exist anyways.
 128         */
 129        err |= __get_user(set.sig[0], &sf->info.si_mask);
 130        err |= __copy_from_user(&set.sig[1], &sf->extramask,
 131                                (_NSIG_WORDS-1) * sizeof(unsigned int));
 132                           
 133        if (err)
 134                goto segv_and_exit;
 135
 136        set_current_blocked(&set);
 137        return;
 138
 139segv_and_exit:
 140        force_sig(SIGSEGV, current);
 141}
 142
 143asmlinkage void do_rt_sigreturn(struct pt_regs *regs)
 144{
 145        struct rt_signal_frame __user *sf;
 146        unsigned int psr, pc, npc, ufp;
 147        __siginfo_fpu_t __user *fpu_save;
 148        __siginfo_rwin_t __user *rwin_save;
 149        sigset_t set;
 150        int err;
 151
 152        synchronize_user_stack();
 153        sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP];
 154        if (invalid_frame_pointer(sf, sizeof(*sf)))
 155                goto segv;
 156
 157        if (get_user(ufp, &sf->regs.u_regs[UREG_FP]))
 158                goto segv;
 159
 160        if (ufp & 0x7)
 161                goto segv;
 162
 163        err = __get_user(pc, &sf->regs.pc);
 164        err |= __get_user(npc, &sf->regs.npc);
 165        err |= ((pc | npc) & 0x03);
 166
 167        err |= __get_user(regs->y, &sf->regs.y);
 168        err |= __get_user(psr, &sf->regs.psr);
 169
 170        err |= __copy_from_user(&regs->u_regs[UREG_G1],
 171                                &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32));
 172
 173        regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC);
 174
 175        /* Prevent syscall restart.  */
 176        pt_regs_clear_syscall(regs);
 177
 178        err |= __get_user(fpu_save, &sf->fpu_save);
 179        if (!err && fpu_save)
 180                err |= restore_fpu_state(regs, fpu_save);
 181        err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
 182        err |= restore_altstack(&sf->stack);
 183        
 184        if (err)
 185                goto segv;
 186                
 187        regs->pc = pc;
 188        regs->npc = npc;
 189        
 190        err |= __get_user(rwin_save, &sf->rwin_save);
 191        if (!err && rwin_save) {
 192                if (restore_rwin_state(rwin_save))
 193                        goto segv;
 194        }
 195
 196        set_current_blocked(&set);
 197        return;
 198segv:
 199        force_sig(SIGSEGV, current);
 200}
 201
 202static inline void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
 203{
 204        unsigned long sp = regs->u_regs[UREG_FP];
 205
 206        /*
 207         * If we are on the alternate signal stack and would overflow it, don't.
 208         * Return an always-bogus address instead so we will die with SIGSEGV.
 209         */
 210        if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
 211                return (void __user *) -1L;
 212
 213        /* This is the X/Open sanctioned signal stack switching.  */
 214        sp = sigsp(sp, ksig) - framesize;
 215
 216        /* Always align the stack frame.  This handles two cases.  First,
 217         * sigaltstack need not be mindful of platform specific stack
 218         * alignment.  Second, if we took this signal because the stack
 219         * is not aligned properly, we'd like to take the signal cleanly
 220         * and report that.
 221         */
 222        sp &= ~15UL;
 223
 224        return (void __user *) sp;
 225}
 226
 227static int setup_frame(struct ksignal *ksig, struct pt_regs *regs,
 228                       sigset_t *oldset)
 229{
 230        struct signal_frame __user *sf;
 231        int sigframe_size, err, wsaved;
 232        void __user *tail;
 233
 234        /* 1. Make sure everything is clean */
 235        synchronize_user_stack();
 236
 237        wsaved = current_thread_info()->w_saved;
 238
 239        sigframe_size = sizeof(*sf);
 240        if (used_math())
 241                sigframe_size += sizeof(__siginfo_fpu_t);
 242        if (wsaved)
 243                sigframe_size += sizeof(__siginfo_rwin_t);
 244
 245        sf = (struct signal_frame __user *)
 246                get_sigframe(ksig, regs, sigframe_size);
 247
 248        if (invalid_frame_pointer(sf, sigframe_size)) {
 249                do_exit(SIGILL);
 250                return -EINVAL;
 251        }
 252
 253        tail = sf + 1;
 254
 255        /* 2. Save the current process state */
 256        err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs));
 257        
 258        err |= __put_user(0, &sf->extra_size);
 259
 260        if (used_math()) {
 261                __siginfo_fpu_t __user *fp = tail;
 262                tail += sizeof(*fp);
 263                err |= save_fpu_state(regs, fp);
 264                err |= __put_user(fp, &sf->fpu_save);
 265        } else {
 266                err |= __put_user(0, &sf->fpu_save);
 267        }
 268        if (wsaved) {
 269                __siginfo_rwin_t __user *rwp = tail;
 270                tail += sizeof(*rwp);
 271                err |= save_rwin_state(wsaved, rwp);
 272                err |= __put_user(rwp, &sf->rwin_save);
 273        } else {
 274                err |= __put_user(0, &sf->rwin_save);
 275        }
 276
 277        err |= __put_user(oldset->sig[0], &sf->info.si_mask);
 278        err |= __copy_to_user(sf->extramask, &oldset->sig[1],
 279                              (_NSIG_WORDS - 1) * sizeof(unsigned int));
 280        if (!wsaved) {
 281                err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
 282                                      sizeof(struct reg_window32));
 283        } else {
 284                struct reg_window32 *rp;
 285
 286                rp = &current_thread_info()->reg_window[wsaved - 1];
 287                err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
 288        }
 289        if (err)
 290                return err;
 291        
 292        /* 3. signal handler back-trampoline and parameters */
 293        regs->u_regs[UREG_FP] = (unsigned long) sf;
 294        regs->u_regs[UREG_I0] = ksig->sig;
 295        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
 296        regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
 297
 298        /* 4. signal handler */
 299        regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
 300        regs->npc = (regs->pc + 4);
 301
 302        /* 5. return to kernel instructions */
 303        if (ksig->ka.ka_restorer)
 304                regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
 305        else {
 306                regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
 307
 308                /* mov __NR_sigreturn, %g1 */
 309                err |= __put_user(0x821020d8, &sf->insns[0]);
 310
 311                /* t 0x10 */
 312                err |= __put_user(0x91d02010, &sf->insns[1]);
 313                if (err)
 314                        return err;
 315
 316                /* Flush instruction space. */
 317                flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
 318        }
 319        return 0;
 320}
 321
 322static int setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs,
 323                          sigset_t *oldset)
 324{
 325        struct rt_signal_frame __user *sf;
 326        int sigframe_size, wsaved;
 327        void __user *tail;
 328        unsigned int psr;
 329        int err;
 330
 331        synchronize_user_stack();
 332        wsaved = current_thread_info()->w_saved;
 333        sigframe_size = sizeof(*sf);
 334        if (used_math())
 335                sigframe_size += sizeof(__siginfo_fpu_t);
 336        if (wsaved)
 337                sigframe_size += sizeof(__siginfo_rwin_t);
 338        sf = (struct rt_signal_frame __user *)
 339                get_sigframe(ksig, regs, sigframe_size);
 340        if (invalid_frame_pointer(sf, sigframe_size)) {
 341                do_exit(SIGILL);
 342                return -EINVAL;
 343        }
 344
 345        tail = sf + 1;
 346        err  = __put_user(regs->pc, &sf->regs.pc);
 347        err |= __put_user(regs->npc, &sf->regs.npc);
 348        err |= __put_user(regs->y, &sf->regs.y);
 349        psr = regs->psr;
 350        if (used_math())
 351                psr |= PSR_EF;
 352        err |= __put_user(psr, &sf->regs.psr);
 353        err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs));
 354        err |= __put_user(0, &sf->extra_size);
 355
 356        if (psr & PSR_EF) {
 357                __siginfo_fpu_t __user *fp = tail;
 358                tail += sizeof(*fp);
 359                err |= save_fpu_state(regs, fp);
 360                err |= __put_user(fp, &sf->fpu_save);
 361        } else {
 362                err |= __put_user(0, &sf->fpu_save);
 363        }
 364        if (wsaved) {
 365                __siginfo_rwin_t __user *rwp = tail;
 366                tail += sizeof(*rwp);
 367                err |= save_rwin_state(wsaved, rwp);
 368                err |= __put_user(rwp, &sf->rwin_save);
 369        } else {
 370                err |= __put_user(0, &sf->rwin_save);
 371        }
 372        err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t));
 373        
 374        /* Setup sigaltstack */
 375        err |= __save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
 376        
 377        if (!wsaved) {
 378                err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP],
 379                                      sizeof(struct reg_window32));
 380        } else {
 381                struct reg_window32 *rp;
 382
 383                rp = &current_thread_info()->reg_window[wsaved - 1];
 384                err |= __copy_to_user(sf, rp, sizeof(struct reg_window32));
 385        }
 386
 387        err |= copy_siginfo_to_user(&sf->info, &ksig->info);
 388
 389        if (err)
 390                return err;
 391
 392        regs->u_regs[UREG_FP] = (unsigned long) sf;
 393        regs->u_regs[UREG_I0] = ksig->sig;
 394        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
 395        regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
 396
 397        regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
 398        regs->npc = (regs->pc + 4);
 399
 400        if (ksig->ka.ka_restorer)
 401                regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
 402        else {
 403                regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
 404
 405                /* mov __NR_sigreturn, %g1 */
 406                err |= __put_user(0x821020d8, &sf->insns[0]);
 407
 408                /* t 0x10 */
 409                err |= __put_user(0x91d02010, &sf->insns[1]);
 410                if (err)
 411                        return err;
 412
 413                /* Flush instruction space. */
 414                flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
 415        }
 416        return 0;
 417}
 418
 419static inline void
 420handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 421{
 422        sigset_t *oldset = sigmask_to_save();
 423        int err;
 424
 425        if (ksig->ka.sa.sa_flags & SA_SIGINFO)
 426                err = setup_rt_frame(ksig, regs, oldset);
 427        else
 428                err = setup_frame(ksig, regs, oldset);
 429        signal_setup_done(err, ksig, 0);
 430}
 431
 432static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
 433                                   struct sigaction *sa)
 434{
 435        switch(regs->u_regs[UREG_I0]) {
 436        case ERESTART_RESTARTBLOCK:
 437        case ERESTARTNOHAND:
 438        no_system_call_restart:
 439                regs->u_regs[UREG_I0] = EINTR;
 440                regs->psr |= PSR_C;
 441                break;
 442        case ERESTARTSYS:
 443                if (!(sa->sa_flags & SA_RESTART))
 444                        goto no_system_call_restart;
 445                /* fallthrough */
 446        case ERESTARTNOINTR:
 447                regs->u_regs[UREG_I0] = orig_i0;
 448                regs->pc -= 4;
 449                regs->npc -= 4;
 450        }
 451}
 452
 453/* Note that 'init' is a special process: it doesn't get signals it doesn't
 454 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 455 * mistake.
 456 */
 457static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
 458{
 459        struct ksignal ksig;
 460        int restart_syscall;
 461        bool has_handler;
 462
 463        /* It's a lot of work and synchronization to add a new ptrace
 464         * register for GDB to save and restore in order to get
 465         * orig_i0 correct for syscall restarts when debugging.
 466         *
 467         * Although it should be the case that most of the global
 468         * registers are volatile across a system call, glibc already
 469         * depends upon that fact that we preserve them.  So we can't
 470         * just use any global register to save away the orig_i0 value.
 471         *
 472         * In particular %g2, %g3, %g4, and %g5 are all assumed to be
 473         * preserved across a system call trap by various pieces of
 474         * code in glibc.
 475         *
 476         * %g7 is used as the "thread register".   %g6 is not used in
 477         * any fixed manner.  %g6 is used as a scratch register and
 478         * a compiler temporary, but it's value is never used across
 479         * a system call.  Therefore %g6 is usable for orig_i0 storage.
 480         */
 481        if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C))
 482                regs->u_regs[UREG_G6] = orig_i0;
 483
 484        has_handler = get_signal(&ksig);
 485
 486        /* If the debugger messes with the program counter, it clears
 487         * the software "in syscall" bit, directing us to not perform
 488         * a syscall restart.
 489         */
 490        restart_syscall = 0;
 491        if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) {
 492                restart_syscall = 1;
 493                orig_i0 = regs->u_regs[UREG_G6];
 494        }
 495
 496        if (has_handler) {
 497                if (restart_syscall)
 498                        syscall_restart(orig_i0, regs, &ksig.ka.sa);
 499                handle_signal(&ksig, regs);
 500        } else {
 501                if (restart_syscall) {
 502                        switch (regs->u_regs[UREG_I0]) {
 503                        case ERESTARTNOHAND:
 504                        case ERESTARTSYS:
 505                        case ERESTARTNOINTR:
 506                                /* replay the system call when we are done */
 507                                regs->u_regs[UREG_I0] = orig_i0;
 508                                regs->pc -= 4;
 509                                regs->npc -= 4;
 510                                pt_regs_clear_syscall(regs);
 511                        case ERESTART_RESTARTBLOCK:
 512                                regs->u_regs[UREG_G1] = __NR_restart_syscall;
 513                                regs->pc -= 4;
 514                                regs->npc -= 4;
 515                                pt_regs_clear_syscall(regs);
 516                        }
 517                }
 518                restore_saved_sigmask();
 519        }
 520}
 521
 522void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0,
 523                      unsigned long thread_info_flags)
 524{
 525        if (thread_info_flags & _TIF_SIGPENDING)
 526                do_signal(regs, orig_i0);
 527        if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 528                clear_thread_flag(TIF_NOTIFY_RESUME);
 529                tracehook_notify_resume(regs);
 530        }
 531}
 532
 533asmlinkage int do_sys_sigstack(struct sigstack __user *ssptr,
 534                               struct sigstack __user *ossptr,
 535                               unsigned long sp)
 536{
 537        int ret = -EFAULT;
 538
 539        /* First see if old state is wanted. */
 540        if (ossptr) {
 541                if (put_user(current->sas_ss_sp + current->sas_ss_size,
 542                             &ossptr->the_stack) ||
 543                    __put_user(on_sig_stack(sp), &ossptr->cur_status))
 544                        goto out;
 545        }
 546
 547        /* Now see if we want to update the new state. */
 548        if (ssptr) {
 549                char *ss_sp;
 550
 551                if (get_user(ss_sp, &ssptr->the_stack))
 552                        goto out;
 553                /* If the current stack was set with sigaltstack, don't
 554                   swap stacks while we are on it.  */
 555                ret = -EPERM;
 556                if (current->sas_ss_sp && on_sig_stack(sp))
 557                        goto out;
 558
 559                /* Since we don't know the extent of the stack, and we don't
 560                   track onstack-ness, but rather calculate it, we must
 561                   presume a size.  Ho hum this interface is lossy.  */
 562                current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
 563                current->sas_ss_size = SIGSTKSZ;
 564        }
 565        ret = 0;
 566out:
 567        return ret;
 568}
 569