xref: /openbmc/linux/arch/x86/kernel/nmi.c (revision 0f9b4c3ca5fdf3e177266ef994071b1a03f07318)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21d48922cSDon Zickus /*
31d48922cSDon Zickus  *  Copyright (C) 1991, 1992  Linus Torvalds
41d48922cSDon Zickus  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
59c48f1c6SDon Zickus  *  Copyright (C) 2011	Don Zickus Red Hat, Inc.
61d48922cSDon Zickus  *
71d48922cSDon Zickus  *  Pentium III FXSR, SSE support
81d48922cSDon Zickus  *	Gareth Hughes <gareth@valinux.com>, May 2000
91d48922cSDon Zickus  */
101d48922cSDon Zickus 
111d48922cSDon Zickus /*
121d48922cSDon Zickus  * Handle hardware traps and faults.
131d48922cSDon Zickus  */
141d48922cSDon Zickus #include <linux/spinlock.h>
151d48922cSDon Zickus #include <linux/kprobes.h>
161d48922cSDon Zickus #include <linux/kdebug.h>
17b17b0153SIngo Molnar #include <linux/sched/debug.h>
181d48922cSDon Zickus #include <linux/nmi.h>
192ab00456SDave Hansen #include <linux/debugfs.h>
20c9126b2eSDon Zickus #include <linux/delay.h>
21c9126b2eSDon Zickus #include <linux/hardirq.h>
22c361db5cSArnd Bergmann #include <linux/ratelimit.h>
23c9126b2eSDon Zickus #include <linux/slab.h>
2469c60c88SPaul Gortmaker #include <linux/export.h>
252a594d4cSThomas Gleixner #include <linux/atomic.h>
26e6017571SIngo Molnar #include <linux/sched/clock.h>
271d48922cSDon Zickus 
282a594d4cSThomas Gleixner #include <asm/cpu_entry_area.h>
291d48922cSDon Zickus #include <asm/traps.h>
301d48922cSDon Zickus #include <asm/mach_traps.h>
31c9126b2eSDon Zickus #include <asm/nmi.h>
326fd36ba0SMathias Nyman #include <asm/x86_init.h>
33b279d67dSHidehiro Kawai #include <asm/reboot.h>
348e2a7f5bSKostenzer Felix #include <asm/cache.h>
3504dcbdb8SThomas Gleixner #include <asm/nospec-branch.h>
3652b5dd84SThomas Gleixner #include <asm/microcode.h>
37e759959fSBrijesh Singh #include <asm/sev.h>
38c9126b2eSDon Zickus 
390c4df02dSDave Hansen #define CREATE_TRACE_POINTS
400c4df02dSDave Hansen #include <trace/events/nmi.h>
410c4df02dSDave Hansen 
42c9126b2eSDon Zickus struct nmi_desc {
43c455fd92SScott Wood 	raw_spinlock_t lock;
44c9126b2eSDon Zickus 	struct list_head head;
45c9126b2eSDon Zickus };
46c9126b2eSDon Zickus 
47c9126b2eSDon Zickus static struct nmi_desc nmi_desc[NMI_MAX] =
48c9126b2eSDon Zickus {
49c9126b2eSDon Zickus 	{
50c455fd92SScott Wood 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
51c9126b2eSDon Zickus 		.head = LIST_HEAD_INIT(nmi_desc[0].head),
52c9126b2eSDon Zickus 	},
53c9126b2eSDon Zickus 	{
54c455fd92SScott Wood 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
55c9126b2eSDon Zickus 		.head = LIST_HEAD_INIT(nmi_desc[1].head),
56c9126b2eSDon Zickus 	},
57553222f3SDon Zickus 	{
58c455fd92SScott Wood 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[2].lock),
59553222f3SDon Zickus 		.head = LIST_HEAD_INIT(nmi_desc[2].head),
60553222f3SDon Zickus 	},
61553222f3SDon Zickus 	{
62c455fd92SScott Wood 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[3].lock),
63553222f3SDon Zickus 		.head = LIST_HEAD_INIT(nmi_desc[3].head),
64553222f3SDon Zickus 	},
65c9126b2eSDon Zickus 
66c9126b2eSDon Zickus };
671d48922cSDon Zickus 
68efc3aac5SDon Zickus struct nmi_stats {
69efc3aac5SDon Zickus 	unsigned int normal;
70efc3aac5SDon Zickus 	unsigned int unknown;
71efc3aac5SDon Zickus 	unsigned int external;
72efc3aac5SDon Zickus 	unsigned int swallow;
731a3ea611SPaul E. McKenney 	unsigned long recv_jiffies;
741a3ea611SPaul E. McKenney 	unsigned long idt_seq;
751a3ea611SPaul E. McKenney 	unsigned long idt_nmi_seq;
761a3ea611SPaul E. McKenney 	unsigned long idt_ignored;
771a3ea611SPaul E. McKenney 	atomic_long_t idt_calls;
781a3ea611SPaul E. McKenney 	unsigned long idt_seq_snap;
791a3ea611SPaul E. McKenney 	unsigned long idt_nmi_seq_snap;
801a3ea611SPaul E. McKenney 	unsigned long idt_ignored_snap;
811a3ea611SPaul E. McKenney 	long idt_calls_snap;
82efc3aac5SDon Zickus };
83efc3aac5SDon Zickus 
84efc3aac5SDon Zickus static DEFINE_PER_CPU(struct nmi_stats, nmi_stats);
85efc3aac5SDon Zickus 
868e2a7f5bSKostenzer Felix static int ignore_nmis __read_mostly;
871d48922cSDon Zickus 
881d48922cSDon Zickus int unknown_nmi_panic;
891d48922cSDon Zickus /*
901d48922cSDon Zickus  * Prevent NMI reason port (0x61) being accessed simultaneously, can
911d48922cSDon Zickus  * only be used in NMI handler.
921d48922cSDon Zickus  */
931d48922cSDon Zickus static DEFINE_RAW_SPINLOCK(nmi_reason_lock);
941d48922cSDon Zickus 
setup_unknown_nmi_panic(char * str)951d48922cSDon Zickus static int __init setup_unknown_nmi_panic(char *str)
961d48922cSDon Zickus {
971d48922cSDon Zickus 	unknown_nmi_panic = 1;
981d48922cSDon Zickus 	return 1;
991d48922cSDon Zickus }
1001d48922cSDon Zickus __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
1011d48922cSDon Zickus 
102c9126b2eSDon Zickus #define nmi_to_desc(type) (&nmi_desc[type])
103c9126b2eSDon Zickus 
1042ab00456SDave Hansen static u64 nmi_longest_ns = 1 * NSEC_PER_MSEC;
105e90c7853SPeter Zijlstra 
nmi_warning_debugfs(void)1062ab00456SDave Hansen static int __init nmi_warning_debugfs(void)
1072ab00456SDave Hansen {
1082ab00456SDave Hansen 	debugfs_create_u64("nmi_longest_ns", 0644,
1092ab00456SDave Hansen 			arch_debugfs_dir, &nmi_longest_ns);
1102ab00456SDave Hansen 	return 0;
1112ab00456SDave Hansen }
1122ab00456SDave Hansen fs_initcall(nmi_warning_debugfs);
1132ab00456SDave Hansen 
nmi_check_duration(struct nmiaction * action,u64 duration)114248ed510SChangbin Du static void nmi_check_duration(struct nmiaction *action, u64 duration)
115e90c7853SPeter Zijlstra {
116e90c7853SPeter Zijlstra 	int remainder_ns, decimal_msecs;
117248ed510SChangbin Du 
118248ed510SChangbin Du 	if (duration < nmi_longest_ns || duration < action->max_duration)
119248ed510SChangbin Du 		return;
120248ed510SChangbin Du 
121248ed510SChangbin Du 	action->max_duration = duration;
122e90c7853SPeter Zijlstra 
123f94c91f7SLibing Zhou 	remainder_ns = do_div(duration, (1000 * 1000));
124e90c7853SPeter Zijlstra 	decimal_msecs = remainder_ns / 1000;
125e90c7853SPeter Zijlstra 
126e90c7853SPeter Zijlstra 	printk_ratelimited(KERN_INFO
127e90c7853SPeter Zijlstra 		"INFO: NMI handler (%ps) took too long to run: %lld.%03d msecs\n",
128f94c91f7SLibing Zhou 		action->handler, duration, decimal_msecs);
129e90c7853SPeter Zijlstra }
130e90c7853SPeter Zijlstra 
nmi_handle(unsigned int type,struct pt_regs * regs)131bf9f2ee2SAndy Lutomirski static int nmi_handle(unsigned int type, struct pt_regs *regs)
132c9126b2eSDon Zickus {
133c9126b2eSDon Zickus 	struct nmi_desc *desc = nmi_to_desc(type);
134c9126b2eSDon Zickus 	struct nmiaction *a;
135c9126b2eSDon Zickus 	int handled=0;
136c9126b2eSDon Zickus 
137c9126b2eSDon Zickus 	rcu_read_lock();
138c9126b2eSDon Zickus 
139c9126b2eSDon Zickus 	/*
140c9126b2eSDon Zickus 	 * NMIs are edge-triggered, which means if you have enough
141c9126b2eSDon Zickus 	 * of them concurrently, you can lose some because only one
142c9126b2eSDon Zickus 	 * can be latched at any given time.  Walk the whole list
143c9126b2eSDon Zickus 	 * to handle those situations.
144c9126b2eSDon Zickus 	 */
1452ab00456SDave Hansen 	list_for_each_entry_rcu(a, &desc->head, list) {
146e90c7853SPeter Zijlstra 		int thishandled;
147e90c7853SPeter Zijlstra 		u64 delta;
1482ab00456SDave Hansen 
149e90c7853SPeter Zijlstra 		delta = sched_clock();
1500c4df02dSDave Hansen 		thishandled = a->handler(type, regs);
1510c4df02dSDave Hansen 		handled += thishandled;
152e90c7853SPeter Zijlstra 		delta = sched_clock() - delta;
1530c4df02dSDave Hansen 		trace_nmi_handler(a->handler, (int)delta, thishandled);
1542ab00456SDave Hansen 
155248ed510SChangbin Du 		nmi_check_duration(a, delta);
1562ab00456SDave Hansen 	}
157c9126b2eSDon Zickus 
158c9126b2eSDon Zickus 	rcu_read_unlock();
159c9126b2eSDon Zickus 
160c9126b2eSDon Zickus 	/* return total number of NMI events handled */
161c9126b2eSDon Zickus 	return handled;
162c9126b2eSDon Zickus }
1639326638cSMasami Hiramatsu NOKPROBE_SYMBOL(nmi_handle);
164c9126b2eSDon Zickus 
__register_nmi_handler(unsigned int type,struct nmiaction * action)16572b3fb24SLi Zhong int __register_nmi_handler(unsigned int type, struct nmiaction *action)
166c9126b2eSDon Zickus {
167c9126b2eSDon Zickus 	struct nmi_desc *desc = nmi_to_desc(type);
168c9126b2eSDon Zickus 	unsigned long flags;
169c9126b2eSDon Zickus 
170a7fed5c0SThomas Gleixner 	if (WARN_ON_ONCE(!action->handler || !list_empty(&action->list)))
17172b3fb24SLi Zhong 		return -EINVAL;
17272b3fb24SLi Zhong 
173c455fd92SScott Wood 	raw_spin_lock_irqsave(&desc->lock, flags);
174c9126b2eSDon Zickus 
175c9126b2eSDon Zickus 	/*
1760d443b70SMike Travis 	 * Indicate if there are multiple registrations on the
1770d443b70SMike Travis 	 * internal NMI handler call chains (SERR and IO_CHECK).
178b227e233SDon Zickus 	 */
179553222f3SDon Zickus 	WARN_ON_ONCE(type == NMI_SERR && !list_empty(&desc->head));
180553222f3SDon Zickus 	WARN_ON_ONCE(type == NMI_IO_CHECK && !list_empty(&desc->head));
181b227e233SDon Zickus 
182b227e233SDon Zickus 	/*
183c9126b2eSDon Zickus 	 * some handlers need to be executed first otherwise a fake
184c9126b2eSDon Zickus 	 * event confuses some handlers (kdump uses this flag)
185c9126b2eSDon Zickus 	 */
186c9126b2eSDon Zickus 	if (action->flags & NMI_FLAG_FIRST)
187c9126b2eSDon Zickus 		list_add_rcu(&action->list, &desc->head);
188c9126b2eSDon Zickus 	else
189c9126b2eSDon Zickus 		list_add_tail_rcu(&action->list, &desc->head);
190c9126b2eSDon Zickus 
191c455fd92SScott Wood 	raw_spin_unlock_irqrestore(&desc->lock, flags);
192c9126b2eSDon Zickus 	return 0;
193c9126b2eSDon Zickus }
19472b3fb24SLi Zhong EXPORT_SYMBOL(__register_nmi_handler);
195c9126b2eSDon Zickus 
unregister_nmi_handler(unsigned int type,const char * name)19672b3fb24SLi Zhong void unregister_nmi_handler(unsigned int type, const char *name)
197c9126b2eSDon Zickus {
198c9126b2eSDon Zickus 	struct nmi_desc *desc = nmi_to_desc(type);
199a7fed5c0SThomas Gleixner 	struct nmiaction *n, *found = NULL;
200c9126b2eSDon Zickus 	unsigned long flags;
201c9126b2eSDon Zickus 
202c455fd92SScott Wood 	raw_spin_lock_irqsave(&desc->lock, flags);
203c9126b2eSDon Zickus 
204c9126b2eSDon Zickus 	list_for_each_entry_rcu(n, &desc->head, list) {
205c9126b2eSDon Zickus 		/*
206c9126b2eSDon Zickus 		 * the name passed in to describe the nmi handler
207c9126b2eSDon Zickus 		 * is used as the lookup key
208c9126b2eSDon Zickus 		 */
209c9126b2eSDon Zickus 		if (!strcmp(n->name, name)) {
210c9126b2eSDon Zickus 			WARN(in_nmi(),
211c9126b2eSDon Zickus 				"Trying to free NMI (%s) from NMI context!\n", n->name);
212c9126b2eSDon Zickus 			list_del_rcu(&n->list);
213a7fed5c0SThomas Gleixner 			found = n;
214c9126b2eSDon Zickus 			break;
215c9126b2eSDon Zickus 		}
216c9126b2eSDon Zickus 	}
217c9126b2eSDon Zickus 
218c455fd92SScott Wood 	raw_spin_unlock_irqrestore(&desc->lock, flags);
219a7fed5c0SThomas Gleixner 	if (found) {
220c9126b2eSDon Zickus 		synchronize_rcu();
221a7fed5c0SThomas Gleixner 		INIT_LIST_HEAD(&found->list);
222a7fed5c0SThomas Gleixner 	}
223c9126b2eSDon Zickus }
224c9126b2eSDon Zickus EXPORT_SYMBOL_GPL(unregister_nmi_handler);
225c9126b2eSDon Zickus 
2269326638cSMasami Hiramatsu static void
pci_serr_error(unsigned char reason,struct pt_regs * regs)2271d48922cSDon Zickus pci_serr_error(unsigned char reason, struct pt_regs *regs)
2281d48922cSDon Zickus {
229553222f3SDon Zickus 	/* check to see if anyone registered against these types of errors */
230bf9f2ee2SAndy Lutomirski 	if (nmi_handle(NMI_SERR, regs))
231553222f3SDon Zickus 		return;
232553222f3SDon Zickus 
2331d48922cSDon Zickus 	pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
2341d48922cSDon Zickus 		 reason, smp_processor_id());
2351d48922cSDon Zickus 
2361d48922cSDon Zickus 	if (panic_on_unrecovered_nmi)
23758c5661fSHidehiro Kawai 		nmi_panic(regs, "NMI: Not continuing");
2381d48922cSDon Zickus 
2391d48922cSDon Zickus 	pr_emerg("Dazed and confused, but trying to continue\n");
2401d48922cSDon Zickus 
2411d48922cSDon Zickus 	/* Clear and disable the PCI SERR error line. */
2421d48922cSDon Zickus 	reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_SERR;
2431d48922cSDon Zickus 	outb(reason, NMI_REASON_PORT);
2441d48922cSDon Zickus }
2459326638cSMasami Hiramatsu NOKPROBE_SYMBOL(pci_serr_error);
2461d48922cSDon Zickus 
2479326638cSMasami Hiramatsu static void
io_check_error(unsigned char reason,struct pt_regs * regs)2481d48922cSDon Zickus io_check_error(unsigned char reason, struct pt_regs *regs)
2491d48922cSDon Zickus {
2501d48922cSDon Zickus 	unsigned long i;
2511d48922cSDon Zickus 
252553222f3SDon Zickus 	/* check to see if anyone registered against these types of errors */
253bf9f2ee2SAndy Lutomirski 	if (nmi_handle(NMI_IO_CHECK, regs))
254553222f3SDon Zickus 		return;
255553222f3SDon Zickus 
2561d48922cSDon Zickus 	pr_emerg(
2571d48922cSDon Zickus 	"NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
2581d48922cSDon Zickus 		 reason, smp_processor_id());
25957da8b96SJan Beulich 	show_regs(regs);
2601d48922cSDon Zickus 
2611717f209SHidehiro Kawai 	if (panic_on_io_nmi) {
26258c5661fSHidehiro Kawai 		nmi_panic(regs, "NMI IOCK error: Not continuing");
2631717f209SHidehiro Kawai 
2641717f209SHidehiro Kawai 		/*
2651717f209SHidehiro Kawai 		 * If we end up here, it means we have received an NMI while
2661717f209SHidehiro Kawai 		 * processing panic(). Simply return without delaying and
2671717f209SHidehiro Kawai 		 * re-enabling NMIs.
2681717f209SHidehiro Kawai 		 */
2691717f209SHidehiro Kawai 		return;
2701717f209SHidehiro Kawai 	}
2711d48922cSDon Zickus 
2721d48922cSDon Zickus 	/* Re-enable the IOCK line, wait for a few seconds */
2731d48922cSDon Zickus 	reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
2741d48922cSDon Zickus 	outb(reason, NMI_REASON_PORT);
2751d48922cSDon Zickus 
2761d48922cSDon Zickus 	i = 20000;
2771d48922cSDon Zickus 	while (--i) {
2781d48922cSDon Zickus 		touch_nmi_watchdog();
2791d48922cSDon Zickus 		udelay(100);
2801d48922cSDon Zickus 	}
2811d48922cSDon Zickus 
2821d48922cSDon Zickus 	reason &= ~NMI_REASON_CLEAR_IOCHK;
2831d48922cSDon Zickus 	outb(reason, NMI_REASON_PORT);
2841d48922cSDon Zickus }
2859326638cSMasami Hiramatsu NOKPROBE_SYMBOL(io_check_error);
2861d48922cSDon Zickus 
2879326638cSMasami Hiramatsu static void
unknown_nmi_error(unsigned char reason,struct pt_regs * regs)2881d48922cSDon Zickus unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
2891d48922cSDon Zickus {
2909c48f1c6SDon Zickus 	int handled;
2919c48f1c6SDon Zickus 
292b227e233SDon Zickus 	/*
293b227e233SDon Zickus 	 * Use 'false' as back-to-back NMIs are dealt with one level up.
294b227e233SDon Zickus 	 * Of course this makes having multiple 'unknown' handlers useless
295b227e233SDon Zickus 	 * as only the first one is ever run (unless it can actually determine
296b227e233SDon Zickus 	 * if it caused the NMI)
297b227e233SDon Zickus 	 */
298bf9f2ee2SAndy Lutomirski 	handled = nmi_handle(NMI_UNKNOWN, regs);
299efc3aac5SDon Zickus 	if (handled) {
300efc3aac5SDon Zickus 		__this_cpu_add(nmi_stats.unknown, handled);
3011d48922cSDon Zickus 		return;
302efc3aac5SDon Zickus 	}
303efc3aac5SDon Zickus 
304efc3aac5SDon Zickus 	__this_cpu_add(nmi_stats.unknown, 1);
305efc3aac5SDon Zickus 
3061d48922cSDon Zickus 	pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
3071d48922cSDon Zickus 		 reason, smp_processor_id());
3081d48922cSDon Zickus 
3091d48922cSDon Zickus 	if (unknown_nmi_panic || panic_on_unrecovered_nmi)
31058c5661fSHidehiro Kawai 		nmi_panic(regs, "NMI: Not continuing");
3111d48922cSDon Zickus 
3121d48922cSDon Zickus 	pr_emerg("Dazed and confused, but trying to continue\n");
3131d48922cSDon Zickus }
3149326638cSMasami Hiramatsu NOKPROBE_SYMBOL(unknown_nmi_error);
3151d48922cSDon Zickus 
316b227e233SDon Zickus static DEFINE_PER_CPU(bool, swallow_nmi);
317b227e233SDon Zickus static DEFINE_PER_CPU(unsigned long, last_nmi_rip);
318b227e233SDon Zickus 
default_do_nmi(struct pt_regs * regs)319f051f697SThomas Gleixner static noinstr void default_do_nmi(struct pt_regs *regs)
3201d48922cSDon Zickus {
3211d48922cSDon Zickus 	unsigned char reason = 0;
3229c48f1c6SDon Zickus 	int handled;
323b227e233SDon Zickus 	bool b2b = false;
3241d48922cSDon Zickus 
3251d48922cSDon Zickus 	/*
3261d48922cSDon Zickus 	 * CPU-specific NMI must be processed before non-CPU-specific
3271d48922cSDon Zickus 	 * NMI, otherwise we may lose it, because the CPU-specific
3281d48922cSDon Zickus 	 * NMI can not be detected/processed on other CPUs.
3291d48922cSDon Zickus 	 */
330b227e233SDon Zickus 
331b227e233SDon Zickus 	/*
332b227e233SDon Zickus 	 * Back-to-back NMIs are interesting because they can either
333b227e233SDon Zickus 	 * be two NMI or more than two NMIs (any thing over two is dropped
334b227e233SDon Zickus 	 * due to NMI being edge-triggered).  If this is the second half
335b227e233SDon Zickus 	 * of the back-to-back NMI, assume we dropped things and process
336b227e233SDon Zickus 	 * more handlers.  Otherwise reset the 'swallow' NMI behaviour
337b227e233SDon Zickus 	 */
338b227e233SDon Zickus 	if (regs->ip == __this_cpu_read(last_nmi_rip))
339b227e233SDon Zickus 		b2b = true;
340b227e233SDon Zickus 	else
341b227e233SDon Zickus 		__this_cpu_write(swallow_nmi, false);
342b227e233SDon Zickus 
343b227e233SDon Zickus 	__this_cpu_write(last_nmi_rip, regs->ip);
344b227e233SDon Zickus 
345f051f697SThomas Gleixner 	instrumentation_begin();
346f051f697SThomas Gleixner 
34752b5dd84SThomas Gleixner 	if (microcode_nmi_handler_enabled() && microcode_nmi_handler())
34852b5dd84SThomas Gleixner 		goto out;
34952b5dd84SThomas Gleixner 
350bf9f2ee2SAndy Lutomirski 	handled = nmi_handle(NMI_LOCAL, regs);
351efc3aac5SDon Zickus 	__this_cpu_add(nmi_stats.normal, handled);
352b227e233SDon Zickus 	if (handled) {
353b227e233SDon Zickus 		/*
354b227e233SDon Zickus 		 * There are cases when a NMI handler handles multiple
355b227e233SDon Zickus 		 * events in the current NMI.  One of these events may
356b227e233SDon Zickus 		 * be queued for in the next NMI.  Because the event is
357b227e233SDon Zickus 		 * already handled, the next NMI will result in an unknown
358b227e233SDon Zickus 		 * NMI.  Instead lets flag this for a potential NMI to
359b227e233SDon Zickus 		 * swallow.
360b227e233SDon Zickus 		 */
361b227e233SDon Zickus 		if (handled > 1)
362b227e233SDon Zickus 			__this_cpu_write(swallow_nmi, true);
363f051f697SThomas Gleixner 		goto out;
364b227e233SDon Zickus 	}
3651d48922cSDon Zickus 
366b279d67dSHidehiro Kawai 	/*
367b279d67dSHidehiro Kawai 	 * Non-CPU-specific NMI: NMI sources can be processed on any CPU.
368b279d67dSHidehiro Kawai 	 *
369b279d67dSHidehiro Kawai 	 * Another CPU may be processing panic routines while holding
370b279d67dSHidehiro Kawai 	 * nmi_reason_lock. Check if the CPU issued the IPI for crash dumping,
371b279d67dSHidehiro Kawai 	 * and if so, call its callback directly.  If there is no CPU preparing
372b279d67dSHidehiro Kawai 	 * crash dump, we simply loop here.
373b279d67dSHidehiro Kawai 	 */
374b279d67dSHidehiro Kawai 	while (!raw_spin_trylock(&nmi_reason_lock)) {
375b279d67dSHidehiro Kawai 		run_crash_ipi_callback(regs);
376b279d67dSHidehiro Kawai 		cpu_relax();
377b279d67dSHidehiro Kawai 	}
378b279d67dSHidehiro Kawai 
379064a59b6SJacob Pan 	reason = x86_platform.get_nmi_reason();
3801d48922cSDon Zickus 
3811d48922cSDon Zickus 	if (reason & NMI_REASON_MASK) {
3821d48922cSDon Zickus 		if (reason & NMI_REASON_SERR)
3831d48922cSDon Zickus 			pci_serr_error(reason, regs);
3841d48922cSDon Zickus 		else if (reason & NMI_REASON_IOCHK)
3851d48922cSDon Zickus 			io_check_error(reason, regs);
3861d48922cSDon Zickus #ifdef CONFIG_X86_32
3871d48922cSDon Zickus 		/*
3881d48922cSDon Zickus 		 * Reassert NMI in case it became active
3891d48922cSDon Zickus 		 * meanwhile as it's edge-triggered:
3901d48922cSDon Zickus 		 */
3911d48922cSDon Zickus 		reassert_nmi();
3921d48922cSDon Zickus #endif
393efc3aac5SDon Zickus 		__this_cpu_add(nmi_stats.external, 1);
3941d48922cSDon Zickus 		raw_spin_unlock(&nmi_reason_lock);
395f051f697SThomas Gleixner 		goto out;
3961d48922cSDon Zickus 	}
3971d48922cSDon Zickus 	raw_spin_unlock(&nmi_reason_lock);
3981d48922cSDon Zickus 
399b227e233SDon Zickus 	/*
400b227e233SDon Zickus 	 * Only one NMI can be latched at a time.  To handle
401b227e233SDon Zickus 	 * this we may process multiple nmi handlers at once to
402b227e233SDon Zickus 	 * cover the case where an NMI is dropped.  The downside
403b227e233SDon Zickus 	 * to this approach is we may process an NMI prematurely,
404b227e233SDon Zickus 	 * while its real NMI is sitting latched.  This will cause
405b227e233SDon Zickus 	 * an unknown NMI on the next run of the NMI processing.
406b227e233SDon Zickus 	 *
407b227e233SDon Zickus 	 * We tried to flag that condition above, by setting the
408b227e233SDon Zickus 	 * swallow_nmi flag when we process more than one event.
409b227e233SDon Zickus 	 * This condition is also only present on the second half
410b227e233SDon Zickus 	 * of a back-to-back NMI, so we flag that condition too.
411b227e233SDon Zickus 	 *
412b227e233SDon Zickus 	 * If both are true, we assume we already processed this
413b227e233SDon Zickus 	 * NMI previously and we swallow it.  Otherwise we reset
414b227e233SDon Zickus 	 * the logic.
415b227e233SDon Zickus 	 *
416b227e233SDon Zickus 	 * There are scenarios where we may accidentally swallow
417b227e233SDon Zickus 	 * a 'real' unknown NMI.  For example, while processing
418b227e233SDon Zickus 	 * a perf NMI another perf NMI comes in along with a
419b227e233SDon Zickus 	 * 'real' unknown NMI.  These two NMIs get combined into
4204d1d0977SMartin Molnar 	 * one (as described above).  When the next NMI gets
421b227e233SDon Zickus 	 * processed, it will be flagged by perf as handled, but
422b227e233SDon Zickus 	 * no one will know that there was a 'real' unknown NMI sent
423b227e233SDon Zickus 	 * also.  As a result it gets swallowed.  Or if the first
424b227e233SDon Zickus 	 * perf NMI returns two events handled then the second
425b227e233SDon Zickus 	 * NMI will get eaten by the logic below, again losing a
426b227e233SDon Zickus 	 * 'real' unknown NMI.  But this is the best we can do
427b227e233SDon Zickus 	 * for now.
428b227e233SDon Zickus 	 */
429b227e233SDon Zickus 	if (b2b && __this_cpu_read(swallow_nmi))
430efc3aac5SDon Zickus 		__this_cpu_add(nmi_stats.swallow, 1);
431b227e233SDon Zickus 	else
4321d48922cSDon Zickus 		unknown_nmi_error(reason, regs);
433f051f697SThomas Gleixner 
434f051f697SThomas Gleixner out:
435f051f697SThomas Gleixner 	instrumentation_end();
4361d48922cSDon Zickus }
4371d48922cSDon Zickus 
438ccd49c23SSteven Rostedt /*
4390b22930eSAndy Lutomirski  * NMIs can page fault or hit breakpoints which will cause it to lose
4400b22930eSAndy Lutomirski  * its NMI context with the CPU when the breakpoint or page fault does an IRET.
4419d050416SAndy Lutomirski  *
4429d050416SAndy Lutomirski  * As a result, NMIs can nest if NMIs get unmasked due an IRET during
4439d050416SAndy Lutomirski  * NMI processing.  On x86_64, the asm glue protects us from nested NMIs
4449d050416SAndy Lutomirski  * if the outer NMI came from kernel mode, but we can still nest if the
4459d050416SAndy Lutomirski  * outer NMI came from user mode.
4469d050416SAndy Lutomirski  *
4479d050416SAndy Lutomirski  * To handle these nested NMIs, we have three states:
448ccd49c23SSteven Rostedt  *
449ccd49c23SSteven Rostedt  *  1) not running
450ccd49c23SSteven Rostedt  *  2) executing
451ccd49c23SSteven Rostedt  *  3) latched
452ccd49c23SSteven Rostedt  *
453ccd49c23SSteven Rostedt  * When no NMI is in progress, it is in the "not running" state.
454ccd49c23SSteven Rostedt  * When an NMI comes in, it goes into the "executing" state.
455ccd49c23SSteven Rostedt  * Normally, if another NMI is triggered, it does not interrupt
456ccd49c23SSteven Rostedt  * the running NMI and the HW will simply latch it so that when
457ccd49c23SSteven Rostedt  * the first NMI finishes, it will restart the second NMI.
458ccd49c23SSteven Rostedt  * (Note, the latch is binary, thus multiple NMIs triggering,
459ccd49c23SSteven Rostedt  *  when one is running, are ignored. Only one NMI is restarted.)
460ccd49c23SSteven Rostedt  *
4619d050416SAndy Lutomirski  * If an NMI executes an iret, another NMI can preempt it. We do not
4629d050416SAndy Lutomirski  * want to allow this new NMI to run, but we want to execute it when the
4639d050416SAndy Lutomirski  * first one finishes.  We set the state to "latched", and the exit of
4649d050416SAndy Lutomirski  * the first NMI will perform a dec_return, if the result is zero
4659d050416SAndy Lutomirski  * (NOT_RUNNING), then it will simply exit the NMI handler. If not, the
4669d050416SAndy Lutomirski  * dec_return would have set the state to NMI_EXECUTING (what we want it
4679d050416SAndy Lutomirski  * to be when we are running). In this case, we simply jump back to
4689d050416SAndy Lutomirski  * rerun the NMI handler again, and restart the 'latched' NMI.
469c7d65a78SSteven Rostedt  *
470c7d65a78SSteven Rostedt  * No trap (breakpoint or page fault) should be hit before nmi_restart,
471c7d65a78SSteven Rostedt  * thus there is no race between the first check of state for NOT_RUNNING
472c7d65a78SSteven Rostedt  * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs
473c7d65a78SSteven Rostedt  * at this point.
47470fb74a5SSteven Rostedt  *
47570fb74a5SSteven Rostedt  * In case the NMI takes a page fault, we need to save off the CR2
47670fb74a5SSteven Rostedt  * because the NMI could have preempted another page fault and corrupt
47770fb74a5SSteven Rostedt  * the CR2 that is about to be read. As nested NMIs must be restarted
47870fb74a5SSteven Rostedt  * and they can not take breakpoints or page faults, the update of the
47970fb74a5SSteven Rostedt  * CR2 must be done before converting the nmi state back to NOT_RUNNING.
48070fb74a5SSteven Rostedt  * Otherwise, there would be a race of another nested NMI coming in
48170fb74a5SSteven Rostedt  * after setting state to NOT_RUNNING but before updating the nmi_cr2.
482ccd49c23SSteven Rostedt  */
483ccd49c23SSteven Rostedt enum nmi_states {
484c7d65a78SSteven Rostedt 	NMI_NOT_RUNNING = 0,
485ccd49c23SSteven Rostedt 	NMI_EXECUTING,
486ccd49c23SSteven Rostedt 	NMI_LATCHED,
487ccd49c23SSteven Rostedt };
488ccd49c23SSteven Rostedt static DEFINE_PER_CPU(enum nmi_states, nmi_state);
48970fb74a5SSteven Rostedt static DEFINE_PER_CPU(unsigned long, nmi_cr2);
490fd338e35SPeter Zijlstra static DEFINE_PER_CPU(unsigned long, nmi_dr7);
491ccd49c23SSteven Rostedt 
DEFINE_IDTENTRY_RAW(exc_nmi)49271ed49d8SThomas Gleixner DEFINE_IDTENTRY_RAW(exc_nmi)
493ccd49c23SSteven Rostedt {
494b6be002bSThomas Gleixner 	irqentry_state_t irq_state;
4951a3ea611SPaul E. McKenney 	struct nmi_stats *nsp = this_cpu_ptr(&nmi_stats);
496ba1f2b2eSPeter Zijlstra 
4974ca68e02SJoerg Roedel 	/*
4984ca68e02SJoerg Roedel 	 * Re-enable NMIs right here when running as an SEV-ES guest. This might
4994ca68e02SJoerg Roedel 	 * cause nested NMIs, but those can be handled safely.
5004ca68e02SJoerg Roedel 	 */
5014ca68e02SJoerg Roedel 	sev_es_nmi_complete();
5021a3ea611SPaul E. McKenney 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU))
5030f613bfaSMark Rutland 		raw_atomic_long_inc(&nsp->idt_calls);
5044ca68e02SJoerg Roedel 
505*287a86b4SThomas Gleixner 	if (IS_ENABLED(CONFIG_SMP) && arch_cpu_is_offline(smp_processor_id())) {
506*287a86b4SThomas Gleixner 		if (microcode_nmi_handler_enabled())
507*287a86b4SThomas Gleixner 			microcode_offline_nmi_handler();
50860dcaad5SThomas Gleixner 		return;
509*287a86b4SThomas Gleixner 	}
51060dcaad5SThomas Gleixner 
5119d050416SAndy Lutomirski 	if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) {
5129d050416SAndy Lutomirski 		this_cpu_write(nmi_state, NMI_LATCHED);
5139d050416SAndy Lutomirski 		return;
5149d050416SAndy Lutomirski 	}
5159d050416SAndy Lutomirski 	this_cpu_write(nmi_state, NMI_EXECUTING);
5169d050416SAndy Lutomirski 	this_cpu_write(nmi_cr2, read_cr2());
5172112a9dfSPaul E. McKenney 
5182112a9dfSPaul E. McKenney nmi_restart:
5191a3ea611SPaul E. McKenney 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
5201a3ea611SPaul E. McKenney 		WRITE_ONCE(nsp->idt_seq, nsp->idt_seq + 1);
5211a3ea611SPaul E. McKenney 		WARN_ON_ONCE(!(nsp->idt_seq & 0x1));
5221a3ea611SPaul E. McKenney 		WRITE_ONCE(nsp->recv_jiffies, jiffies);
5231a3ea611SPaul E. McKenney 	}
5249d050416SAndy Lutomirski 
525315562c9SJoerg Roedel 	/*
526315562c9SJoerg Roedel 	 * Needs to happen before DR7 is accessed, because the hypervisor can
527315562c9SJoerg Roedel 	 * intercept DR7 reads/writes, turning those into #VC exceptions.
528315562c9SJoerg Roedel 	 */
529315562c9SJoerg Roedel 	sev_es_ist_enter(regs);
530315562c9SJoerg Roedel 
531fd338e35SPeter Zijlstra 	this_cpu_write(nmi_dr7, local_db_save());
532ccd49c23SSteven Rostedt 
533b6be002bSThomas Gleixner 	irq_state = irqentry_nmi_enter(regs);
5341d48922cSDon Zickus 
5351d48922cSDon Zickus 	inc_irq_stat(__nmi_count);
5361d48922cSDon Zickus 
5371a3ea611SPaul E. McKenney 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU) && ignore_nmis) {
5381a3ea611SPaul E. McKenney 		WRITE_ONCE(nsp->idt_ignored, nsp->idt_ignored + 1);
5391a3ea611SPaul E. McKenney 	} else if (!ignore_nmis) {
5401a3ea611SPaul E. McKenney 		if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
5411a3ea611SPaul E. McKenney 			WRITE_ONCE(nsp->idt_nmi_seq, nsp->idt_nmi_seq + 1);
5421a3ea611SPaul E. McKenney 			WARN_ON_ONCE(!(nsp->idt_nmi_seq & 0x1));
5431a3ea611SPaul E. McKenney 		}
5441d48922cSDon Zickus 		default_do_nmi(regs);
5451a3ea611SPaul E. McKenney 		if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
5461a3ea611SPaul E. McKenney 			WRITE_ONCE(nsp->idt_nmi_seq, nsp->idt_nmi_seq + 1);
5471a3ea611SPaul E. McKenney 			WARN_ON_ONCE(nsp->idt_nmi_seq & 0x1);
5481a3ea611SPaul E. McKenney 		}
5491a3ea611SPaul E. McKenney 	}
5501d48922cSDon Zickus 
551b6be002bSThomas Gleixner 	irqentry_nmi_exit(regs, irq_state);
552228bdaa9SSteven Rostedt 
553fd338e35SPeter Zijlstra 	local_db_restore(this_cpu_read(nmi_dr7));
5549d050416SAndy Lutomirski 
555315562c9SJoerg Roedel 	sev_es_ist_exit();
556315562c9SJoerg Roedel 
5579d050416SAndy Lutomirski 	if (unlikely(this_cpu_read(nmi_cr2) != read_cr2()))
5589d050416SAndy Lutomirski 		write_cr2(this_cpu_read(nmi_cr2));
5591a3ea611SPaul E. McKenney 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
5601a3ea611SPaul E. McKenney 		WRITE_ONCE(nsp->idt_seq, nsp->idt_seq + 1);
5611a3ea611SPaul E. McKenney 		WARN_ON_ONCE(nsp->idt_seq & 0x1);
5621a3ea611SPaul E. McKenney 		WRITE_ONCE(nsp->recv_jiffies, jiffies);
5631a3ea611SPaul E. McKenney 	}
5642112a9dfSPaul E. McKenney 	if (this_cpu_dec_return(nmi_state))
5652112a9dfSPaul E. McKenney 		goto nmi_restart;
5661d48922cSDon Zickus }
5671d48922cSDon Zickus 
56854a3b70aSSean Christopherson #if IS_ENABLED(CONFIG_KVM_INTEL)
DEFINE_IDTENTRY_RAW(exc_nmi_kvm_vmx)56954a3b70aSSean Christopherson DEFINE_IDTENTRY_RAW(exc_nmi_kvm_vmx)
570a217a659SLai Jiangshan {
571a217a659SLai Jiangshan 	exc_nmi(regs);
572a217a659SLai Jiangshan }
573a217a659SLai Jiangshan #if IS_MODULE(CONFIG_KVM_INTEL)
57454a3b70aSSean Christopherson EXPORT_SYMBOL_GPL(asm_exc_nmi_kvm_vmx);
57554a3b70aSSean Christopherson #endif
576a217a659SLai Jiangshan #endif
577a217a659SLai Jiangshan 
578344da544SPaul E. McKenney #ifdef CONFIG_NMI_CHECK_CPU
579344da544SPaul E. McKenney 
580344da544SPaul E. McKenney static char *nmi_check_stall_msg[] = {
581344da544SPaul E. McKenney /*									*/
582344da544SPaul E. McKenney /* +--------- nsp->idt_seq_snap & 0x1: CPU is in NMI handler.		*/
583344da544SPaul E. McKenney /* | +------ cpu_is_offline(cpu)					*/
584344da544SPaul E. McKenney /* | | +--- nsp->idt_calls_snap != atomic_long_read(&nsp->idt_calls):	*/
585344da544SPaul E. McKenney /* | | |	NMI handler has been invoked.				*/
586344da544SPaul E. McKenney /* | | |								*/
587344da544SPaul E. McKenney /* V V V								*/
588344da544SPaul E. McKenney /* 0 0 0 */ "NMIs are not reaching exc_nmi() handler",
589344da544SPaul E. McKenney /* 0 0 1 */ "exc_nmi() handler is ignoring NMIs",
590344da544SPaul E. McKenney /* 0 1 0 */ "CPU is offline and NMIs are not reaching exc_nmi() handler",
591344da544SPaul E. McKenney /* 0 1 1 */ "CPU is offline and exc_nmi() handler is legitimately ignoring NMIs",
592344da544SPaul E. McKenney /* 1 0 0 */ "CPU is in exc_nmi() handler and no further NMIs are reaching handler",
593344da544SPaul E. McKenney /* 1 0 1 */ "CPU is in exc_nmi() handler which is legitimately ignoring NMIs",
594344da544SPaul E. McKenney /* 1 1 0 */ "CPU is offline in exc_nmi() handler and no more NMIs are reaching exc_nmi() handler",
595344da544SPaul E. McKenney /* 1 1 1 */ "CPU is offline in exc_nmi() handler which is legitimately ignoring NMIs",
596344da544SPaul E. McKenney };
597344da544SPaul E. McKenney 
nmi_backtrace_stall_snap(const struct cpumask * btp)598344da544SPaul E. McKenney void nmi_backtrace_stall_snap(const struct cpumask *btp)
599344da544SPaul E. McKenney {
600344da544SPaul E. McKenney 	int cpu;
601344da544SPaul E. McKenney 	struct nmi_stats *nsp;
602344da544SPaul E. McKenney 
603344da544SPaul E. McKenney 	for_each_cpu(cpu, btp) {
604344da544SPaul E. McKenney 		nsp = per_cpu_ptr(&nmi_stats, cpu);
605344da544SPaul E. McKenney 		nsp->idt_seq_snap = READ_ONCE(nsp->idt_seq);
606344da544SPaul E. McKenney 		nsp->idt_nmi_seq_snap = READ_ONCE(nsp->idt_nmi_seq);
607344da544SPaul E. McKenney 		nsp->idt_ignored_snap = READ_ONCE(nsp->idt_ignored);
608344da544SPaul E. McKenney 		nsp->idt_calls_snap = atomic_long_read(&nsp->idt_calls);
609344da544SPaul E. McKenney 	}
610344da544SPaul E. McKenney }
611344da544SPaul E. McKenney 
nmi_backtrace_stall_check(const struct cpumask * btp)612344da544SPaul E. McKenney void nmi_backtrace_stall_check(const struct cpumask *btp)
613344da544SPaul E. McKenney {
614344da544SPaul E. McKenney 	int cpu;
615344da544SPaul E. McKenney 	int idx;
616344da544SPaul E. McKenney 	unsigned long nmi_seq;
617344da544SPaul E. McKenney 	unsigned long j = jiffies;
618344da544SPaul E. McKenney 	char *modp;
619344da544SPaul E. McKenney 	char *msgp;
620344da544SPaul E. McKenney 	char *msghp;
621344da544SPaul E. McKenney 	struct nmi_stats *nsp;
622344da544SPaul E. McKenney 
623344da544SPaul E. McKenney 	for_each_cpu(cpu, btp) {
624344da544SPaul E. McKenney 		nsp = per_cpu_ptr(&nmi_stats, cpu);
625344da544SPaul E. McKenney 		modp = "";
626344da544SPaul E. McKenney 		msghp = "";
627344da544SPaul E. McKenney 		nmi_seq = READ_ONCE(nsp->idt_nmi_seq);
628344da544SPaul E. McKenney 		if (nsp->idt_nmi_seq_snap + 1 == nmi_seq && (nmi_seq & 0x1)) {
629344da544SPaul E. McKenney 			msgp = "CPU entered NMI handler function, but has not exited";
630344da544SPaul E. McKenney 		} else if ((nsp->idt_nmi_seq_snap & 0x1) != (nmi_seq & 0x1)) {
631344da544SPaul E. McKenney 			msgp = "CPU is handling NMIs";
632344da544SPaul E. McKenney 		} else {
633344da544SPaul E. McKenney 			idx = ((nsp->idt_seq_snap & 0x1) << 2) |
634344da544SPaul E. McKenney 			      (cpu_is_offline(cpu) << 1) |
635344da544SPaul E. McKenney 			      (nsp->idt_calls_snap != atomic_long_read(&nsp->idt_calls));
636344da544SPaul E. McKenney 			msgp = nmi_check_stall_msg[idx];
637344da544SPaul E. McKenney 			if (nsp->idt_ignored_snap != READ_ONCE(nsp->idt_ignored) && (idx & 0x1))
638344da544SPaul E. McKenney 				modp = ", but OK because ignore_nmis was set";
639f9f67e87SBreno Leitao 			if (nmi_seq & 0x1)
640344da544SPaul E. McKenney 				msghp = " (CPU currently in NMI handler function)";
641344da544SPaul E. McKenney 			else if (nsp->idt_nmi_seq_snap + 1 == nmi_seq)
642344da544SPaul E. McKenney 				msghp = " (CPU exited one NMI handler function)";
643344da544SPaul E. McKenney 		}
644344da544SPaul E. McKenney 		pr_alert("%s: CPU %d: %s%s%s, last activity: %lu jiffies ago.\n",
645344da544SPaul E. McKenney 			 __func__, cpu, msgp, modp, msghp, j - READ_ONCE(nsp->recv_jiffies));
646344da544SPaul E. McKenney 	}
647344da544SPaul E. McKenney }
648344da544SPaul E. McKenney 
649344da544SPaul E. McKenney #endif
650344da544SPaul E. McKenney 
stop_nmi(void)6511d48922cSDon Zickus void stop_nmi(void)
6521d48922cSDon Zickus {
6531d48922cSDon Zickus 	ignore_nmis++;
6541d48922cSDon Zickus }
6551d48922cSDon Zickus 
restart_nmi(void)6561d48922cSDon Zickus void restart_nmi(void)
6571d48922cSDon Zickus {
6581d48922cSDon Zickus 	ignore_nmis--;
6591d48922cSDon Zickus }
660b227e233SDon Zickus 
661b227e233SDon Zickus /* reset the back-to-back NMI logic */
local_touch_nmi(void)662b227e233SDon Zickus void local_touch_nmi(void)
663b227e233SDon Zickus {
664b227e233SDon Zickus 	__this_cpu_write(last_nmi_rip, 0);
665b227e233SDon Zickus }
66629c6fb7bSJacob Pan EXPORT_SYMBOL_GPL(local_touch_nmi);
667