nmi.c 21 KB

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