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/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
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 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
106
107
108
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
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
149
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
156
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
179
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
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
206
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
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
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
244
245
246
247 return;
248 }
249
250
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
271
272
273
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
304
305
306
307
308
309
310
311
312
313
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
327
328
329
330
331
332
333 if (handled > 1)
334 __this_cpu_write(swallow_nmi, true);
335 return;
336 }
337
338
339
340
341
342
343
344
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
361
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
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 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
463
464
465
466
467
468
469
470
471
472
473
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
492
493
494
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
535void local_touch_nmi(void)
536{
537 __this_cpu_write(last_nmi_rip, 0);
538}
539EXPORT_SYMBOL_GPL(local_touch_nmi);
540