xref: /openbmc/linux/arch/x86/kernel/nmi.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 1991, 1992  Linus Torvalds
4  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5  *  Copyright (C) 2011	Don Zickus Red Hat, Inc.
6  *
7  *  Pentium III FXSR, SSE support
8  *	Gareth Hughes <gareth@valinux.com>, May 2000
9  */
10 
11 /*
12  * Handle hardware traps and faults.
13  */
14 #include <linux/spinlock.h>
15 #include <linux/kprobes.h>
16 #include <linux/kdebug.h>
17 #include <linux/sched/debug.h>
18 #include <linux/nmi.h>
19 #include <linux/debugfs.h>
20 #include <linux/delay.h>
21 #include <linux/hardirq.h>
22 #include <linux/ratelimit.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
25 #include <linux/atomic.h>
26 #include <linux/sched/clock.h>
27 
28 #include <asm/cpu_entry_area.h>
29 #include <asm/traps.h>
30 #include <asm/mach_traps.h>
31 #include <asm/nmi.h>
32 #include <asm/x86_init.h>
33 #include <asm/reboot.h>
34 #include <asm/cache.h>
35 #include <asm/nospec-branch.h>
36 #include <asm/microcode.h>
37 #include <asm/sev.h>
38 
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/nmi.h>
41 
42 /*
43  * An emergency handler can be set in any context including NMI
44  */
45 struct nmi_desc {
46 	raw_spinlock_t lock;
47 	nmi_handler_t emerg_handler;
48 	struct list_head head;
49 };
50 
51 static struct nmi_desc nmi_desc[NMI_MAX] =
52 {
53 	{
54 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
55 		.head = LIST_HEAD_INIT(nmi_desc[0].head),
56 	},
57 	{
58 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
59 		.head = LIST_HEAD_INIT(nmi_desc[1].head),
60 	},
61 	{
62 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[2].lock),
63 		.head = LIST_HEAD_INIT(nmi_desc[2].head),
64 	},
65 	{
66 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[3].lock),
67 		.head = LIST_HEAD_INIT(nmi_desc[3].head),
68 	},
69 
70 };
71 
72 struct nmi_stats {
73 	unsigned int normal;
74 	unsigned int unknown;
75 	unsigned int external;
76 	unsigned int swallow;
77 	unsigned long recv_jiffies;
78 	unsigned long idt_seq;
79 	unsigned long idt_nmi_seq;
80 	unsigned long idt_ignored;
81 	atomic_long_t idt_calls;
82 	unsigned long idt_seq_snap;
83 	unsigned long idt_nmi_seq_snap;
84 	unsigned long idt_ignored_snap;
85 	long idt_calls_snap;
86 };
87 
88 static DEFINE_PER_CPU(struct nmi_stats, nmi_stats);
89 
90 static int ignore_nmis __read_mostly;
91 
92 int unknown_nmi_panic;
93 /*
94  * Prevent NMI reason port (0x61) being accessed simultaneously, can
95  * only be used in NMI handler.
96  */
97 static DEFINE_RAW_SPINLOCK(nmi_reason_lock);
98 
setup_unknown_nmi_panic(char * str)99 static int __init setup_unknown_nmi_panic(char *str)
100 {
101 	unknown_nmi_panic = 1;
102 	return 1;
103 }
104 __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
105 
106 #define nmi_to_desc(type) (&nmi_desc[type])
107 
108 static u64 nmi_longest_ns = 1 * NSEC_PER_MSEC;
109 
nmi_warning_debugfs(void)110 static int __init nmi_warning_debugfs(void)
111 {
112 	debugfs_create_u64("nmi_longest_ns", 0644,
113 			arch_debugfs_dir, &nmi_longest_ns);
114 	return 0;
115 }
116 fs_initcall(nmi_warning_debugfs);
117 
nmi_check_duration(struct nmiaction * action,u64 duration)118 static void nmi_check_duration(struct nmiaction *action, u64 duration)
119 {
120 	int remainder_ns, decimal_msecs;
121 
122 	if (duration < nmi_longest_ns || duration < action->max_duration)
123 		return;
124 
125 	action->max_duration = duration;
126 
127 	remainder_ns = do_div(duration, (1000 * 1000));
128 	decimal_msecs = remainder_ns / 1000;
129 
130 	printk_ratelimited(KERN_INFO
131 		"INFO: NMI handler (%ps) took too long to run: %lld.%03d msecs\n",
132 		action->handler, duration, decimal_msecs);
133 }
134 
nmi_handle(unsigned int type,struct pt_regs * regs)135 static int nmi_handle(unsigned int type, struct pt_regs *regs)
136 {
137 	struct nmi_desc *desc = nmi_to_desc(type);
138 	nmi_handler_t ehandler;
139 	struct nmiaction *a;
140 	int handled=0;
141 
142 	/*
143 	 * Call the emergency handler, if set
144 	 *
145 	 * In the case of crash_nmi_callback() emergency handler, it will
146 	 * return in the case of the crashing CPU to enable it to complete
147 	 * other necessary crashing actions ASAP. Other handlers in the
148 	 * linked list won't need to be run.
149 	 */
150 	ehandler = desc->emerg_handler;
151 	if (ehandler)
152 		return ehandler(type, regs);
153 
154 	rcu_read_lock();
155 
156 	/*
157 	 * NMIs are edge-triggered, which means if you have enough
158 	 * of them concurrently, you can lose some because only one
159 	 * can be latched at any given time.  Walk the whole list
160 	 * to handle those situations.
161 	 */
162 	list_for_each_entry_rcu(a, &desc->head, list) {
163 		int thishandled;
164 		u64 delta;
165 
166 		delta = sched_clock();
167 		thishandled = a->handler(type, regs);
168 		handled += thishandled;
169 		delta = sched_clock() - delta;
170 		trace_nmi_handler(a->handler, (int)delta, thishandled);
171 
172 		nmi_check_duration(a, delta);
173 	}
174 
175 	rcu_read_unlock();
176 
177 	/* return total number of NMI events handled */
178 	return handled;
179 }
180 NOKPROBE_SYMBOL(nmi_handle);
181 
__register_nmi_handler(unsigned int type,struct nmiaction * action)182 int __register_nmi_handler(unsigned int type, struct nmiaction *action)
183 {
184 	struct nmi_desc *desc = nmi_to_desc(type);
185 	unsigned long flags;
186 
187 	if (WARN_ON_ONCE(!action->handler || !list_empty(&action->list)))
188 		return -EINVAL;
189 
190 	raw_spin_lock_irqsave(&desc->lock, flags);
191 
192 	/*
193 	 * Indicate if there are multiple registrations on the
194 	 * internal NMI handler call chains (SERR and IO_CHECK).
195 	 */
196 	WARN_ON_ONCE(type == NMI_SERR && !list_empty(&desc->head));
197 	WARN_ON_ONCE(type == NMI_IO_CHECK && !list_empty(&desc->head));
198 
199 	/*
200 	 * some handlers need to be executed first otherwise a fake
201 	 * event confuses some handlers (kdump uses this flag)
202 	 */
203 	if (action->flags & NMI_FLAG_FIRST)
204 		list_add_rcu(&action->list, &desc->head);
205 	else
206 		list_add_tail_rcu(&action->list, &desc->head);
207 
208 	raw_spin_unlock_irqrestore(&desc->lock, flags);
209 	return 0;
210 }
211 EXPORT_SYMBOL(__register_nmi_handler);
212 
unregister_nmi_handler(unsigned int type,const char * name)213 void unregister_nmi_handler(unsigned int type, const char *name)
214 {
215 	struct nmi_desc *desc = nmi_to_desc(type);
216 	struct nmiaction *n, *found = NULL;
217 	unsigned long flags;
218 
219 	raw_spin_lock_irqsave(&desc->lock, flags);
220 
221 	list_for_each_entry_rcu(n, &desc->head, list) {
222 		/*
223 		 * the name passed in to describe the nmi handler
224 		 * is used as the lookup key
225 		 */
226 		if (!strcmp(n->name, name)) {
227 			WARN(in_nmi(),
228 				"Trying to free NMI (%s) from NMI context!\n", n->name);
229 			list_del_rcu(&n->list);
230 			found = n;
231 			break;
232 		}
233 	}
234 
235 	raw_spin_unlock_irqrestore(&desc->lock, flags);
236 	if (found) {
237 		synchronize_rcu();
238 		INIT_LIST_HEAD(&found->list);
239 	}
240 }
241 EXPORT_SYMBOL_GPL(unregister_nmi_handler);
242 
243 /**
244  * set_emergency_nmi_handler - Set emergency handler
245  * @type:    NMI type
246  * @handler: the emergency handler to be stored
247  *
248  * Set an emergency NMI handler which, if set, will preempt all the other
249  * handlers in the linked list. If a NULL handler is passed in, it will clear
250  * it. It is expected that concurrent calls to this function will not happen
251  * or the system is screwed beyond repair.
252  */
set_emergency_nmi_handler(unsigned int type,nmi_handler_t handler)253 void set_emergency_nmi_handler(unsigned int type, nmi_handler_t handler)
254 {
255 	struct nmi_desc *desc = nmi_to_desc(type);
256 
257 	if (WARN_ON_ONCE(desc->emerg_handler == handler))
258 		return;
259 	desc->emerg_handler = handler;
260 
261 	/*
262 	 * Ensure the emergency handler is visible to other CPUs before
263 	 * function return
264 	 */
265 	smp_wmb();
266 }
267 
268 static void
pci_serr_error(unsigned char reason,struct pt_regs * regs)269 pci_serr_error(unsigned char reason, struct pt_regs *regs)
270 {
271 	/* check to see if anyone registered against these types of errors */
272 	if (nmi_handle(NMI_SERR, regs))
273 		return;
274 
275 	pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
276 		 reason, smp_processor_id());
277 
278 	if (panic_on_unrecovered_nmi)
279 		nmi_panic(regs, "NMI: Not continuing");
280 
281 	pr_emerg("Dazed and confused, but trying to continue\n");
282 
283 	/* Clear and disable the PCI SERR error line. */
284 	reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_SERR;
285 	outb(reason, NMI_REASON_PORT);
286 }
287 NOKPROBE_SYMBOL(pci_serr_error);
288 
289 static void
io_check_error(unsigned char reason,struct pt_regs * regs)290 io_check_error(unsigned char reason, struct pt_regs *regs)
291 {
292 	unsigned long i;
293 
294 	/* check to see if anyone registered against these types of errors */
295 	if (nmi_handle(NMI_IO_CHECK, regs))
296 		return;
297 
298 	pr_emerg(
299 	"NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
300 		 reason, smp_processor_id());
301 	show_regs(regs);
302 
303 	if (panic_on_io_nmi) {
304 		nmi_panic(regs, "NMI IOCK error: Not continuing");
305 
306 		/*
307 		 * If we end up here, it means we have received an NMI while
308 		 * processing panic(). Simply return without delaying and
309 		 * re-enabling NMIs.
310 		 */
311 		return;
312 	}
313 
314 	/* Re-enable the IOCK line, wait for a few seconds */
315 	reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
316 	outb(reason, NMI_REASON_PORT);
317 
318 	i = 20000;
319 	while (--i) {
320 		touch_nmi_watchdog();
321 		udelay(100);
322 	}
323 
324 	reason &= ~NMI_REASON_CLEAR_IOCHK;
325 	outb(reason, NMI_REASON_PORT);
326 }
327 NOKPROBE_SYMBOL(io_check_error);
328 
329 static void
unknown_nmi_error(unsigned char reason,struct pt_regs * regs)330 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
331 {
332 	int handled;
333 
334 	/*
335 	 * Use 'false' as back-to-back NMIs are dealt with one level up.
336 	 * Of course this makes having multiple 'unknown' handlers useless
337 	 * as only the first one is ever run (unless it can actually determine
338 	 * if it caused the NMI)
339 	 */
340 	handled = nmi_handle(NMI_UNKNOWN, regs);
341 	if (handled) {
342 		__this_cpu_add(nmi_stats.unknown, handled);
343 		return;
344 	}
345 
346 	__this_cpu_add(nmi_stats.unknown, 1);
347 
348 	pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
349 		 reason, smp_processor_id());
350 
351 	if (unknown_nmi_panic || panic_on_unrecovered_nmi)
352 		nmi_panic(regs, "NMI: Not continuing");
353 
354 	pr_emerg("Dazed and confused, but trying to continue\n");
355 }
356 NOKPROBE_SYMBOL(unknown_nmi_error);
357 
358 static DEFINE_PER_CPU(bool, swallow_nmi);
359 static DEFINE_PER_CPU(unsigned long, last_nmi_rip);
360 
default_do_nmi(struct pt_regs * regs)361 static noinstr void default_do_nmi(struct pt_regs *regs)
362 {
363 	unsigned char reason = 0;
364 	int handled;
365 	bool b2b = false;
366 
367 	/*
368 	 * CPU-specific NMI must be processed before non-CPU-specific
369 	 * NMI, otherwise we may lose it, because the CPU-specific
370 	 * NMI can not be detected/processed on other CPUs.
371 	 */
372 
373 	/*
374 	 * Back-to-back NMIs are interesting because they can either
375 	 * be two NMI or more than two NMIs (any thing over two is dropped
376 	 * due to NMI being edge-triggered).  If this is the second half
377 	 * of the back-to-back NMI, assume we dropped things and process
378 	 * more handlers.  Otherwise reset the 'swallow' NMI behaviour
379 	 */
380 	if (regs->ip == __this_cpu_read(last_nmi_rip))
381 		b2b = true;
382 	else
383 		__this_cpu_write(swallow_nmi, false);
384 
385 	__this_cpu_write(last_nmi_rip, regs->ip);
386 
387 	instrumentation_begin();
388 
389 	if (microcode_nmi_handler_enabled() && microcode_nmi_handler())
390 		goto out;
391 
392 	handled = nmi_handle(NMI_LOCAL, regs);
393 	__this_cpu_add(nmi_stats.normal, handled);
394 	if (handled) {
395 		/*
396 		 * There are cases when a NMI handler handles multiple
397 		 * events in the current NMI.  One of these events may
398 		 * be queued for in the next NMI.  Because the event is
399 		 * already handled, the next NMI will result in an unknown
400 		 * NMI.  Instead lets flag this for a potential NMI to
401 		 * swallow.
402 		 */
403 		if (handled > 1)
404 			__this_cpu_write(swallow_nmi, true);
405 		goto out;
406 	}
407 
408 	/*
409 	 * Non-CPU-specific NMI: NMI sources can be processed on any CPU.
410 	 *
411 	 * Another CPU may be processing panic routines while holding
412 	 * nmi_reason_lock. Check if the CPU issued the IPI for crash dumping,
413 	 * and if so, call its callback directly.  If there is no CPU preparing
414 	 * crash dump, we simply loop here.
415 	 */
416 	while (!raw_spin_trylock(&nmi_reason_lock)) {
417 		run_crash_ipi_callback(regs);
418 		cpu_relax();
419 	}
420 
421 	reason = x86_platform.get_nmi_reason();
422 
423 	if (reason & NMI_REASON_MASK) {
424 		if (reason & NMI_REASON_SERR)
425 			pci_serr_error(reason, regs);
426 		else if (reason & NMI_REASON_IOCHK)
427 			io_check_error(reason, regs);
428 #ifdef CONFIG_X86_32
429 		/*
430 		 * Reassert NMI in case it became active
431 		 * meanwhile as it's edge-triggered:
432 		 */
433 		reassert_nmi();
434 #endif
435 		__this_cpu_add(nmi_stats.external, 1);
436 		raw_spin_unlock(&nmi_reason_lock);
437 		goto out;
438 	}
439 	raw_spin_unlock(&nmi_reason_lock);
440 
441 	/*
442 	 * Only one NMI can be latched at a time.  To handle
443 	 * this we may process multiple nmi handlers at once to
444 	 * cover the case where an NMI is dropped.  The downside
445 	 * to this approach is we may process an NMI prematurely,
446 	 * while its real NMI is sitting latched.  This will cause
447 	 * an unknown NMI on the next run of the NMI processing.
448 	 *
449 	 * We tried to flag that condition above, by setting the
450 	 * swallow_nmi flag when we process more than one event.
451 	 * This condition is also only present on the second half
452 	 * of a back-to-back NMI, so we flag that condition too.
453 	 *
454 	 * If both are true, we assume we already processed this
455 	 * NMI previously and we swallow it.  Otherwise we reset
456 	 * the logic.
457 	 *
458 	 * There are scenarios where we may accidentally swallow
459 	 * a 'real' unknown NMI.  For example, while processing
460 	 * a perf NMI another perf NMI comes in along with a
461 	 * 'real' unknown NMI.  These two NMIs get combined into
462 	 * one (as described above).  When the next NMI gets
463 	 * processed, it will be flagged by perf as handled, but
464 	 * no one will know that there was a 'real' unknown NMI sent
465 	 * also.  As a result it gets swallowed.  Or if the first
466 	 * perf NMI returns two events handled then the second
467 	 * NMI will get eaten by the logic below, again losing a
468 	 * 'real' unknown NMI.  But this is the best we can do
469 	 * for now.
470 	 */
471 	if (b2b && __this_cpu_read(swallow_nmi))
472 		__this_cpu_add(nmi_stats.swallow, 1);
473 	else
474 		unknown_nmi_error(reason, regs);
475 
476 out:
477 	instrumentation_end();
478 }
479 
480 /*
481  * NMIs can page fault or hit breakpoints which will cause it to lose
482  * its NMI context with the CPU when the breakpoint or page fault does an IRET.
483  *
484  * As a result, NMIs can nest if NMIs get unmasked due an IRET during
485  * NMI processing.  On x86_64, the asm glue protects us from nested NMIs
486  * if the outer NMI came from kernel mode, but we can still nest if the
487  * outer NMI came from user mode.
488  *
489  * To handle these nested NMIs, we have three states:
490  *
491  *  1) not running
492  *  2) executing
493  *  3) latched
494  *
495  * When no NMI is in progress, it is in the "not running" state.
496  * When an NMI comes in, it goes into the "executing" state.
497  * Normally, if another NMI is triggered, it does not interrupt
498  * the running NMI and the HW will simply latch it so that when
499  * the first NMI finishes, it will restart the second NMI.
500  * (Note, the latch is binary, thus multiple NMIs triggering,
501  *  when one is running, are ignored. Only one NMI is restarted.)
502  *
503  * If an NMI executes an iret, another NMI can preempt it. We do not
504  * want to allow this new NMI to run, but we want to execute it when the
505  * first one finishes.  We set the state to "latched", and the exit of
506  * the first NMI will perform a dec_return, if the result is zero
507  * (NOT_RUNNING), then it will simply exit the NMI handler. If not, the
508  * dec_return would have set the state to NMI_EXECUTING (what we want it
509  * to be when we are running). In this case, we simply jump back to
510  * rerun the NMI handler again, and restart the 'latched' NMI.
511  *
512  * No trap (breakpoint or page fault) should be hit before nmi_restart,
513  * thus there is no race between the first check of state for NOT_RUNNING
514  * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs
515  * at this point.
516  *
517  * In case the NMI takes a page fault, we need to save off the CR2
518  * because the NMI could have preempted another page fault and corrupt
519  * the CR2 that is about to be read. As nested NMIs must be restarted
520  * and they can not take breakpoints or page faults, the update of the
521  * CR2 must be done before converting the nmi state back to NOT_RUNNING.
522  * Otherwise, there would be a race of another nested NMI coming in
523  * after setting state to NOT_RUNNING but before updating the nmi_cr2.
524  */
525 enum nmi_states {
526 	NMI_NOT_RUNNING = 0,
527 	NMI_EXECUTING,
528 	NMI_LATCHED,
529 };
530 static DEFINE_PER_CPU(enum nmi_states, nmi_state);
531 static DEFINE_PER_CPU(unsigned long, nmi_cr2);
532 static DEFINE_PER_CPU(unsigned long, nmi_dr7);
533 
DEFINE_IDTENTRY_RAW(exc_nmi)534 DEFINE_IDTENTRY_RAW(exc_nmi)
535 {
536 	irqentry_state_t irq_state;
537 	struct nmi_stats *nsp = this_cpu_ptr(&nmi_stats);
538 
539 	/*
540 	 * Re-enable NMIs right here when running as an SEV-ES guest. This might
541 	 * cause nested NMIs, but those can be handled safely.
542 	 */
543 	sev_es_nmi_complete();
544 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU))
545 		raw_atomic_long_inc(&nsp->idt_calls);
546 
547 	if (IS_ENABLED(CONFIG_SMP) && arch_cpu_is_offline(smp_processor_id())) {
548 		if (microcode_nmi_handler_enabled())
549 			microcode_offline_nmi_handler();
550 		return;
551 	}
552 
553 	if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) {
554 		this_cpu_write(nmi_state, NMI_LATCHED);
555 		return;
556 	}
557 	this_cpu_write(nmi_state, NMI_EXECUTING);
558 	this_cpu_write(nmi_cr2, read_cr2());
559 
560 nmi_restart:
561 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
562 		WRITE_ONCE(nsp->idt_seq, nsp->idt_seq + 1);
563 		WARN_ON_ONCE(!(nsp->idt_seq & 0x1));
564 		WRITE_ONCE(nsp->recv_jiffies, jiffies);
565 	}
566 
567 	/*
568 	 * Needs to happen before DR7 is accessed, because the hypervisor can
569 	 * intercept DR7 reads/writes, turning those into #VC exceptions.
570 	 */
571 	sev_es_ist_enter(regs);
572 
573 	this_cpu_write(nmi_dr7, local_db_save());
574 
575 	irq_state = irqentry_nmi_enter(regs);
576 
577 	inc_irq_stat(__nmi_count);
578 
579 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU) && ignore_nmis) {
580 		WRITE_ONCE(nsp->idt_ignored, nsp->idt_ignored + 1);
581 	} else if (!ignore_nmis) {
582 		if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
583 			WRITE_ONCE(nsp->idt_nmi_seq, nsp->idt_nmi_seq + 1);
584 			WARN_ON_ONCE(!(nsp->idt_nmi_seq & 0x1));
585 		}
586 		default_do_nmi(regs);
587 		if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
588 			WRITE_ONCE(nsp->idt_nmi_seq, nsp->idt_nmi_seq + 1);
589 			WARN_ON_ONCE(nsp->idt_nmi_seq & 0x1);
590 		}
591 	}
592 
593 	irqentry_nmi_exit(regs, irq_state);
594 
595 	local_db_restore(this_cpu_read(nmi_dr7));
596 
597 	sev_es_ist_exit();
598 
599 	if (unlikely(this_cpu_read(nmi_cr2) != read_cr2()))
600 		write_cr2(this_cpu_read(nmi_cr2));
601 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
602 		WRITE_ONCE(nsp->idt_seq, nsp->idt_seq + 1);
603 		WARN_ON_ONCE(nsp->idt_seq & 0x1);
604 		WRITE_ONCE(nsp->recv_jiffies, jiffies);
605 	}
606 	if (this_cpu_dec_return(nmi_state))
607 		goto nmi_restart;
608 }
609 
610 #if IS_ENABLED(CONFIG_KVM_INTEL)
DEFINE_IDTENTRY_RAW(exc_nmi_kvm_vmx)611 DEFINE_IDTENTRY_RAW(exc_nmi_kvm_vmx)
612 {
613 	exc_nmi(regs);
614 }
615 #if IS_MODULE(CONFIG_KVM_INTEL)
616 EXPORT_SYMBOL_GPL(asm_exc_nmi_kvm_vmx);
617 #endif
618 #endif
619 
620 #ifdef CONFIG_NMI_CHECK_CPU
621 
622 static char *nmi_check_stall_msg[] = {
623 /*									*/
624 /* +--------- nsp->idt_seq_snap & 0x1: CPU is in NMI handler.		*/
625 /* | +------ cpu_is_offline(cpu)					*/
626 /* | | +--- nsp->idt_calls_snap != atomic_long_read(&nsp->idt_calls):	*/
627 /* | | |	NMI handler has been invoked.				*/
628 /* | | |								*/
629 /* V V V								*/
630 /* 0 0 0 */ "NMIs are not reaching exc_nmi() handler",
631 /* 0 0 1 */ "exc_nmi() handler is ignoring NMIs",
632 /* 0 1 0 */ "CPU is offline and NMIs are not reaching exc_nmi() handler",
633 /* 0 1 1 */ "CPU is offline and exc_nmi() handler is legitimately ignoring NMIs",
634 /* 1 0 0 */ "CPU is in exc_nmi() handler and no further NMIs are reaching handler",
635 /* 1 0 1 */ "CPU is in exc_nmi() handler which is legitimately ignoring NMIs",
636 /* 1 1 0 */ "CPU is offline in exc_nmi() handler and no more NMIs are reaching exc_nmi() handler",
637 /* 1 1 1 */ "CPU is offline in exc_nmi() handler which is legitimately ignoring NMIs",
638 };
639 
nmi_backtrace_stall_snap(const struct cpumask * btp)640 void nmi_backtrace_stall_snap(const struct cpumask *btp)
641 {
642 	int cpu;
643 	struct nmi_stats *nsp;
644 
645 	for_each_cpu(cpu, btp) {
646 		nsp = per_cpu_ptr(&nmi_stats, cpu);
647 		nsp->idt_seq_snap = READ_ONCE(nsp->idt_seq);
648 		nsp->idt_nmi_seq_snap = READ_ONCE(nsp->idt_nmi_seq);
649 		nsp->idt_ignored_snap = READ_ONCE(nsp->idt_ignored);
650 		nsp->idt_calls_snap = atomic_long_read(&nsp->idt_calls);
651 	}
652 }
653 
nmi_backtrace_stall_check(const struct cpumask * btp)654 void nmi_backtrace_stall_check(const struct cpumask *btp)
655 {
656 	int cpu;
657 	int idx;
658 	unsigned long nmi_seq;
659 	unsigned long j = jiffies;
660 	char *modp;
661 	char *msgp;
662 	char *msghp;
663 	struct nmi_stats *nsp;
664 
665 	for_each_cpu(cpu, btp) {
666 		nsp = per_cpu_ptr(&nmi_stats, cpu);
667 		modp = "";
668 		msghp = "";
669 		nmi_seq = READ_ONCE(nsp->idt_nmi_seq);
670 		if (nsp->idt_nmi_seq_snap + 1 == nmi_seq && (nmi_seq & 0x1)) {
671 			msgp = "CPU entered NMI handler function, but has not exited";
672 		} else if ((nsp->idt_nmi_seq_snap & 0x1) != (nmi_seq & 0x1)) {
673 			msgp = "CPU is handling NMIs";
674 		} else {
675 			idx = ((nsp->idt_seq_snap & 0x1) << 2) |
676 			      (cpu_is_offline(cpu) << 1) |
677 			      (nsp->idt_calls_snap != atomic_long_read(&nsp->idt_calls));
678 			msgp = nmi_check_stall_msg[idx];
679 			if (nsp->idt_ignored_snap != READ_ONCE(nsp->idt_ignored) && (idx & 0x1))
680 				modp = ", but OK because ignore_nmis was set";
681 			if (nmi_seq & 0x1)
682 				msghp = " (CPU currently in NMI handler function)";
683 			else if (nsp->idt_nmi_seq_snap + 1 == nmi_seq)
684 				msghp = " (CPU exited one NMI handler function)";
685 		}
686 		pr_alert("%s: CPU %d: %s%s%s, last activity: %lu jiffies ago.\n",
687 			 __func__, cpu, msgp, modp, msghp, j - READ_ONCE(nsp->recv_jiffies));
688 	}
689 }
690 
691 #endif
692 
stop_nmi(void)693 void stop_nmi(void)
694 {
695 	ignore_nmis++;
696 }
697 
restart_nmi(void)698 void restart_nmi(void)
699 {
700 	ignore_nmis--;
701 }
702 
703 /* reset the back-to-back NMI logic */
local_touch_nmi(void)704 void local_touch_nmi(void)
705 {
706 	__this_cpu_write(last_nmi_rip, 0);
707 }
708 EXPORT_SYMBOL_GPL(local_touch_nmi);
709