Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Common signal handling code for both 32 and 64 bits
0003  *
0004  *    Copyright (c) 2007 Benjamin Herrenschmidt, IBM Corporation
0005  *    Extracted from signal_32.c and signal_64.c
0006  *
0007  * This file is subject to the terms and conditions of the GNU General
0008  * Public License.  See the file README.legal in the main directory of
0009  * this archive for more details.
0010  */
0011 
0012 #include <linux/resume_user_mode.h>
0013 #include <linux/signal.h>
0014 #include <linux/uprobes.h>
0015 #include <linux/key.h>
0016 #include <linux/context_tracking.h>
0017 #include <linux/livepatch.h>
0018 #include <linux/syscalls.h>
0019 #include <asm/hw_breakpoint.h>
0020 #include <linux/uaccess.h>
0021 #include <asm/switch_to.h>
0022 #include <asm/unistd.h>
0023 #include <asm/debug.h>
0024 #include <asm/tm.h>
0025 
0026 #include "signal.h"
0027 
0028 #ifdef CONFIG_VSX
0029 unsigned long copy_fpr_to_user(void __user *to,
0030                    struct task_struct *task)
0031 {
0032     u64 buf[ELF_NFPREG];
0033     int i;
0034 
0035     /* save FPR copy to local buffer then write to the thread_struct */
0036     for (i = 0; i < (ELF_NFPREG - 1) ; i++)
0037         buf[i] = task->thread.TS_FPR(i);
0038     buf[i] = task->thread.fp_state.fpscr;
0039     return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
0040 }
0041 
0042 unsigned long copy_fpr_from_user(struct task_struct *task,
0043                  void __user *from)
0044 {
0045     u64 buf[ELF_NFPREG];
0046     int i;
0047 
0048     if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
0049         return 1;
0050     for (i = 0; i < (ELF_NFPREG - 1) ; i++)
0051         task->thread.TS_FPR(i) = buf[i];
0052     task->thread.fp_state.fpscr = buf[i];
0053 
0054     return 0;
0055 }
0056 
0057 unsigned long copy_vsx_to_user(void __user *to,
0058                    struct task_struct *task)
0059 {
0060     u64 buf[ELF_NVSRHALFREG];
0061     int i;
0062 
0063     /* save FPR copy to local buffer then write to the thread_struct */
0064     for (i = 0; i < ELF_NVSRHALFREG; i++)
0065         buf[i] = task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET];
0066     return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
0067 }
0068 
0069 unsigned long copy_vsx_from_user(struct task_struct *task,
0070                  void __user *from)
0071 {
0072     u64 buf[ELF_NVSRHALFREG];
0073     int i;
0074 
0075     if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
0076         return 1;
0077     for (i = 0; i < ELF_NVSRHALFREG ; i++)
0078         task->thread.fp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
0079     return 0;
0080 }
0081 
0082 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
0083 unsigned long copy_ckfpr_to_user(void __user *to,
0084                   struct task_struct *task)
0085 {
0086     u64 buf[ELF_NFPREG];
0087     int i;
0088 
0089     /* save FPR copy to local buffer then write to the thread_struct */
0090     for (i = 0; i < (ELF_NFPREG - 1) ; i++)
0091         buf[i] = task->thread.TS_CKFPR(i);
0092     buf[i] = task->thread.ckfp_state.fpscr;
0093     return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
0094 }
0095 
0096 unsigned long copy_ckfpr_from_user(struct task_struct *task,
0097                       void __user *from)
0098 {
0099     u64 buf[ELF_NFPREG];
0100     int i;
0101 
0102     if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
0103         return 1;
0104     for (i = 0; i < (ELF_NFPREG - 1) ; i++)
0105         task->thread.TS_CKFPR(i) = buf[i];
0106     task->thread.ckfp_state.fpscr = buf[i];
0107 
0108     return 0;
0109 }
0110 
0111 unsigned long copy_ckvsx_to_user(void __user *to,
0112                   struct task_struct *task)
0113 {
0114     u64 buf[ELF_NVSRHALFREG];
0115     int i;
0116 
0117     /* save FPR copy to local buffer then write to the thread_struct */
0118     for (i = 0; i < ELF_NVSRHALFREG; i++)
0119         buf[i] = task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET];
0120     return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
0121 }
0122 
0123 unsigned long copy_ckvsx_from_user(struct task_struct *task,
0124                       void __user *from)
0125 {
0126     u64 buf[ELF_NVSRHALFREG];
0127     int i;
0128 
0129     if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
0130         return 1;
0131     for (i = 0; i < ELF_NVSRHALFREG ; i++)
0132         task->thread.ckfp_state.fpr[i][TS_VSRLOWOFFSET] = buf[i];
0133     return 0;
0134 }
0135 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
0136 #endif
0137 
0138 /* Log an error when sending an unhandled signal to a process. Controlled
0139  * through debug.exception-trace sysctl.
0140  */
0141 
0142 int show_unhandled_signals = 1;
0143 
0144 unsigned long get_min_sigframe_size(void)
0145 {
0146     if (IS_ENABLED(CONFIG_PPC64))
0147         return get_min_sigframe_size_64();
0148     else
0149         return get_min_sigframe_size_32();
0150 }
0151 
0152 #ifdef CONFIG_COMPAT
0153 unsigned long get_min_sigframe_size_compat(void)
0154 {
0155     return get_min_sigframe_size_32();
0156 }
0157 #endif
0158 
0159 /*
0160  * Allocate space for the signal frame
0161  */
0162 static unsigned long get_tm_stackpointer(struct task_struct *tsk);
0163 
0164 void __user *get_sigframe(struct ksignal *ksig, struct task_struct *tsk,
0165               size_t frame_size, int is_32)
0166 {
0167         unsigned long oldsp, newsp;
0168     unsigned long sp = get_tm_stackpointer(tsk);
0169 
0170         /* Default to using normal stack */
0171     if (is_32)
0172         oldsp = sp & 0x0ffffffffUL;
0173     else
0174         oldsp = sp;
0175     oldsp = sigsp(oldsp, ksig);
0176     newsp = (oldsp - frame_size) & ~0xFUL;
0177 
0178         return (void __user *)newsp;
0179 }
0180 
0181 static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
0182                   int has_handler)
0183 {
0184     unsigned long ret = regs->gpr[3];
0185     int restart = 1;
0186 
0187     /* syscall ? */
0188     if (!trap_is_syscall(regs))
0189         return;
0190 
0191     if (trap_norestart(regs))
0192         return;
0193 
0194     /* error signalled ? */
0195     if (trap_is_scv(regs)) {
0196         /* 32-bit compat mode sign extend? */
0197         if (!IS_ERR_VALUE(ret))
0198             return;
0199         ret = -ret;
0200     } else if (!(regs->ccr & 0x10000000)) {
0201         return;
0202     }
0203 
0204     switch (ret) {
0205     case ERESTART_RESTARTBLOCK:
0206     case ERESTARTNOHAND:
0207         /* ERESTARTNOHAND means that the syscall should only be
0208          * restarted if there was no handler for the signal, and since
0209          * we only get here if there is a handler, we dont restart.
0210          */
0211         restart = !has_handler;
0212         break;
0213     case ERESTARTSYS:
0214         /* ERESTARTSYS means to restart the syscall if there is no
0215          * handler or the handler was registered with SA_RESTART
0216          */
0217         restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
0218         break;
0219     case ERESTARTNOINTR:
0220         /* ERESTARTNOINTR means that the syscall should be
0221          * called again after the signal handler returns.
0222          */
0223         break;
0224     default:
0225         return;
0226     }
0227     if (restart) {
0228         if (ret == ERESTART_RESTARTBLOCK)
0229             regs->gpr[0] = __NR_restart_syscall;
0230         else
0231             regs->gpr[3] = regs->orig_gpr3;
0232         regs_add_return_ip(regs, -4);
0233         regs->result = 0;
0234     } else {
0235         if (trap_is_scv(regs)) {
0236             regs->result = -EINTR;
0237             regs->gpr[3] = -EINTR;
0238         } else {
0239             regs->result = -EINTR;
0240             regs->gpr[3] = EINTR;
0241             regs->ccr |= 0x10000000;
0242         }
0243     }
0244 }
0245 
0246 static void do_signal(struct task_struct *tsk)
0247 {
0248     sigset_t *oldset = sigmask_to_save();
0249     struct ksignal ksig = { .sig = 0 };
0250     int ret;
0251 
0252     BUG_ON(tsk != current);
0253 
0254     get_signal(&ksig);
0255 
0256     /* Is there any syscall restart business here ? */
0257     check_syscall_restart(tsk->thread.regs, &ksig.ka, ksig.sig > 0);
0258 
0259     if (ksig.sig <= 0) {
0260         /* No signal to deliver -- put the saved sigmask back */
0261         restore_saved_sigmask();
0262         set_trap_norestart(tsk->thread.regs);
0263         return;               /* no signals delivered */
0264     }
0265 
0266         /*
0267      * Reenable the DABR before delivering the signal to
0268      * user space. The DABR will have been cleared if it
0269      * triggered inside the kernel.
0270      */
0271     if (!IS_ENABLED(CONFIG_PPC_ADV_DEBUG_REGS)) {
0272         int i;
0273 
0274         for (i = 0; i < nr_wp_slots(); i++) {
0275             if (tsk->thread.hw_brk[i].address && tsk->thread.hw_brk[i].type)
0276                 __set_breakpoint(i, &tsk->thread.hw_brk[i]);
0277         }
0278     }
0279 
0280     /* Re-enable the breakpoints for the signal stack */
0281     thread_change_pc(tsk, tsk->thread.regs);
0282 
0283     rseq_signal_deliver(&ksig, tsk->thread.regs);
0284 
0285     if (is_32bit_task()) {
0286             if (ksig.ka.sa.sa_flags & SA_SIGINFO)
0287             ret = handle_rt_signal32(&ksig, oldset, tsk);
0288         else
0289             ret = handle_signal32(&ksig, oldset, tsk);
0290     } else {
0291         ret = handle_rt_signal64(&ksig, oldset, tsk);
0292     }
0293 
0294     set_trap_norestart(tsk->thread.regs);
0295     signal_setup_done(ret, &ksig, test_thread_flag(TIF_SINGLESTEP));
0296 }
0297 
0298 void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
0299 {
0300     if (thread_info_flags & _TIF_UPROBE)
0301         uprobe_notify_resume(regs);
0302 
0303     if (thread_info_flags & _TIF_PATCH_PENDING)
0304         klp_update_patch_state(current);
0305 
0306     if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
0307         BUG_ON(regs != current->thread.regs);
0308         do_signal(current);
0309     }
0310 
0311     if (thread_info_flags & _TIF_NOTIFY_RESUME)
0312         resume_user_mode_work(regs);
0313 }
0314 
0315 static unsigned long get_tm_stackpointer(struct task_struct *tsk)
0316 {
0317     /* When in an active transaction that takes a signal, we need to be
0318      * careful with the stack.  It's possible that the stack has moved back
0319      * up after the tbegin.  The obvious case here is when the tbegin is
0320      * called inside a function that returns before a tend.  In this case,
0321      * the stack is part of the checkpointed transactional memory state.
0322      * If we write over this non transactionally or in suspend, we are in
0323      * trouble because if we get a tm abort, the program counter and stack
0324      * pointer will be back at the tbegin but our in memory stack won't be
0325      * valid anymore.
0326      *
0327      * To avoid this, when taking a signal in an active transaction, we
0328      * need to use the stack pointer from the checkpointed state, rather
0329      * than the speculated state.  This ensures that the signal context
0330      * (written tm suspended) will be written below the stack required for
0331      * the rollback.  The transaction is aborted because of the treclaim,
0332      * so any memory written between the tbegin and the signal will be
0333      * rolled back anyway.
0334      *
0335      * For signals taken in non-TM or suspended mode, we use the
0336      * normal/non-checkpointed stack pointer.
0337      */
0338     struct pt_regs *regs = tsk->thread.regs;
0339     unsigned long ret = regs->gpr[1];
0340 
0341 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
0342     BUG_ON(tsk != current);
0343 
0344     if (MSR_TM_ACTIVE(regs->msr)) {
0345         preempt_disable();
0346         tm_reclaim_current(TM_CAUSE_SIGNAL);
0347         if (MSR_TM_TRANSACTIONAL(regs->msr))
0348             ret = tsk->thread.ckpt_regs.gpr[1];
0349 
0350         /*
0351          * If we treclaim, we must clear the current thread's TM bits
0352          * before re-enabling preemption. Otherwise we might be
0353          * preempted and have the live MSR[TS] changed behind our back
0354          * (tm_recheckpoint_new_task() would recheckpoint). Besides, we
0355          * enter the signal handler in non-transactional state.
0356          */
0357         regs_set_return_msr(regs, regs->msr & ~MSR_TS_MASK);
0358         preempt_enable();
0359     }
0360 #endif
0361     return ret;
0362 }
0363 
0364 static const char fm32[] = KERN_INFO "%s[%d]: bad frame in %s: %p nip %08lx lr %08lx\n";
0365 static const char fm64[] = KERN_INFO "%s[%d]: bad frame in %s: %p nip %016lx lr %016lx\n";
0366 
0367 void signal_fault(struct task_struct *tsk, struct pt_regs *regs,
0368           const char *where, void __user *ptr)
0369 {
0370     if (show_unhandled_signals)
0371         printk_ratelimited(regs->msr & MSR_64BIT ? fm64 : fm32, tsk->comm,
0372                    task_pid_nr(tsk), where, ptr, regs->nip, regs->link);
0373 }