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.
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
11 * Handle hardware traps and faults.
13 #include <linux/spinlock.h>
14 #include <linux/kprobes.h>
15 #include <linux/kdebug.h>
16 #include <linux/nmi.h>
17 #include <linux/delay.h>
18 #include <linux/hardirq.h>
19 #include <linux/slab.h>
21 #if defined(CONFIG_EDAC)
22 #include <linux/edac.h>
25 #include <linux/atomic.h>
26 #include <asm/traps.h>
27 #include <asm/mach_traps.h>
30 #define NMI_MAX_NAMELEN 16
32 struct list_head list
;
33 nmi_handler_t handler
;
40 struct list_head head
;
43 static struct nmi_desc nmi_desc
[NMI_MAX
] =
46 .lock
= __SPIN_LOCK_UNLOCKED(&nmi_desc
[0].lock
),
47 .head
= LIST_HEAD_INIT(nmi_desc
[0].head
),
50 .lock
= __SPIN_LOCK_UNLOCKED(&nmi_desc
[1].lock
),
51 .head
= LIST_HEAD_INIT(nmi_desc
[1].head
),
56 static int ignore_nmis
;
58 int unknown_nmi_panic
;
60 * Prevent NMI reason port (0x61) being accessed simultaneously, can
61 * only be used in NMI handler.
63 static DEFINE_RAW_SPINLOCK(nmi_reason_lock
);
65 static int __init
setup_unknown_nmi_panic(char *str
)
67 unknown_nmi_panic
= 1;
70 __setup("unknown_nmi_panic", setup_unknown_nmi_panic
);
72 #define nmi_to_desc(type) (&nmi_desc[type])
74 static int notrace __kprobes
nmi_handle(unsigned int type
, struct pt_regs
*regs
)
76 struct nmi_desc
*desc
= nmi_to_desc(type
);
83 * NMIs are edge-triggered, which means if you have enough
84 * of them concurrently, you can lose some because only one
85 * can be latched at any given time. Walk the whole list
86 * to handle those situations.
88 list_for_each_entry_rcu(a
, &desc
->head
, list
) {
90 handled
+= a
->handler(type
, regs
);
96 /* return total number of NMI events handled */
100 static int __setup_nmi(unsigned int type
, struct nmiaction
*action
)
102 struct nmi_desc
*desc
= nmi_to_desc(type
);
105 spin_lock_irqsave(&desc
->lock
, flags
);
108 * some handlers need to be executed first otherwise a fake
109 * event confuses some handlers (kdump uses this flag)
111 if (action
->flags
& NMI_FLAG_FIRST
)
112 list_add_rcu(&action
->list
, &desc
->head
);
114 list_add_tail_rcu(&action
->list
, &desc
->head
);
116 spin_unlock_irqrestore(&desc
->lock
, flags
);
120 static struct nmiaction
*__free_nmi(unsigned int type
, const char *name
)
122 struct nmi_desc
*desc
= nmi_to_desc(type
);
126 spin_lock_irqsave(&desc
->lock
, flags
);
128 list_for_each_entry_rcu(n
, &desc
->head
, list
) {
130 * the name passed in to describe the nmi handler
131 * is used as the lookup key
133 if (!strcmp(n
->name
, name
)) {
135 "Trying to free NMI (%s) from NMI context!\n", n
->name
);
136 list_del_rcu(&n
->list
);
141 spin_unlock_irqrestore(&desc
->lock
, flags
);
146 int register_nmi_handler(unsigned int type
, nmi_handler_t handler
,
147 unsigned long nmiflags
, const char *devname
)
149 struct nmiaction
*action
;
150 int retval
= -ENOMEM
;
155 action
= kzalloc(sizeof(struct nmiaction
), GFP_KERNEL
);
159 action
->handler
= handler
;
160 action
->flags
= nmiflags
;
161 action
->name
= kstrndup(devname
, NMI_MAX_NAMELEN
, GFP_KERNEL
);
163 goto fail_action_name
;
165 retval
= __setup_nmi(type
, action
);
180 EXPORT_SYMBOL_GPL(register_nmi_handler
);
182 void unregister_nmi_handler(unsigned int type
, const char *name
)
186 a
= __free_nmi(type
, name
);
193 EXPORT_SYMBOL_GPL(unregister_nmi_handler
);
195 static notrace __kprobes
void
196 pci_serr_error(unsigned char reason
, struct pt_regs
*regs
)
198 pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
199 reason
, smp_processor_id());
202 * On some machines, PCI SERR line is used to report memory
203 * errors. EDAC makes use of it.
205 #if defined(CONFIG_EDAC)
206 if (edac_handler_set()) {
207 edac_atomic_assert_error();
212 if (panic_on_unrecovered_nmi
)
213 panic("NMI: Not continuing");
215 pr_emerg("Dazed and confused, but trying to continue\n");
217 /* Clear and disable the PCI SERR error line. */
218 reason
= (reason
& NMI_REASON_CLEAR_MASK
) | NMI_REASON_CLEAR_SERR
;
219 outb(reason
, NMI_REASON_PORT
);
222 static notrace __kprobes
void
223 io_check_error(unsigned char reason
, struct pt_regs
*regs
)
228 "NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
229 reason
, smp_processor_id());
230 show_registers(regs
);
233 panic("NMI IOCK error: Not continuing");
235 /* Re-enable the IOCK line, wait for a few seconds */
236 reason
= (reason
& NMI_REASON_CLEAR_MASK
) | NMI_REASON_CLEAR_IOCHK
;
237 outb(reason
, NMI_REASON_PORT
);
241 touch_nmi_watchdog();
245 reason
&= ~NMI_REASON_CLEAR_IOCHK
;
246 outb(reason
, NMI_REASON_PORT
);
249 static notrace __kprobes
void
250 unknown_nmi_error(unsigned char reason
, struct pt_regs
*regs
)
254 handled
= nmi_handle(NMI_UNKNOWN
, regs
);
259 * Might actually be able to figure out what the guilty party
267 pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
268 reason
, smp_processor_id());
270 pr_emerg("Do you have a strange power saving mode enabled?\n");
271 if (unknown_nmi_panic
|| panic_on_unrecovered_nmi
)
272 panic("NMI: Not continuing");
274 pr_emerg("Dazed and confused, but trying to continue\n");
277 static notrace __kprobes
void default_do_nmi(struct pt_regs
*regs
)
279 unsigned char reason
= 0;
283 * CPU-specific NMI must be processed before non-CPU-specific
284 * NMI, otherwise we may lose it, because the CPU-specific
285 * NMI can not be detected/processed on other CPUs.
287 handled
= nmi_handle(NMI_LOCAL
, regs
);
291 /* Non-CPU-specific NMI: NMI sources can be processed on any CPU */
292 raw_spin_lock(&nmi_reason_lock
);
293 reason
= get_nmi_reason();
295 if (reason
& NMI_REASON_MASK
) {
296 if (reason
& NMI_REASON_SERR
)
297 pci_serr_error(reason
, regs
);
298 else if (reason
& NMI_REASON_IOCHK
)
299 io_check_error(reason
, regs
);
302 * Reassert NMI in case it became active
303 * meanwhile as it's edge-triggered:
307 raw_spin_unlock(&nmi_reason_lock
);
310 raw_spin_unlock(&nmi_reason_lock
);
312 unknown_nmi_error(reason
, regs
);
315 dotraplinkage notrace __kprobes
void
316 do_nmi(struct pt_regs
*regs
, long error_code
)
320 inc_irq_stat(__nmi_count
);
323 default_do_nmi(regs
);
333 void restart_nmi(void)