Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Signal Handling for ARC
0004  *
0005  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
0006  *
0007  * vineetg: Jan 2010 (Restarting of timer related syscalls)
0008  *
0009  * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK)
0010  *  -do_signal() supports TIF_RESTORE_SIGMASK
0011  *  -do_signal() no loner needs oldset, required by OLD sys_sigsuspend
0012  *  -sys_rt_sigsuspend() now comes from generic code, so discard arch implemen
0013  *  -sys_sigsuspend() no longer needs to fudge ptregs, hence that arg removed
0014  *  -sys_sigsuspend() no longer loops for do_signal(), sets TIF_xxx and leaves
0015  *   the job to do_signal()
0016  *
0017  * vineetg: July 2009
0018  *  -Modified Code to support the uClibc provided userland sigreturn stub
0019  *   to avoid kernel synthesing it on user stack at runtime, costing TLB
0020  *   probes and Cache line flushes.
0021  *
0022  * vineetg: July 2009
0023  *  -In stash_usr_regs( ) and restore_usr_regs( ), save/restore of user regs
0024  *   in done in block copy rather than one word at a time.
0025  *   This saves around 2K of code and improves LMBench lat_sig <catch>
0026  *
0027  * rajeshwarr: Feb 2009
0028  *  - Support for Realtime Signals
0029  *
0030  * vineetg: Aug 11th 2008: Bug #94183
0031  *  -ViXS were still seeing crashes when using insmod to load drivers.
0032  *   It turned out that the code to change Execute permssions for TLB entries
0033  *   of user was not guarded for interrupts (mod_tlb_permission)
0034  *   This was causing TLB entries to be overwritten on unrelated indexes
0035  *
0036  * Vineetg: July 15th 2008: Bug #94183
0037  *  -Exception happens in Delay slot of a JMP, and before user space resumes,
0038  *   Signal is delivered (Ctrl + C) = >SIGINT.
0039  *   setup_frame( ) sets up PC,SP,BLINK to enable user space signal handler
0040  *   to run, but doesn't clear the Delay slot bit from status32. As a result,
0041  *   on resuming user mode, signal handler branches off to BTA of orig JMP
0042  *  -FIX: clear the DE bit from status32 in setup_frame( )
0043  *
0044  * Rahul Trivedi, Kanika Nema: Codito Technologies 2004
0045  */
0046 
0047 #include <linux/signal.h>
0048 #include <linux/ptrace.h>
0049 #include <linux/personality.h>
0050 #include <linux/uaccess.h>
0051 #include <linux/syscalls.h>
0052 #include <linux/resume_user_mode.h>
0053 #include <linux/sched/task_stack.h>
0054 
0055 #include <asm/ucontext.h>
0056 
0057 struct rt_sigframe {
0058     struct siginfo info;
0059     struct ucontext uc;
0060 #define MAGIC_SIGALTSTK     0x07302004
0061     unsigned int sigret_magic;
0062 };
0063 
0064 static int save_arcv2_regs(struct sigcontext *mctx, struct pt_regs *regs)
0065 {
0066     int err = 0;
0067 #ifndef CONFIG_ISA_ARCOMPACT
0068     struct user_regs_arcv2 v2abi;
0069 
0070     v2abi.r30 = regs->r30;
0071 #ifdef CONFIG_ARC_HAS_ACCL_REGS
0072     v2abi.r58 = regs->r58;
0073     v2abi.r59 = regs->r59;
0074 #else
0075     v2abi.r58 = v2abi.r59 = 0;
0076 #endif
0077     err = __copy_to_user(&mctx->v2abi, &v2abi, sizeof(v2abi));
0078 #endif
0079     return err;
0080 }
0081 
0082 static int restore_arcv2_regs(struct sigcontext *mctx, struct pt_regs *regs)
0083 {
0084     int err = 0;
0085 #ifndef CONFIG_ISA_ARCOMPACT
0086     struct user_regs_arcv2 v2abi;
0087 
0088     err = __copy_from_user(&v2abi, &mctx->v2abi, sizeof(v2abi));
0089 
0090     regs->r30 = v2abi.r30;
0091 #ifdef CONFIG_ARC_HAS_ACCL_REGS
0092     regs->r58 = v2abi.r58;
0093     regs->r59 = v2abi.r59;
0094 #endif
0095 #endif
0096     return err;
0097 }
0098 
0099 static int
0100 stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs,
0101            sigset_t *set)
0102 {
0103     int err;
0104     struct user_regs_struct uregs;
0105 
0106     uregs.scratch.bta   = regs->bta;
0107     uregs.scratch.lp_start  = regs->lp_start;
0108     uregs.scratch.lp_end    = regs->lp_end;
0109     uregs.scratch.lp_count  = regs->lp_count;
0110     uregs.scratch.status32  = regs->status32;
0111     uregs.scratch.ret   = regs->ret;
0112     uregs.scratch.blink = regs->blink;
0113     uregs.scratch.fp    = regs->fp;
0114     uregs.scratch.gp    = regs->r26;
0115     uregs.scratch.r12   = regs->r12;
0116     uregs.scratch.r11   = regs->r11;
0117     uregs.scratch.r10   = regs->r10;
0118     uregs.scratch.r9    = regs->r9;
0119     uregs.scratch.r8    = regs->r8;
0120     uregs.scratch.r7    = regs->r7;
0121     uregs.scratch.r6    = regs->r6;
0122     uregs.scratch.r5    = regs->r5;
0123     uregs.scratch.r4    = regs->r4;
0124     uregs.scratch.r3    = regs->r3;
0125     uregs.scratch.r2    = regs->r2;
0126     uregs.scratch.r1    = regs->r1;
0127     uregs.scratch.r0    = regs->r0;
0128     uregs.scratch.sp    = regs->sp;
0129 
0130     err = __copy_to_user(&(sf->uc.uc_mcontext.regs.scratch), &uregs.scratch,
0131                  sizeof(sf->uc.uc_mcontext.regs.scratch));
0132 
0133     if (is_isa_arcv2())
0134         err |= save_arcv2_regs(&(sf->uc.uc_mcontext), regs);
0135 
0136     err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t));
0137 
0138     return err ? -EFAULT : 0;
0139 }
0140 
0141 static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf)
0142 {
0143     sigset_t set;
0144     int err;
0145     struct user_regs_struct uregs;
0146 
0147     err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
0148     err |= __copy_from_user(&uregs.scratch,
0149                 &(sf->uc.uc_mcontext.regs.scratch),
0150                 sizeof(sf->uc.uc_mcontext.regs.scratch));
0151 
0152     if (is_isa_arcv2())
0153         err |= restore_arcv2_regs(&(sf->uc.uc_mcontext), regs);
0154 
0155     if (err)
0156         return -EFAULT;
0157 
0158     set_current_blocked(&set);
0159     regs->bta   = uregs.scratch.bta;
0160     regs->lp_start  = uregs.scratch.lp_start;
0161     regs->lp_end    = uregs.scratch.lp_end;
0162     regs->lp_count  = uregs.scratch.lp_count;
0163     regs->status32  = uregs.scratch.status32;
0164     regs->ret   = uregs.scratch.ret;
0165     regs->blink = uregs.scratch.blink;
0166     regs->fp    = uregs.scratch.fp;
0167     regs->r26   = uregs.scratch.gp;
0168     regs->r12   = uregs.scratch.r12;
0169     regs->r11   = uregs.scratch.r11;
0170     regs->r10   = uregs.scratch.r10;
0171     regs->r9    = uregs.scratch.r9;
0172     regs->r8    = uregs.scratch.r8;
0173     regs->r7    = uregs.scratch.r7;
0174     regs->r6    = uregs.scratch.r6;
0175     regs->r5    = uregs.scratch.r5;
0176     regs->r4    = uregs.scratch.r4;
0177     regs->r3    = uregs.scratch.r3;
0178     regs->r2    = uregs.scratch.r2;
0179     regs->r1    = uregs.scratch.r1;
0180     regs->r0    = uregs.scratch.r0;
0181     regs->sp    = uregs.scratch.sp;
0182 
0183     return 0;
0184 }
0185 
0186 static inline int is_do_ss_needed(unsigned int magic)
0187 {
0188     if (MAGIC_SIGALTSTK == magic)
0189         return 1;
0190     else
0191         return 0;
0192 }
0193 
0194 SYSCALL_DEFINE0(rt_sigreturn)
0195 {
0196     struct rt_sigframe __user *sf;
0197     unsigned int magic;
0198     struct pt_regs *regs = current_pt_regs();
0199 
0200     /* Always make any pending restarted system calls return -EINTR */
0201     current->restart_block.fn = do_no_restart_syscall;
0202 
0203     /* Since we stacked the signal on a word boundary,
0204      * then 'sp' should be word aligned here.  If it's
0205      * not, then the user is trying to mess with us.
0206      */
0207     if (regs->sp & 3)
0208         goto badframe;
0209 
0210     sf = (struct rt_sigframe __force __user *)(regs->sp);
0211 
0212     if (!access_ok(sf, sizeof(*sf)))
0213         goto badframe;
0214 
0215     if (__get_user(magic, &sf->sigret_magic))
0216         goto badframe;
0217 
0218     if (unlikely(is_do_ss_needed(magic)))
0219         if (restore_altstack(&sf->uc.uc_stack))
0220             goto badframe;
0221 
0222     if (restore_usr_regs(regs, sf))
0223         goto badframe;
0224 
0225     /* Don't restart from sigreturn */
0226     syscall_wont_restart(regs);
0227 
0228     /*
0229      * Ensure that sigreturn always returns to user mode (in case the
0230      * regs saved on user stack got fudged between save and sigreturn)
0231      * Otherwise it is easy to panic the kernel with a custom
0232      * signal handler and/or restorer which clobberes the status32/ret
0233      * to return to a bogus location in kernel mode.
0234      */
0235     regs->status32 |= STATUS_U_MASK;
0236 
0237     return regs->r0;
0238 
0239 badframe:
0240     force_sig(SIGSEGV);
0241     return 0;
0242 }
0243 
0244 /*
0245  * Determine which stack to use..
0246  */
0247 static inline void __user *get_sigframe(struct ksignal *ksig,
0248                     struct pt_regs *regs,
0249                     unsigned long framesize)
0250 {
0251     unsigned long sp = sigsp(regs->sp, ksig);
0252     void __user *frame;
0253 
0254     /* No matter what happens, 'sp' must be word
0255      * aligned otherwise nasty things could happen
0256      */
0257 
0258     /* ATPCS B01 mandates 8-byte alignment */
0259     frame = (void __user *)((sp - framesize) & ~7);
0260 
0261     /* Check that we can actually write to the signal frame */
0262     if (!access_ok(frame, framesize))
0263         frame = NULL;
0264 
0265     return frame;
0266 }
0267 
0268 static int
0269 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
0270 {
0271     struct rt_sigframe __user *sf;
0272     unsigned int magic = 0;
0273     int err = 0;
0274 
0275     sf = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
0276     if (!sf)
0277         return 1;
0278 
0279     /*
0280      * w/o SA_SIGINFO, struct ucontext is partially populated (only
0281      * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
0282      * during signal handler execution. This works for SA_SIGINFO as well
0283      * although the semantics are now overloaded (the same reg state can be
0284      * inspected by userland: but are they allowed to fiddle with it ?
0285      */
0286     err |= stash_usr_regs(sf, regs, set);
0287 
0288     /*
0289      * SA_SIGINFO requires 3 args to signal handler:
0290      *  #1: sig-no (common to any handler)
0291      *  #2: struct siginfo
0292      *  #3: struct ucontext (completely populated)
0293      */
0294     if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) {
0295         err |= copy_siginfo_to_user(&sf->info, &ksig->info);
0296         err |= __put_user(0, &sf->uc.uc_flags);
0297         err |= __put_user(NULL, &sf->uc.uc_link);
0298         err |= __save_altstack(&sf->uc.uc_stack, regs->sp);
0299 
0300         /* setup args 2 and 3 for user mode handler */
0301         regs->r1 = (unsigned long)&sf->info;
0302         regs->r2 = (unsigned long)&sf->uc;
0303 
0304         /*
0305          * small optim to avoid unconditionally calling do_sigaltstack
0306          * in sigreturn path, now that we only have rt_sigreturn
0307          */
0308         magic = MAGIC_SIGALTSTK;
0309     }
0310 
0311     err |= __put_user(magic, &sf->sigret_magic);
0312     if (err)
0313         return err;
0314 
0315     /* #1 arg to the user Signal handler */
0316     regs->r0 = ksig->sig;
0317 
0318     /* setup PC of user space signal handler */
0319     regs->ret = (unsigned long)ksig->ka.sa.sa_handler;
0320 
0321     /*
0322      * handler returns using sigreturn stub provided already by userspace
0323      * If not, nuke the process right away
0324      */
0325     if(!(ksig->ka.sa.sa_flags & SA_RESTORER))
0326         return 1;
0327 
0328     regs->blink = (unsigned long)ksig->ka.sa.sa_restorer;
0329 
0330     /* User Stack for signal handler will be above the frame just carved */
0331     regs->sp = (unsigned long)sf;
0332 
0333     /*
0334      * Bug 94183, Clear the DE bit, so that when signal handler
0335      * starts to run, it doesn't use BTA
0336      */
0337     regs->status32 &= ~STATUS_DE_MASK;
0338     regs->status32 |= STATUS_L_MASK;
0339 
0340     return err;
0341 }
0342 
0343 static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs)
0344 {
0345     switch (regs->r0) {
0346     case -ERESTART_RESTARTBLOCK:
0347     case -ERESTARTNOHAND:
0348         /*
0349          * ERESTARTNOHAND means that the syscall should
0350          * only be restarted if there was no handler for
0351          * the signal, and since we only get here if there
0352          * is a handler, we don't restart
0353          */
0354         regs->r0 = -EINTR;   /* ERESTART_xxx is internal */
0355         break;
0356 
0357     case -ERESTARTSYS:
0358         /*
0359          * ERESTARTSYS means to restart the syscall if
0360          * there is no handler or the handler was
0361          * registered with SA_RESTART
0362          */
0363         if (!(ka->sa.sa_flags & SA_RESTART)) {
0364             regs->r0 = -EINTR;
0365             break;
0366         }
0367         fallthrough;
0368 
0369     case -ERESTARTNOINTR:
0370         /*
0371          * ERESTARTNOINTR means that the syscall should
0372          * be called again after the signal handler returns.
0373          * Setup reg state just as it was before doing the trap
0374          * r0 has been clobbered with sys call ret code thus it
0375          * needs to be reloaded with orig first arg to syscall
0376          * in orig_r0. Rest of relevant reg-file:
0377          * r8 (syscall num) and (r1 - r7) will be reset to
0378          * their orig user space value when we ret from kernel
0379          */
0380         regs->r0 = regs->orig_r0;
0381         regs->ret -= is_isa_arcv2() ? 2 : 4;
0382         break;
0383     }
0384 }
0385 
0386 /*
0387  * OK, we're invoking a handler
0388  */
0389 static void
0390 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
0391 {
0392     sigset_t *oldset = sigmask_to_save();
0393     int failed;
0394 
0395     /* Set up the stack frame */
0396     failed = setup_rt_frame(ksig, oldset, regs);
0397 
0398     signal_setup_done(failed, ksig, 0);
0399 }
0400 
0401 void do_signal(struct pt_regs *regs)
0402 {
0403     struct ksignal ksig;
0404     int restart_scall;
0405 
0406     restart_scall = in_syscall(regs) && syscall_restartable(regs);
0407 
0408     if (test_thread_flag(TIF_SIGPENDING) && get_signal(&ksig)) {
0409         if (restart_scall) {
0410             arc_restart_syscall(&ksig.ka, regs);
0411             syscall_wont_restart(regs); /* No more restarts */
0412         }
0413         handle_signal(&ksig, regs);
0414         return;
0415     }
0416 
0417     if (restart_scall) {
0418         /* No handler for syscall: restart it */
0419         if (regs->r0 == -ERESTARTNOHAND ||
0420             regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) {
0421             regs->r0 = regs->orig_r0;
0422             regs->ret -= is_isa_arcv2() ? 2 : 4;
0423         } else if (regs->r0 == -ERESTART_RESTARTBLOCK) {
0424             regs->r8 = __NR_restart_syscall;
0425             regs->ret -= is_isa_arcv2() ? 2 : 4;
0426         }
0427         syscall_wont_restart(regs); /* No more restarts */
0428     }
0429 
0430     /* If there's no signal to deliver, restore the saved sigmask back */
0431     restore_saved_sigmask();
0432 }
0433 
0434 void do_notify_resume(struct pt_regs *regs)
0435 {
0436     /*
0437      * ASM glue guarantees that this is only called when returning to
0438      * user mode
0439      */
0440     if (test_thread_flag(TIF_NOTIFY_RESUME))
0441         resume_user_mode_work(regs);
0442 }