2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
10 * Handle hardware traps and faults.
12 #include <linux/spinlock.h>
13 #include <linux/kprobes.h>
14 #include <linux/kdebug.h>
15 #include <linux/nmi.h>
16 #include <linux/delay.h>
17 #include <linux/hardirq.h>
18 #include <linux/slab.h>
20 #if defined(CONFIG_EDAC)
21 #include <linux/edac.h>
24 #include <linux/atomic.h>
25 #include <asm/traps.h>
26 #include <asm/mach_traps.h>
29 #define NMI_MAX_NAMELEN 16
31 struct list_head list;
32 nmi_handler_t handler;
39 struct list_head head;
42 static struct nmi_desc nmi_desc[NMI_MAX] =
45 .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
46 .head = LIST_HEAD_INIT(nmi_desc[0].head),
49 .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
50 .head = LIST_HEAD_INIT(nmi_desc[1].head),
55 static int ignore_nmis;
57 int unknown_nmi_panic;
59 * Prevent NMI reason port (0x61) being accessed simultaneously, can
60 * only be used in NMI handler.
62 static DEFINE_RAW_SPINLOCK(nmi_reason_lock);
64 static int __init setup_unknown_nmi_panic(char *str)
66 unknown_nmi_panic = 1;
69 __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
71 #define nmi_to_desc(type) (&nmi_desc[type])
73 static int notrace __kprobes nmi_handle(unsigned int type, struct pt_regs *regs)
75 struct nmi_desc *desc = nmi_to_desc(type);
82 * NMIs are edge-triggered, which means if you have enough
83 * of them concurrently, you can lose some because only one
84 * can be latched at any given time. Walk the whole list
85 * to handle those situations.
87 list_for_each_entry_rcu(a, &desc->head, list) {
89 handled += a->handler(type, regs);
95 /* return total number of NMI events handled */
99 static int __setup_nmi(unsigned int type, struct nmiaction *action)
101 struct nmi_desc *desc = nmi_to_desc(type);
104 spin_lock_irqsave(&desc->lock, flags);
107 * some handlers need to be executed first otherwise a fake
108 * event confuses some handlers (kdump uses this flag)
110 if (action->flags & NMI_FLAG_FIRST)
111 list_add_rcu(&action->list, &desc->head);
113 list_add_tail_rcu(&action->list, &desc->head);
115 spin_unlock_irqrestore(&desc->lock, flags);
119 static struct nmiaction *__free_nmi(unsigned int type, const char *name)
121 struct nmi_desc *desc = nmi_to_desc(type);
125 spin_lock_irqsave(&desc->lock, flags);
127 list_for_each_entry_rcu(n, &desc->head, list) {
129 * the name passed in to describe the nmi handler
130 * is used as the lookup key
132 if (!strcmp(n->name, name)) {
134 "Trying to free NMI (%s) from NMI context!\n", n->name);
135 list_del_rcu(&n->list);
140 spin_unlock_irqrestore(&desc->lock, flags);
145 int register_nmi_handler(unsigned int type, nmi_handler_t handler,
146 unsigned long nmiflags, const char *devname)
148 struct nmiaction *action;
149 int retval = -ENOMEM;
154 action = kzalloc(sizeof(struct nmiaction), GFP_KERNEL);
158 action->handler = handler;
159 action->flags = nmiflags;
160 action->name = kstrndup(devname, NMI_MAX_NAMELEN, GFP_KERNEL);
162 goto fail_action_name;
164 retval = __setup_nmi(type, action);
179 EXPORT_SYMBOL_GPL(register_nmi_handler);
181 void unregister_nmi_handler(unsigned int type, const char *name)
185 a = __free_nmi(type, name);
192 EXPORT_SYMBOL_GPL(unregister_nmi_handler);
194 static notrace __kprobes void
195 pci_serr_error(unsigned char reason, struct pt_regs *regs)
197 pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
198 reason, smp_processor_id());
201 * On some machines, PCI SERR line is used to report memory
202 * errors. EDAC makes use of it.
204 #if defined(CONFIG_EDAC)
205 if (edac_handler_set()) {
206 edac_atomic_assert_error();
211 if (panic_on_unrecovered_nmi)
212 panic("NMI: Not continuing");
214 pr_emerg("Dazed and confused, but trying to continue\n");
216 /* Clear and disable the PCI SERR error line. */
217 reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_SERR;
218 outb(reason, NMI_REASON_PORT);
221 static notrace __kprobes void
222 io_check_error(unsigned char reason, struct pt_regs *regs)
227 "NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
228 reason, smp_processor_id());
229 show_registers(regs);
232 panic("NMI IOCK error: Not continuing");
234 /* Re-enable the IOCK line, wait for a few seconds */
235 reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
236 outb(reason, NMI_REASON_PORT);
240 touch_nmi_watchdog();
244 reason &= ~NMI_REASON_CLEAR_IOCHK;
245 outb(reason, NMI_REASON_PORT);
248 static notrace __kprobes void
249 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
251 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
256 * Might actually be able to figure out what the guilty party
264 pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
265 reason, smp_processor_id());
267 pr_emerg("Do you have a strange power saving mode enabled?\n");
268 if (unknown_nmi_panic || panic_on_unrecovered_nmi)
269 panic("NMI: Not continuing");
271 pr_emerg("Dazed and confused, but trying to continue\n");
274 static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
276 unsigned char reason = 0;
279 * CPU-specific NMI must be processed before non-CPU-specific
280 * NMI, otherwise we may lose it, because the CPU-specific
281 * NMI can not be detected/processed on other CPUs.
283 if (notify_die(DIE_NMI, "nmi", regs, 0, 2, SIGINT) == NOTIFY_STOP)
286 /* Non-CPU-specific NMI: NMI sources can be processed on any CPU */
287 raw_spin_lock(&nmi_reason_lock);
288 reason = get_nmi_reason();
290 if (reason & NMI_REASON_MASK) {
291 if (reason & NMI_REASON_SERR)
292 pci_serr_error(reason, regs);
293 else if (reason & NMI_REASON_IOCHK)
294 io_check_error(reason, regs);
297 * Reassert NMI in case it became active
298 * meanwhile as it's edge-triggered:
302 raw_spin_unlock(&nmi_reason_lock);
305 raw_spin_unlock(&nmi_reason_lock);
307 unknown_nmi_error(reason, regs);
310 dotraplinkage notrace __kprobes void
311 do_nmi(struct pt_regs *regs, long error_code)
315 inc_irq_stat(__nmi_count);
318 default_do_nmi(regs);
328 void restart_nmi(void)