linux/arch/tile/kernel/signal.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 1991, 1992  Linus Torvalds
   3 * Copyright 2010 Tilera Corporation. All Rights Reserved.
   4 *
   5 *   This program is free software; you can redistribute it and/or
   6 *   modify it under the terms of the GNU General Public License
   7 *   as published by the Free Software Foundation, version 2.
   8 *
   9 *   This program is distributed in the hope that it will be useful, but
  10 *   WITHOUT ANY WARRANTY; without even the implied warranty of
  11 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12 *   NON INFRINGEMENT.  See the GNU General Public License for
  13 *   more details.
  14 */
  15
  16#include <linux/sched.h>
  17#include <linux/mm.h>
  18#include <linux/smp.h>
  19#include <linux/kernel.h>
  20#include <linux/signal.h>
  21#include <linux/errno.h>
  22#include <linux/wait.h>
  23#include <linux/unistd.h>
  24#include <linux/stddef.h>
  25#include <linux/personality.h>
  26#include <linux/suspend.h>
  27#include <linux/ptrace.h>
  28#include <linux/elf.h>
  29#include <linux/compat.h>
  30#include <linux/syscalls.h>
  31#include <linux/uaccess.h>
  32#include <asm/processor.h>
  33#include <asm/ucontext.h>
  34#include <asm/sigframe.h>
  35#include <asm/syscalls.h>
  36#include <asm/vdso.h>
  37#include <arch/interrupts.h>
  38
  39#define DEBUG_SIG 0
  40
  41/*
  42 * Do a signal return; undo the signal stack.
  43 */
  44
  45int restore_sigcontext(struct pt_regs *regs,
  46                       struct sigcontext __user *sc)
  47{
  48        int err;
  49
  50        /* Always make any pending restarted system calls return -EINTR */
  51        current_thread_info()->restart_block.fn = do_no_restart_syscall;
  52
  53        /*
  54         * Enforce that sigcontext is like pt_regs, and doesn't mess
  55         * up our stack alignment rules.
  56         */
  57        BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
  58        BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
  59        err = __copy_from_user(regs, sc, sizeof(*regs));
  60
  61        /* Ensure that the PL is always set to USER_PL. */
  62        regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
  63
  64        regs->faultnum = INT_SWINT_1_SIGRETURN;
  65
  66        return err;
  67}
  68
  69void signal_fault(const char *type, struct pt_regs *regs,
  70                  void __user *frame, int sig)
  71{
  72        trace_unhandled_signal(type, regs, (unsigned long)frame, SIGSEGV);
  73        force_sigsegv(sig, current);
  74}
  75
  76/* The assembly shim for this function arranges to ignore the return value. */
  77SYSCALL_DEFINE0(rt_sigreturn)
  78{
  79        struct pt_regs *regs = current_pt_regs();
  80        struct rt_sigframe __user *frame =
  81                (struct rt_sigframe __user *)(regs->sp);
  82        sigset_t set;
  83
  84        if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  85                goto badframe;
  86        if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  87                goto badframe;
  88
  89        set_current_blocked(&set);
  90
  91        if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  92                goto badframe;
  93
  94        if (restore_altstack(&frame->uc.uc_stack))
  95                goto badframe;
  96
  97        return 0;
  98
  99badframe:
 100        signal_fault("bad sigreturn frame", regs, frame, 0);
 101        return 0;
 102}
 103
 104/*
 105 * Set up a signal frame.
 106 */
 107
 108int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
 109{
 110        return  __copy_to_user(sc, regs, sizeof(*regs));
 111}
 112
 113/*
 114 * Determine which stack to use..
 115 */
 116static inline void __user *get_sigframe(struct k_sigaction *ka,
 117                                        struct pt_regs *regs,
 118                                        size_t frame_size)
 119{
 120        unsigned long sp;
 121
 122        /* Default to using normal stack */
 123        sp = regs->sp;
 124
 125        /*
 126         * If we are on the alternate signal stack and would overflow
 127         * it, don't.  Return an always-bogus address instead so we
 128         * will die with SIGSEGV.
 129         */
 130        if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
 131                return (void __user __force *)-1UL;
 132
 133        /* This is the X/Open sanctioned signal stack switching.  */
 134        if (ka->sa.sa_flags & SA_ONSTACK) {
 135                if (sas_ss_flags(sp) == 0)
 136                        sp = current->sas_ss_sp + current->sas_ss_size;
 137        }
 138
 139        sp -= frame_size;
 140        /*
 141         * Align the stack pointer according to the TILE ABI,
 142         * i.e. so that on function entry (sp & 15) == 0.
 143         */
 144        sp &= -16UL;
 145        return (void __user *) sp;
 146}
 147
 148static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
 149                          struct pt_regs *regs)
 150{
 151        unsigned long restorer;
 152        struct rt_sigframe __user *frame;
 153        int err = 0, sig = ksig->sig;
 154        int usig;
 155
 156        frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
 157
 158        if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
 159                goto err;
 160
 161        usig = current_thread_info()->exec_domain
 162                && current_thread_info()->exec_domain->signal_invmap
 163                && sig < 32
 164                ? current_thread_info()->exec_domain->signal_invmap[sig]
 165                : sig;
 166
 167        /* Always write at least the signal number for the stack backtracer. */
 168        if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
 169                /* At sigreturn time, restore the callee-save registers too. */
 170                err |= copy_siginfo_to_user(&frame->info, &ksig->info);
 171                regs->flags |= PT_FLAGS_RESTORE_REGS;
 172        } else {
 173                err |= __put_user(ksig->info.si_signo, &frame->info.si_signo);
 174        }
 175
 176        /* Create the ucontext.  */
 177        err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
 178        err |= __put_user(0, &frame->uc.uc_flags);
 179        err |= __put_user(NULL, &frame->uc.uc_link);
 180        err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
 181        err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
 182        err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
 183        if (err)
 184                goto err;
 185
 186        restorer = VDSO_SYM(&__vdso_rt_sigreturn);
 187        if (ksig->ka.sa.sa_flags & SA_RESTORER)
 188                restorer = (unsigned long) ksig->ka.sa.sa_restorer;
 189
 190        /*
 191         * Set up registers for signal handler.
 192         * Registers that we don't modify keep the value they had from
 193         * user-space at the time we took the signal.
 194         * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
 195         * since some things rely on this (e.g. glibc's debug/segfault.c).
 196         */
 197        regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
 198        regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
 199        regs->sp = (unsigned long) frame;
 200        regs->lr = restorer;
 201        regs->regs[0] = (unsigned long) usig;
 202        regs->regs[1] = (unsigned long) &frame->info;
 203        regs->regs[2] = (unsigned long) &frame->uc;
 204        regs->flags |= PT_FLAGS_CALLER_SAVES;
 205        return 0;
 206
 207err:
 208        trace_unhandled_signal("bad sigreturn frame", regs,
 209                              (unsigned long)frame, SIGSEGV);
 210        return -EFAULT;
 211}
 212
 213/*
 214 * OK, we're invoking a handler
 215 */
 216
 217static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 218{
 219        sigset_t *oldset = sigmask_to_save();
 220        int ret;
 221
 222        /* Are we from a system call? */
 223        if (regs->faultnum == INT_SWINT_1) {
 224                /* If so, check system call restarting.. */
 225                switch (regs->regs[0]) {
 226                case -ERESTART_RESTARTBLOCK:
 227                case -ERESTARTNOHAND:
 228                        regs->regs[0] = -EINTR;
 229                        break;
 230
 231                case -ERESTARTSYS:
 232                        if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
 233                                regs->regs[0] = -EINTR;
 234                                break;
 235                        }
 236                        /* fallthrough */
 237                case -ERESTARTNOINTR:
 238                        /* Reload caller-saves to restore r0..r5 and r10. */
 239                        regs->flags |= PT_FLAGS_CALLER_SAVES;
 240                        regs->regs[0] = regs->orig_r0;
 241                        regs->pc -= 8;
 242                }
 243        }
 244
 245        /* Set up the stack frame */
 246#ifdef CONFIG_COMPAT
 247        if (is_compat_task())
 248                ret = compat_setup_rt_frame(ksig, oldset, regs);
 249        else
 250#endif
 251                ret = setup_rt_frame(ksig, oldset, regs);
 252
 253        signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
 254}
 255
 256/*
 257 * Note that 'init' is a special process: it doesn't get signals it doesn't
 258 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 259 * mistake.
 260 */
 261void do_signal(struct pt_regs *regs)
 262{
 263        struct ksignal ksig;
 264
 265        /*
 266         * i386 will check if we're coming from kernel mode and bail out
 267         * here.  In my experience this just turns weird crashes into
 268         * weird spin-hangs.  But if we find a case where this seems
 269         * helpful, we can reinstate the check on "!user_mode(regs)".
 270         */
 271
 272        if (get_signal(&ksig)) {
 273                /* Whee! Actually deliver the signal.  */
 274                handle_signal(&ksig, regs);
 275                goto done;
 276        }
 277
 278        /* Did we come from a system call? */
 279        if (regs->faultnum == INT_SWINT_1) {
 280                /* Restart the system call - no handlers present */
 281                switch (regs->regs[0]) {
 282                case -ERESTARTNOHAND:
 283                case -ERESTARTSYS:
 284                case -ERESTARTNOINTR:
 285                        regs->flags |= PT_FLAGS_CALLER_SAVES;
 286                        regs->regs[0] = regs->orig_r0;
 287                        regs->pc -= 8;
 288                        break;
 289
 290                case -ERESTART_RESTARTBLOCK:
 291                        regs->flags |= PT_FLAGS_CALLER_SAVES;
 292                        regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
 293                        regs->pc -= 8;
 294                        break;
 295                }
 296        }
 297
 298        /* If there's no signal to deliver, just put the saved sigmask back. */
 299        restore_saved_sigmask();
 300
 301done:
 302        /* Avoid double syscall restart if there are nested signals. */
 303        regs->faultnum = INT_SWINT_1_SIGRETURN;
 304}
 305
 306int show_unhandled_signals = 1;
 307
 308static int __init crashinfo(char *str)
 309{
 310        const char *word;
 311
 312        if (*str == '\0')
 313                show_unhandled_signals = 2;
 314        else if (*str != '=' || kstrtoint(++str, 0, &show_unhandled_signals) != 0)
 315                return 0;
 316
 317        switch (show_unhandled_signals) {
 318        case 0:
 319                word = "No";
 320                break;
 321        case 1:
 322                word = "One-line";
 323                break;
 324        default:
 325                word = "Detailed";
 326                break;
 327        }
 328        pr_info("%s crash reports will be generated on the console\n", word);
 329        return 1;
 330}
 331__setup("crashinfo", crashinfo);
 332
 333static void dump_mem(void __user *address)
 334{
 335        void __user *addr;
 336        enum { region_size = 256, bytes_per_line = 16 };
 337        int i, j, k;
 338        int found_readable_mem = 0;
 339
 340        if (!access_ok(VERIFY_READ, address, 1)) {
 341                pr_err("Not dumping at address 0x%lx (kernel address)\n",
 342                       (unsigned long)address);
 343                return;
 344        }
 345
 346        addr = (void __user *)
 347                (((unsigned long)address & -bytes_per_line) - region_size/2);
 348        if (addr > address)
 349                addr = NULL;
 350        for (i = 0; i < region_size;
 351             addr += bytes_per_line, i += bytes_per_line) {
 352                unsigned char buf[bytes_per_line];
 353                char line[100];
 354                if (copy_from_user(buf, addr, bytes_per_line))
 355                        continue;
 356                if (!found_readable_mem) {
 357                        pr_err("Dumping memory around address 0x%lx:\n",
 358                               (unsigned long)address);
 359                        found_readable_mem = 1;
 360                }
 361                j = sprintf(line, REGFMT ":", (unsigned long)addr);
 362                for (k = 0; k < bytes_per_line; ++k)
 363                        j += sprintf(&line[j], " %02x", buf[k]);
 364                pr_err("%s\n", line);
 365        }
 366        if (!found_readable_mem)
 367                pr_err("No readable memory around address 0x%lx\n",
 368                       (unsigned long)address);
 369}
 370
 371void trace_unhandled_signal(const char *type, struct pt_regs *regs,
 372                            unsigned long address, int sig)
 373{
 374        struct task_struct *tsk = current;
 375
 376        if (show_unhandled_signals == 0)
 377                return;
 378
 379        /* If the signal is handled, don't show it here. */
 380        if (!is_global_init(tsk)) {
 381                void __user *handler =
 382                        tsk->sighand->action[sig-1].sa.sa_handler;
 383                if (handler != SIG_IGN && handler != SIG_DFL)
 384                        return;
 385        }
 386
 387        /* Rate-limit the one-line output, not the detailed output. */
 388        if (show_unhandled_signals <= 1 && !printk_ratelimit())
 389                return;
 390
 391        printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d",
 392               task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
 393               tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig);
 394
 395        print_vma_addr(KERN_CONT " in ", regs->pc);
 396
 397        printk(KERN_CONT "\n");
 398
 399        if (show_unhandled_signals > 1) {
 400                switch (sig) {
 401                case SIGILL:
 402                case SIGFPE:
 403                case SIGSEGV:
 404                case SIGBUS:
 405                        pr_err("User crash: signal %d, trap %ld, address 0x%lx\n",
 406                               sig, regs->faultnum, address);
 407                        show_regs(regs);
 408                        dump_mem((void __user *)address);
 409                        break;
 410                default:
 411                        pr_err("User crash: signal %d, trap %ld\n",
 412                               sig, regs->faultnum);
 413                        break;
 414                }
 415        }
 416}
 417