1
2
3
4
5
6
7
8
9
10
11
12
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>
20#include <linux/export.h>
21
22#include <linux/mca.h>
23
24#if defined(CONFIG_EDAC)
25#include <linux/edac.h>
26#endif
27
28#include <linux/atomic.h>
29#include <asm/traps.h>
30#include <asm/mach_traps.h>
31#include <asm/nmi.h>
32#include <asm/x86_init.h>
33
34#define NMI_MAX_NAMELEN 16
35struct nmiaction {
36 struct list_head list;
37 nmi_handler_t handler;
38 unsigned int flags;
39 char *name;
40};
41
42struct nmi_desc {
43 spinlock_t lock;
44 struct list_head head;
45};
46
47static struct nmi_desc nmi_desc[NMI_MAX] =
48{
49 {
50 .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
51 .head = LIST_HEAD_INIT(nmi_desc[0].head),
52 },
53 {
54 .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
55 .head = LIST_HEAD_INIT(nmi_desc[1].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
74
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 int notrace __kprobes nmi_handle(unsigned int type, struct pt_regs *regs, bool b2b)
88{
89 struct nmi_desc *desc = nmi_to_desc(type);
90 struct nmiaction *a;
91 int handled=0;
92
93 rcu_read_lock();
94
95
96
97
98
99
100
101 list_for_each_entry_rcu(a, &desc->head, list)
102 handled += a->handler(type, regs);
103
104 rcu_read_unlock();
105
106
107 return handled;
108}
109
110static int __setup_nmi(unsigned int type, struct nmiaction *action)
111{
112 struct nmi_desc *desc = nmi_to_desc(type);
113 unsigned long flags;
114
115 spin_lock_irqsave(&desc->lock, flags);
116
117
118
119
120
121
122 WARN_ON_ONCE(type == NMI_UNKNOWN && !list_empty(&desc->head));
123
124
125
126
127
128 if (action->flags & NMI_FLAG_FIRST)
129 list_add_rcu(&action->list, &desc->head);
130 else
131 list_add_tail_rcu(&action->list, &desc->head);
132
133 spin_unlock_irqrestore(&desc->lock, flags);
134 return 0;
135}
136
137static struct nmiaction *__free_nmi(unsigned int type, const char *name)
138{
139 struct nmi_desc *desc = nmi_to_desc(type);
140 struct nmiaction *n;
141 unsigned long flags;
142
143 spin_lock_irqsave(&desc->lock, flags);
144
145 list_for_each_entry_rcu(n, &desc->head, list) {
146
147
148
149
150 if (!strcmp(n->name, name)) {
151 WARN(in_nmi(),
152 "Trying to free NMI (%s) from NMI context!\n", n->name);
153 list_del_rcu(&n->list);
154 break;
155 }
156 }
157
158 spin_unlock_irqrestore(&desc->lock, flags);
159 synchronize_rcu();
160 return (n);
161}
162
163int register_nmi_handler(unsigned int type, nmi_handler_t handler,
164 unsigned long nmiflags, const char *devname)
165{
166 struct nmiaction *action;
167 int retval = -ENOMEM;
168
169 if (!handler)
170 return -EINVAL;
171
172 action = kzalloc(sizeof(struct nmiaction), GFP_KERNEL);
173 if (!action)
174 goto fail_action;
175
176 action->handler = handler;
177 action->flags = nmiflags;
178 action->name = kstrndup(devname, NMI_MAX_NAMELEN, GFP_KERNEL);
179 if (!action->name)
180 goto fail_action_name;
181
182 retval = __setup_nmi(type, action);
183
184 if (retval)
185 goto fail_setup_nmi;
186
187 return retval;
188
189fail_setup_nmi:
190 kfree(action->name);
191fail_action_name:
192 kfree(action);
193fail_action:
194
195 return retval;
196}
197EXPORT_SYMBOL_GPL(register_nmi_handler);
198
199void unregister_nmi_handler(unsigned int type, const char *name)
200{
201 struct nmiaction *a;
202
203 a = __free_nmi(type, name);
204 if (a) {
205 kfree(a->name);
206 kfree(a);
207 }
208}
209
210EXPORT_SYMBOL_GPL(unregister_nmi_handler);
211
212static notrace __kprobes void
213pci_serr_error(unsigned char reason, struct pt_regs *regs)
214{
215 pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
216 reason, smp_processor_id());
217
218
219
220
221
222#if defined(CONFIG_EDAC)
223 if (edac_handler_set()) {
224 edac_atomic_assert_error();
225 return;
226 }
227#endif
228
229 if (panic_on_unrecovered_nmi)
230 panic("NMI: Not continuing");
231
232 pr_emerg("Dazed and confused, but trying to continue\n");
233
234
235 reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_SERR;
236 outb(reason, NMI_REASON_PORT);
237}
238
239static notrace __kprobes void
240io_check_error(unsigned char reason, struct pt_regs *regs)
241{
242 unsigned long i;
243
244 pr_emerg(
245 "NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
246 reason, smp_processor_id());
247 show_registers(regs);
248
249 if (panic_on_io_nmi)
250 panic("NMI IOCK error: Not continuing");
251
252
253 reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
254 outb(reason, NMI_REASON_PORT);
255
256 i = 20000;
257 while (--i) {
258 touch_nmi_watchdog();
259 udelay(100);
260 }
261
262 reason &= ~NMI_REASON_CLEAR_IOCHK;
263 outb(reason, NMI_REASON_PORT);
264}
265
266static notrace __kprobes void
267unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
268{
269 int handled;
270
271
272
273
274
275
276
277 handled = nmi_handle(NMI_UNKNOWN, regs, false);
278 if (handled) {
279 __this_cpu_add(nmi_stats.unknown, handled);
280 return;
281 }
282
283 __this_cpu_add(nmi_stats.unknown, 1);
284
285#ifdef CONFIG_MCA
286
287
288
289
290 if (MCA_bus) {
291 mca_handle_nmi();
292 return;
293 }
294#endif
295 pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
296 reason, smp_processor_id());
297
298 pr_emerg("Do you have a strange power saving mode enabled?\n");
299 if (unknown_nmi_panic || panic_on_unrecovered_nmi)
300 panic("NMI: Not continuing");
301
302 pr_emerg("Dazed and confused, but trying to continue\n");
303}
304
305static DEFINE_PER_CPU(bool, swallow_nmi);
306static DEFINE_PER_CPU(unsigned long, last_nmi_rip);
307
308static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
309{
310 unsigned char reason = 0;
311 int handled;
312 bool b2b = false;
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327 if (regs->ip == __this_cpu_read(last_nmi_rip))
328 b2b = true;
329 else
330 __this_cpu_write(swallow_nmi, false);
331
332 __this_cpu_write(last_nmi_rip, regs->ip);
333
334 handled = nmi_handle(NMI_LOCAL, regs, b2b);
335 __this_cpu_add(nmi_stats.normal, handled);
336 if (handled) {
337
338
339
340
341
342
343
344
345 if (handled > 1)
346 __this_cpu_write(swallow_nmi, true);
347 return;
348 }
349
350
351 raw_spin_lock(&nmi_reason_lock);
352 reason = x86_platform.get_nmi_reason();
353
354 if (reason & NMI_REASON_MASK) {
355 if (reason & NMI_REASON_SERR)
356 pci_serr_error(reason, regs);
357 else if (reason & NMI_REASON_IOCHK)
358 io_check_error(reason, regs);
359#ifdef CONFIG_X86_32
360
361
362
363
364 reassert_nmi();
365#endif
366 __this_cpu_add(nmi_stats.external, 1);
367 raw_spin_unlock(&nmi_reason_lock);
368 return;
369 }
370 raw_spin_unlock(&nmi_reason_lock);
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402 if (b2b && __this_cpu_read(swallow_nmi))
403 __this_cpu_add(nmi_stats.swallow, 1);
404 else
405 unknown_nmi_error(reason, regs);
406}
407
408dotraplinkage notrace __kprobes void
409do_nmi(struct pt_regs *regs, long error_code)
410{
411 nmi_enter();
412
413 inc_irq_stat(__nmi_count);
414
415 if (!ignore_nmis)
416 default_do_nmi(regs);
417
418 nmi_exit();
419}
420
421void stop_nmi(void)
422{
423 ignore_nmis++;
424}
425
426void restart_nmi(void)
427{
428 ignore_nmis--;
429}
430
431
432void local_touch_nmi(void)
433{
434 __this_cpu_write(last_nmi_rip, 0);
435}
436