linux/arch/x86/kernel/nmi.c
<<
>>
Prefs
   1/*
   2 *  Copyright (C) 1991, 1992  Linus Torvalds
   3 *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
   4 *  Copyright (C) 2011  Don Zickus Red Hat, Inc.
   5 *
   6 *  Pentium III FXSR, SSE support
   7 *      Gareth Hughes <gareth@valinux.com>, May 2000
   8 */
   9
  10/*
  11 * Handle hardware traps and faults.
  12 */
  13#include <linux/spinlock.h>
  14#include <linux/kprobes.h>
  15#include <linux/kdebug.h>
  16#include <linux/nmi.h>
  17#include <linux/debugfs.h>
  18#include <linux/delay.h>
  19#include <linux/hardirq.h>
  20#include <linux/slab.h>
  21#include <linux/export.h>
  22
  23#if defined(CONFIG_EDAC)
  24#include <linux/edac.h>
  25#endif
  26
  27#include <linux/atomic.h>
  28#include <asm/traps.h>
  29#include <asm/mach_traps.h>
  30#include <asm/nmi.h>
  31#include <asm/x86_init.h>
  32#include <asm/reboot.h>
  33
  34struct nmi_desc {
  35        raw_spinlock_t lock;
  36        struct list_head head;
  37};
  38
  39static struct nmi_desc nmi_desc[NMI_MAX] = 
  40{
  41        {
  42                .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
  43                .head = LIST_HEAD_INIT(nmi_desc[0].head),
  44        },
  45        {
  46                .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
  47                .head = LIST_HEAD_INIT(nmi_desc[1].head),
  48        },
  49        {
  50                .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[2].lock),
  51                .head = LIST_HEAD_INIT(nmi_desc[2].head),
  52        },
  53        {
  54                .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[3].lock),
  55                .head = LIST_HEAD_INIT(nmi_desc[3].head),
  56        },
  57
  58};
  59
  60struct nmi_stats {
  61        unsigned int normal;
  62        unsigned int unknown;
  63        unsigned int external;
  64        unsigned int swallow;
  65};
  66
  67static DEFINE_PER_CPU(struct nmi_stats, nmi_stats);
  68
  69static int ignore_nmis;
  70
  71int unknown_nmi_panic;
  72/*
  73 * Prevent NMI reason port (0x61) being accessed simultaneously, can
  74 * only be used in NMI handler.
  75 */
  76static DEFINE_RAW_SPINLOCK(nmi_reason_lock);
  77
  78static int __init setup_unknown_nmi_panic(char *str)
  79{
  80        unknown_nmi_panic = 1;
  81        return 1;
  82}
  83__setup("unknown_nmi_panic", setup_unknown_nmi_panic);
  84
  85#define nmi_to_desc(type) (&nmi_desc[type])
  86
  87static u64 nmi_longest_ns = 1 * NSEC_PER_MSEC;
  88static int __init nmi_warning_debugfs(void)
  89{
  90        debugfs_create_u64("nmi_longest_ns", 0644,
  91                        arch_debugfs_dir, &nmi_longest_ns);
  92        return 0;
  93}
  94fs_initcall(nmi_warning_debugfs);
  95
  96static int __kprobes nmi_handle(unsigned int type, struct pt_regs *regs, bool b2b)
  97{
  98        struct nmi_desc *desc = nmi_to_desc(type);
  99        struct nmiaction *a;
 100        int handled=0;
 101
 102        rcu_read_lock();
 103
 104        /*
 105         * NMIs are edge-triggered, which means if you have enough
 106         * of them concurrently, you can lose some because only one
 107         * can be latched at any given time.  Walk the whole list
 108         * to handle those situations.
 109         */
 110        list_for_each_entry_rcu(a, &desc->head, list) {
 111                u64 before, delta, whole_msecs;
 112                int remainder_ns, decimal_msecs;
 113
 114                before = sched_clock();
 115                handled += a->handler(type, regs);
 116                delta = sched_clock() - before;
 117
 118                if (delta < nmi_longest_ns)
 119                        continue;
 120
 121                nmi_longest_ns = delta;
 122                whole_msecs = delta;
 123                remainder_ns = do_div(whole_msecs, (1000 * 1000));
 124                decimal_msecs = remainder_ns / 1000;
 125                printk_ratelimited(KERN_INFO
 126                        "INFO: NMI handler (%ps) took too long to run: "
 127                        "%lld.%03d msecs\n", a->handler, whole_msecs,
 128                        decimal_msecs);
 129        }
 130
 131        rcu_read_unlock();
 132
 133        /* return total number of NMI events handled */
 134        return handled;
 135}
 136
 137int __register_nmi_handler(unsigned int type, struct nmiaction *action)
 138{
 139        struct nmi_desc *desc = nmi_to_desc(type);
 140        unsigned long flags;
 141
 142        if (!action->handler)
 143                return -EINVAL;
 144
 145        raw_spin_lock_irqsave(&desc->lock, flags);
 146
 147        /*
 148         * Indicate if there are multiple registrations on the
 149         * internal NMI handler call chains (SERR and IO_CHECK).
 150         */
 151        WARN_ON_ONCE(type == NMI_SERR && !list_empty(&desc->head));
 152        WARN_ON_ONCE(type == NMI_IO_CHECK && !list_empty(&desc->head));
 153
 154        /*
 155         * some handlers need to be executed first otherwise a fake
 156         * event confuses some handlers (kdump uses this flag)
 157         */
 158        if (action->flags & NMI_FLAG_FIRST)
 159                list_add_rcu(&action->list, &desc->head);
 160        else
 161                list_add_tail_rcu(&action->list, &desc->head);
 162        
 163        raw_spin_unlock_irqrestore(&desc->lock, flags);
 164        return 0;
 165}
 166EXPORT_SYMBOL(__register_nmi_handler);
 167
 168void unregister_nmi_handler(unsigned int type, const char *name)
 169{
 170        struct nmi_desc *desc = nmi_to_desc(type);
 171        struct nmiaction *n;
 172        unsigned long flags;
 173
 174        raw_spin_lock_irqsave(&desc->lock, flags);
 175
 176        list_for_each_entry_rcu(n, &desc->head, list) {
 177                /*
 178                 * the name passed in to describe the nmi handler
 179                 * is used as the lookup key
 180                 */
 181                if (!strcmp(n->name, name)) {
 182                        WARN(in_nmi(),
 183                                "Trying to free NMI (%s) from NMI context!\n", n->name);
 184                        list_del_rcu(&n->list);
 185                        break;
 186                }
 187        }
 188
 189        raw_spin_unlock_irqrestore(&desc->lock, flags);
 190        synchronize_rcu();
 191}
 192EXPORT_SYMBOL_GPL(unregister_nmi_handler);
 193
 194static __kprobes void
 195pci_serr_error(unsigned char reason, struct pt_regs *regs)
 196{
 197        /* check to see if anyone registered against these types of errors */
 198        if (nmi_handle(NMI_SERR, regs, false))
 199                return;
 200
 201        pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
 202                 reason, smp_processor_id());
 203
 204        /*
 205         * On some machines, PCI SERR line is used to report memory
 206         * errors. EDAC makes use of it.
 207         */
 208#if defined(CONFIG_EDAC)
 209        if (edac_handler_set()) {
 210                edac_atomic_assert_error();
 211                return;
 212        }
 213#endif
 214
 215        if (panic_on_unrecovered_nmi)
 216                nmi_panic(regs, "NMI: Not continuing");
 217
 218        pr_emerg("Dazed and confused, but trying to continue\n");
 219
 220        /* Clear and disable the PCI SERR error line. */
 221        reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_SERR;
 222        outb(reason, NMI_REASON_PORT);
 223}
 224
 225static __kprobes void
 226io_check_error(unsigned char reason, struct pt_regs *regs)
 227{
 228        unsigned long i;
 229
 230        /* check to see if anyone registered against these types of errors */
 231        if (nmi_handle(NMI_IO_CHECK, regs, false))
 232                return;
 233
 234        pr_emerg(
 235        "NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
 236                 reason, smp_processor_id());
 237        show_regs(regs);
 238
 239        if (panic_on_io_nmi) {
 240                nmi_panic(regs, "NMI IOCK error: Not continuing");
 241
 242                /*
 243                 * If we end up here, it means we have received an NMI while
 244                 * processing panic(). Simply return without delaying and
 245                 * re-enabling NMIs.
 246                 */
 247                return;
 248        }
 249
 250        /* Re-enable the IOCK line, wait for a few seconds */
 251        reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
 252        outb(reason, NMI_REASON_PORT);
 253
 254        i = 20000;
 255        while (--i) {
 256                touch_nmi_watchdog();
 257                udelay(100);
 258        }
 259
 260        reason &= ~NMI_REASON_CLEAR_IOCHK;
 261        outb(reason, NMI_REASON_PORT);
 262}
 263
 264static __kprobes void
 265unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
 266{
 267        int handled;
 268
 269        /*
 270         * Use 'false' as back-to-back NMIs are dealt with one level up.
 271         * Of course this makes having multiple 'unknown' handlers useless
 272         * as only the first one is ever run (unless it can actually determine
 273         * if it caused the NMI)
 274         */
 275        handled = nmi_handle(NMI_UNKNOWN, regs, false);
 276        if (handled) {
 277                __this_cpu_add(nmi_stats.unknown, handled);
 278                return;
 279        }
 280
 281        __this_cpu_add(nmi_stats.unknown, 1);
 282
 283        pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
 284                 reason, smp_processor_id());
 285
 286        pr_emerg("Do you have a strange power saving mode enabled?\n");
 287        if (unknown_nmi_panic || panic_on_unrecovered_nmi)
 288                nmi_panic(regs, "NMI: Not continuing");
 289
 290        pr_emerg("Dazed and confused, but trying to continue\n");
 291}
 292
 293static DEFINE_PER_CPU(bool, swallow_nmi);
 294static DEFINE_PER_CPU(unsigned long, last_nmi_rip);
 295
 296static __kprobes void default_do_nmi(struct pt_regs *regs)
 297{
 298        unsigned char reason = 0;
 299        int handled;
 300        bool b2b = false;
 301
 302        /*
 303         * CPU-specific NMI must be processed before non-CPU-specific
 304         * NMI, otherwise we may lose it, because the CPU-specific
 305         * NMI can not be detected/processed on other CPUs.
 306         */
 307
 308        /*
 309         * Back-to-back NMIs are interesting because they can either
 310         * be two NMI or more than two NMIs (any thing over two is dropped
 311         * due to NMI being edge-triggered).  If this is the second half
 312         * of the back-to-back NMI, assume we dropped things and process
 313         * more handlers.  Otherwise reset the 'swallow' NMI behaviour
 314         */
 315        if (regs->ip == __this_cpu_read(last_nmi_rip))
 316                b2b = true;
 317        else
 318                __this_cpu_write(swallow_nmi, false);
 319
 320        __this_cpu_write(last_nmi_rip, regs->ip);
 321
 322        handled = nmi_handle(NMI_LOCAL, regs, b2b);
 323        __this_cpu_add(nmi_stats.normal, handled);
 324        if (handled) {
 325                /*
 326                 * There are cases when a NMI handler handles multiple
 327                 * events in the current NMI.  One of these events may
 328                 * be queued for in the next NMI.  Because the event is
 329                 * already handled, the next NMI will result in an unknown
 330                 * NMI.  Instead lets flag this for a potential NMI to
 331                 * swallow.
 332                 */
 333                if (handled > 1)
 334                        __this_cpu_write(swallow_nmi, true);
 335                return;
 336        }
 337
 338        /*
 339         * Non-CPU-specific NMI: NMI sources can be processed on any CPU.
 340         *
 341         * Another CPU may be processing panic routines while holding
 342         * nmi_reason_lock. Check if the CPU issued the IPI for crash dumping,
 343         * and if so, call its callback directly.  If there is no CPU preparing
 344         * crash dump, we simply loop here.
 345         */
 346        while (!raw_spin_trylock(&nmi_reason_lock)) {
 347                run_crash_ipi_callback(regs);
 348                cpu_relax();
 349        }
 350
 351        reason = x86_platform.get_nmi_reason();
 352
 353        if (reason & NMI_REASON_MASK) {
 354                if (reason & NMI_REASON_SERR)
 355                        pci_serr_error(reason, regs);
 356                else if (reason & NMI_REASON_IOCHK)
 357                        io_check_error(reason, regs);
 358#ifdef CONFIG_X86_32
 359                /*
 360                 * Reassert NMI in case it became active
 361                 * meanwhile as it's edge-triggered:
 362                 */
 363                reassert_nmi();
 364#endif
 365                __this_cpu_add(nmi_stats.external, 1);
 366                raw_spin_unlock(&nmi_reason_lock);
 367                return;
 368        }
 369        raw_spin_unlock(&nmi_reason_lock);
 370
 371        /*
 372         * Only one NMI can be latched at a time.  To handle
 373         * this we may process multiple nmi handlers at once to
 374         * cover the case where an NMI is dropped.  The downside
 375         * to this approach is we may process an NMI prematurely,
 376         * while its real NMI is sitting latched.  This will cause
 377         * an unknown NMI on the next run of the NMI processing.
 378         *
 379         * We tried to flag that condition above, by setting the
 380         * swallow_nmi flag when we process more than one event.
 381         * This condition is also only present on the second half
 382         * of a back-to-back NMI, so we flag that condition too.
 383         *
 384         * If both are true, we assume we already processed this
 385         * NMI previously and we swallow it.  Otherwise we reset
 386         * the logic.
 387         *
 388         * There are scenarios where we may accidentally swallow
 389         * a 'real' unknown NMI.  For example, while processing
 390         * a perf NMI another perf NMI comes in along with a
 391         * 'real' unknown NMI.  These two NMIs get combined into
 392         * one (as descibed above).  When the next NMI gets
 393         * processed, it will be flagged by perf as handled, but
 394         * noone will know that there was a 'real' unknown NMI sent
 395         * also.  As a result it gets swallowed.  Or if the first
 396         * perf NMI returns two events handled then the second
 397         * NMI will get eaten by the logic below, again losing a
 398         * 'real' unknown NMI.  But this is the best we can do
 399         * for now.
 400         */
 401        if (b2b && __this_cpu_read(swallow_nmi))
 402                __this_cpu_add(nmi_stats.swallow, 1);
 403        else
 404                unknown_nmi_error(reason, regs);
 405}
 406
 407/*
 408 * NMIs can hit breakpoints which will cause it to lose its NMI context
 409 * with the CPU when the breakpoint or page fault does an IRET.
 410 *
 411 * As a result, NMIs can nest if NMIs get unmasked due an IRET during
 412 * NMI processing.  On x86_64, the asm glue protects us from nested NMIs
 413 * if the outer NMI came from kernel mode, but we can still nest if the
 414 * outer NMI came from user mode.
 415 *
 416 * To handle these nested NMIs, we have three states:
 417 *
 418 *  1) not running
 419 *  2) executing
 420 *  3) latched
 421 *
 422 * When no NMI is in progress, it is in the "not running" state.
 423 * When an NMI comes in, it goes into the "executing" state.
 424 * Normally, if another NMI is triggered, it does not interrupt
 425 * the running NMI and the HW will simply latch it so that when
 426 * the first NMI finishes, it will restart the second NMI.
 427 * (Note, the latch is binary, thus multiple NMIs triggering,
 428 *  when one is running, are ignored. Only one NMI is restarted.)
 429 *
 430 * If an NMI executes an iret, another NMI can preempt it. We do not
 431 * want to allow this new NMI to run, but we want to execute it when the
 432 * first one finishes.  We set the state to "latched", and the exit of
 433 * the first NMI will perform a dec_return, if the result is zero
 434 * (NOT_RUNNING), then it will simply exit the NMI handler. If not, the
 435 * dec_return would have set the state to NMI_EXECUTING (what we want it
 436 * to be when we are running). In this case, we simply jump back to
 437 * rerun the NMI handler again, and restart the 'latched' NMI.
 438 *
 439 * No trap (breakpoint or page fault) should be hit before nmi_restart,
 440 * thus there is no race between the first check of state for NOT_RUNNING
 441 * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs
 442 * at this point.
 443 *
 444 * In case the NMI takes a page fault, we need to save off the CR2
 445 * because the NMI could have preempted another page fault and corrupt
 446 * the CR2 that is about to be read. As nested NMIs must be restarted
 447 * and they can not take breakpoints or page faults, the update of the
 448 * CR2 must be done before converting the nmi state back to NOT_RUNNING.
 449 * Otherwise, there would be a race of another nested NMI coming in
 450 * after setting state to NOT_RUNNING but before updating the nmi_cr2.
 451 */
 452enum nmi_states {
 453        NMI_NOT_RUNNING = 0,
 454        NMI_EXECUTING,
 455        NMI_LATCHED,
 456};
 457static DEFINE_PER_CPU(enum nmi_states, nmi_state);
 458static DEFINE_PER_CPU(unsigned long, nmi_cr2);
 459
 460#ifdef CONFIG_X86_64
 461/*
 462 * In x86_64, we need to handle breakpoint -> NMI -> breakpoint.  Without
 463 * some care, the inner breakpoint will clobber the outer breakpoint's
 464 * stack.
 465 *
 466 * If a breakpoint is being processed, and the debug stack is being
 467 * used, if an NMI comes in and also hits a breakpoint, the stack
 468 * pointer will be set to the same fixed address as the breakpoint that
 469 * was interrupted, causing that stack to be corrupted. To handle this
 470 * case, check if the stack that was interrupted is the debug stack, and
 471 * if so, change the IDT so that new breakpoints will use the current
 472 * stack and not switch to the fixed address. On return of the NMI,
 473 * switch back to the original IDT.
 474 */
 475static DEFINE_PER_CPU(int, update_debug_stack);
 476#endif
 477
 478dotraplinkage notrace __kprobes void
 479do_nmi(struct pt_regs *regs, long error_code)
 480{
 481        if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) {
 482                this_cpu_write(nmi_state, NMI_LATCHED);
 483                return;
 484        }
 485        this_cpu_write(nmi_state, NMI_EXECUTING);
 486        this_cpu_write(nmi_cr2, read_cr2());
 487nmi_restart:
 488
 489#ifdef CONFIG_X86_64
 490        /*
 491         * If we interrupted a breakpoint, it is possible that
 492         * the nmi handler will have breakpoints too. We need to
 493         * change the IDT such that breakpoints that happen here
 494         * continue to use the NMI stack.
 495         */
 496        if (unlikely(is_debug_stack(regs->sp))) {
 497                debug_stack_set_zero();
 498                this_cpu_write(update_debug_stack, 1);
 499        }
 500#endif
 501
 502        nmi_enter();
 503
 504        inc_irq_stat(__nmi_count);
 505
 506        if (!ignore_nmis)
 507                default_do_nmi(regs);
 508
 509        nmi_exit();
 510
 511#ifdef CONFIG_X86_64
 512        if (unlikely(this_cpu_read(update_debug_stack))) {
 513                debug_stack_reset();
 514                this_cpu_write(update_debug_stack, 0);
 515        }
 516#endif
 517
 518        if (unlikely(this_cpu_read(nmi_cr2) != read_cr2()))
 519                write_cr2(this_cpu_read(nmi_cr2));
 520        if (this_cpu_dec_return(nmi_state))
 521                goto nmi_restart;
 522}
 523
 524void stop_nmi(void)
 525{
 526        ignore_nmis++;
 527}
 528
 529void restart_nmi(void)
 530{
 531        ignore_nmis--;
 532}
 533
 534/* reset the back-to-back NMI logic */
 535void local_touch_nmi(void)
 536{
 537        __this_cpu_write(last_nmi_rip, 0);
 538}
 539EXPORT_SYMBOL_GPL(local_touch_nmi);
 540