nmi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. * Copyright (C) 2011 Don Zickus Red Hat, Inc.
  5. *
  6. * Pentium III FXSR, SSE support
  7. * Gareth Hughes <gareth@valinux.com>, May 2000
  8. */
  9. /*
  10. * Handle hardware traps and faults.
  11. */
  12. #include <linux/spinlock.h>
  13. #include <linux/kprobes.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/sched/debug.h>
  16. #include <linux/nmi.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/delay.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/ratelimit.h>
  21. #include <linux/slab.h>
  22. #include <linux/export.h>
  23. #include <linux/sched/clock.h>
  24. #if defined(CONFIG_EDAC)
  25. #include <linux/edac.h>
  26. #endif
  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. #include <asm/cache.h>
  34. #include <asm/nospec-branch.h>
  35. #define CREATE_TRACE_POINTS
  36. #include <trace/events/nmi.h>
  37. struct nmi_desc {
  38. raw_spinlock_t lock;
  39. struct list_head head;
  40. };
  41. static struct nmi_desc nmi_desc[NMI_MAX] =
  42. {
  43. {
  44. .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
  45. .head = LIST_HEAD_INIT(nmi_desc[0].head),
  46. },
  47. {
  48. .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
  49. .head = LIST_HEAD_INIT(nmi_desc[1].head),
  50. },
  51. {
  52. .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[2].lock),
  53. .head = LIST_HEAD_INIT(nmi_desc[2].head),
  54. },
  55. {
  56. .lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[3].lock),
  57. .head = LIST_HEAD_INIT(nmi_desc[3].head),
  58. },
  59. };
  60. struct nmi_stats {
  61. unsigned int normal;
  62. unsigned int unknown;
  63. unsigned int external;
  64. unsigned int swallow;
  65. };
  66. static DEFINE_PER_CPU(struct nmi_stats, nmi_stats);
  67. static int ignore_nmis __read_mostly;
  68. int unknown_nmi_panic;
  69. /*
  70. * Prevent NMI reason port (0x61) being accessed simultaneously, can
  71. * only be used in NMI handler.
  72. */
  73. static DEFINE_RAW_SPINLOCK(nmi_reason_lock);
  74. static int __init setup_unknown_nmi_panic(char *str)
  75. {
  76. unknown_nmi_panic = 1;
  77. return 1;
  78. }
  79. __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
  80. #define nmi_to_desc(type) (&nmi_desc[type])
  81. static u64 nmi_longest_ns = 1 * NSEC_PER_MSEC;
  82. static int __init nmi_warning_debugfs(void)
  83. {
  84. debugfs_create_u64("nmi_longest_ns", 0644,
  85. arch_debugfs_dir, &nmi_longest_ns);
  86. return 0;
  87. }
  88. fs_initcall(nmi_warning_debugfs);
  89. static void nmi_check_duration(struct nmiaction *action, u64 duration)
  90. {
  91. int remainder_ns, decimal_msecs;
  92. if (duration < nmi_longest_ns || duration < action->max_duration)
  93. return;
  94. action->max_duration = duration;
  95. remainder_ns = do_div(duration, (1000 * 1000));
  96. decimal_msecs = remainder_ns / 1000;
  97. printk_ratelimited(KERN_INFO
  98. "INFO: NMI handler (%ps) took too long to run: %lld.%03d msecs\n",
  99. action->handler, duration, decimal_msecs);
  100. }
  101. static int nmi_handle(unsigned int type, struct pt_regs *regs)
  102. {
  103. struct nmi_desc *desc = nmi_to_desc(type);
  104. struct nmiaction *a;
  105. int handled=0;
  106. rcu_read_lock();
  107. /*
  108. * NMIs are edge-triggered, which means if you have enough
  109. * of them concurrently, you can lose some because only one
  110. * can be latched at any given time. Walk the whole list
  111. * to handle those situations.
  112. */
  113. list_for_each_entry_rcu(a, &desc->head, list) {
  114. int thishandled;
  115. u64 delta;
  116. delta = sched_clock();
  117. thishandled = a->handler(type, regs);
  118. handled += thishandled;
  119. delta = sched_clock() - delta;
  120. trace_nmi_handler(a->handler, (int)delta, thishandled);
  121. nmi_check_duration(a, delta);
  122. }
  123. rcu_read_unlock();
  124. /* return total number of NMI events handled */
  125. return handled;
  126. }
  127. NOKPROBE_SYMBOL(nmi_handle);
  128. int __register_nmi_handler(unsigned int type, struct nmiaction *action)
  129. {
  130. struct nmi_desc *desc = nmi_to_desc(type);
  131. unsigned long flags;
  132. if (!action->handler)
  133. return -EINVAL;
  134. raw_spin_lock_irqsave(&desc->lock, flags);
  135. /*
  136. * Indicate if there are multiple registrations on the
  137. * internal NMI handler call chains (SERR and IO_CHECK).
  138. */
  139. WARN_ON_ONCE(type == NMI_SERR && !list_empty(&desc->head));
  140. WARN_ON_ONCE(type == NMI_IO_CHECK && !list_empty(&desc->head));
  141. /*
  142. * some handlers need to be executed first otherwise a fake
  143. * event confuses some handlers (kdump uses this flag)
  144. */
  145. if (action->flags & NMI_FLAG_FIRST)
  146. list_add_rcu(&action->list, &desc->head);
  147. else
  148. list_add_tail_rcu(&action->list, &desc->head);
  149. raw_spin_unlock_irqrestore(&desc->lock, flags);
  150. return 0;
  151. }
  152. EXPORT_SYMBOL(__register_nmi_handler);
  153. void unregister_nmi_handler(unsigned int type, const char *name)
  154. {
  155. struct nmi_desc *desc = nmi_to_desc(type);
  156. struct nmiaction *n;
  157. unsigned long flags;
  158. raw_spin_lock_irqsave(&desc->lock, flags);
  159. list_for_each_entry_rcu(n, &desc->head, list) {
  160. /*
  161. * the name passed in to describe the nmi handler
  162. * is used as the lookup key
  163. */
  164. if (!strcmp(n->name, name)) {
  165. WARN(in_nmi(),
  166. "Trying to free NMI (%s) from NMI context!\n", n->name);
  167. list_del_rcu(&n->list);
  168. break;
  169. }
  170. }
  171. raw_spin_unlock_irqrestore(&desc->lock, flags);
  172. synchronize_rcu();
  173. }
  174. EXPORT_SYMBOL_GPL(unregister_nmi_handler);
  175. static void
  176. pci_serr_error(unsigned char reason, struct pt_regs *regs)
  177. {
  178. /* check to see if anyone registered against these types of errors */
  179. if (nmi_handle(NMI_SERR, regs))
  180. return;
  181. pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
  182. reason, smp_processor_id());
  183. if (panic_on_unrecovered_nmi)
  184. nmi_panic(regs, "NMI: Not continuing");
  185. pr_emerg("Dazed and confused, but trying to continue\n");
  186. /* Clear and disable the PCI SERR error line. */
  187. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_SERR;
  188. outb(reason, NMI_REASON_PORT);
  189. }
  190. NOKPROBE_SYMBOL(pci_serr_error);
  191. static void
  192. io_check_error(unsigned char reason, struct pt_regs *regs)
  193. {
  194. unsigned long i;
  195. /* check to see if anyone registered against these types of errors */
  196. if (nmi_handle(NMI_IO_CHECK, regs))
  197. return;
  198. pr_emerg(
  199. "NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
  200. reason, smp_processor_id());
  201. show_regs(regs);
  202. if (panic_on_io_nmi) {
  203. nmi_panic(regs, "NMI IOCK error: Not continuing");
  204. /*
  205. * If we end up here, it means we have received an NMI while
  206. * processing panic(). Simply return without delaying and
  207. * re-enabling NMIs.
  208. */
  209. return;
  210. }
  211. /* Re-enable the IOCK line, wait for a few seconds */
  212. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
  213. outb(reason, NMI_REASON_PORT);
  214. i = 20000;
  215. while (--i) {
  216. touch_nmi_watchdog();
  217. udelay(100);
  218. }
  219. reason &= ~NMI_REASON_CLEAR_IOCHK;
  220. outb(reason, NMI_REASON_PORT);
  221. }
  222. NOKPROBE_SYMBOL(io_check_error);
  223. static void
  224. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  225. {
  226. int handled;
  227. /*
  228. * Use 'false' as back-to-back NMIs are dealt with one level up.
  229. * Of course this makes having multiple 'unknown' handlers useless
  230. * as only the first one is ever run (unless it can actually determine
  231. * if it caused the NMI)
  232. */
  233. handled = nmi_handle(NMI_UNKNOWN, regs);
  234. if (handled) {
  235. __this_cpu_add(nmi_stats.unknown, handled);
  236. return;
  237. }
  238. __this_cpu_add(nmi_stats.unknown, 1);
  239. pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  240. reason, smp_processor_id());
  241. pr_emerg("Do you have a strange power saving mode enabled?\n");
  242. if (unknown_nmi_panic || panic_on_unrecovered_nmi)
  243. nmi_panic(regs, "NMI: Not continuing");
  244. pr_emerg("Dazed and confused, but trying to continue\n");
  245. }
  246. NOKPROBE_SYMBOL(unknown_nmi_error);
  247. static DEFINE_PER_CPU(bool, swallow_nmi);
  248. static DEFINE_PER_CPU(unsigned long, last_nmi_rip);
  249. static void default_do_nmi(struct pt_regs *regs)
  250. {
  251. unsigned char reason = 0;
  252. int handled;
  253. bool b2b = false;
  254. /*
  255. * CPU-specific NMI must be processed before non-CPU-specific
  256. * NMI, otherwise we may lose it, because the CPU-specific
  257. * NMI can not be detected/processed on other CPUs.
  258. */
  259. /*
  260. * Back-to-back NMIs are interesting because they can either
  261. * be two NMI or more than two NMIs (any thing over two is dropped
  262. * due to NMI being edge-triggered). If this is the second half
  263. * of the back-to-back NMI, assume we dropped things and process
  264. * more handlers. Otherwise reset the 'swallow' NMI behaviour
  265. */
  266. if (regs->ip == __this_cpu_read(last_nmi_rip))
  267. b2b = true;
  268. else
  269. __this_cpu_write(swallow_nmi, false);
  270. __this_cpu_write(last_nmi_rip, regs->ip);
  271. handled = nmi_handle(NMI_LOCAL, regs);
  272. __this_cpu_add(nmi_stats.normal, handled);
  273. if (handled) {
  274. /*
  275. * There are cases when a NMI handler handles multiple
  276. * events in the current NMI. One of these events may
  277. * be queued for in the next NMI. Because the event is
  278. * already handled, the next NMI will result in an unknown
  279. * NMI. Instead lets flag this for a potential NMI to
  280. * swallow.
  281. */
  282. if (handled > 1)
  283. __this_cpu_write(swallow_nmi, true);
  284. return;
  285. }
  286. /*
  287. * Non-CPU-specific NMI: NMI sources can be processed on any CPU.
  288. *
  289. * Another CPU may be processing panic routines while holding
  290. * nmi_reason_lock. Check if the CPU issued the IPI for crash dumping,
  291. * and if so, call its callback directly. If there is no CPU preparing
  292. * crash dump, we simply loop here.
  293. */
  294. while (!raw_spin_trylock(&nmi_reason_lock)) {
  295. run_crash_ipi_callback(regs);
  296. cpu_relax();
  297. }
  298. reason = x86_platform.get_nmi_reason();
  299. if (reason & NMI_REASON_MASK) {
  300. if (reason & NMI_REASON_SERR)
  301. pci_serr_error(reason, regs);
  302. else if (reason & NMI_REASON_IOCHK)
  303. io_check_error(reason, regs);
  304. #ifdef CONFIG_X86_32
  305. /*
  306. * Reassert NMI in case it became active
  307. * meanwhile as it's edge-triggered:
  308. */
  309. reassert_nmi();
  310. #endif
  311. __this_cpu_add(nmi_stats.external, 1);
  312. raw_spin_unlock(&nmi_reason_lock);
  313. return;
  314. }
  315. raw_spin_unlock(&nmi_reason_lock);
  316. /*
  317. * Only one NMI can be latched at a time. To handle
  318. * this we may process multiple nmi handlers at once to
  319. * cover the case where an NMI is dropped. The downside
  320. * to this approach is we may process an NMI prematurely,
  321. * while its real NMI is sitting latched. This will cause
  322. * an unknown NMI on the next run of the NMI processing.
  323. *
  324. * We tried to flag that condition above, by setting the
  325. * swallow_nmi flag when we process more than one event.
  326. * This condition is also only present on the second half
  327. * of a back-to-back NMI, so we flag that condition too.
  328. *
  329. * If both are true, we assume we already processed this
  330. * NMI previously and we swallow it. Otherwise we reset
  331. * the logic.
  332. *
  333. * There are scenarios where we may accidentally swallow
  334. * a 'real' unknown NMI. For example, while processing
  335. * a perf NMI another perf NMI comes in along with a
  336. * 'real' unknown NMI. These two NMIs get combined into
  337. * one (as descibed above). When the next NMI gets
  338. * processed, it will be flagged by perf as handled, but
  339. * noone will know that there was a 'real' unknown NMI sent
  340. * also. As a result it gets swallowed. Or if the first
  341. * perf NMI returns two events handled then the second
  342. * NMI will get eaten by the logic below, again losing a
  343. * 'real' unknown NMI. But this is the best we can do
  344. * for now.
  345. */
  346. if (b2b && __this_cpu_read(swallow_nmi))
  347. __this_cpu_add(nmi_stats.swallow, 1);
  348. else
  349. unknown_nmi_error(reason, regs);
  350. }
  351. NOKPROBE_SYMBOL(default_do_nmi);
  352. /*
  353. * NMIs can page fault or hit breakpoints which will cause it to lose
  354. * its NMI context with the CPU when the breakpoint or page fault does an IRET.
  355. *
  356. * As a result, NMIs can nest if NMIs get unmasked due an IRET during
  357. * NMI processing. On x86_64, the asm glue protects us from nested NMIs
  358. * if the outer NMI came from kernel mode, but we can still nest if the
  359. * outer NMI came from user mode.
  360. *
  361. * To handle these nested NMIs, we have three states:
  362. *
  363. * 1) not running
  364. * 2) executing
  365. * 3) latched
  366. *
  367. * When no NMI is in progress, it is in the "not running" state.
  368. * When an NMI comes in, it goes into the "executing" state.
  369. * Normally, if another NMI is triggered, it does not interrupt
  370. * the running NMI and the HW will simply latch it so that when
  371. * the first NMI finishes, it will restart the second NMI.
  372. * (Note, the latch is binary, thus multiple NMIs triggering,
  373. * when one is running, are ignored. Only one NMI is restarted.)
  374. *
  375. * If an NMI executes an iret, another NMI can preempt it. We do not
  376. * want to allow this new NMI to run, but we want to execute it when the
  377. * first one finishes. We set the state to "latched", and the exit of
  378. * the first NMI will perform a dec_return, if the result is zero
  379. * (NOT_RUNNING), then it will simply exit the NMI handler. If not, the
  380. * dec_return would have set the state to NMI_EXECUTING (what we want it
  381. * to be when we are running). In this case, we simply jump back to
  382. * rerun the NMI handler again, and restart the 'latched' NMI.
  383. *
  384. * No trap (breakpoint or page fault) should be hit before nmi_restart,
  385. * thus there is no race between the first check of state for NOT_RUNNING
  386. * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs
  387. * at this point.
  388. *
  389. * In case the NMI takes a page fault, we need to save off the CR2
  390. * because the NMI could have preempted another page fault and corrupt
  391. * the CR2 that is about to be read. As nested NMIs must be restarted
  392. * and they can not take breakpoints or page faults, the update of the
  393. * CR2 must be done before converting the nmi state back to NOT_RUNNING.
  394. * Otherwise, there would be a race of another nested NMI coming in
  395. * after setting state to NOT_RUNNING but before updating the nmi_cr2.
  396. */
  397. enum nmi_states {
  398. NMI_NOT_RUNNING = 0,
  399. NMI_EXECUTING,
  400. NMI_LATCHED,
  401. };
  402. static DEFINE_PER_CPU(enum nmi_states, nmi_state);
  403. static DEFINE_PER_CPU(unsigned long, nmi_cr2);
  404. #ifdef CONFIG_X86_64
  405. /*
  406. * In x86_64, we need to handle breakpoint -> NMI -> breakpoint. Without
  407. * some care, the inner breakpoint will clobber the outer breakpoint's
  408. * stack.
  409. *
  410. * If a breakpoint is being processed, and the debug stack is being
  411. * used, if an NMI comes in and also hits a breakpoint, the stack
  412. * pointer will be set to the same fixed address as the breakpoint that
  413. * was interrupted, causing that stack to be corrupted. To handle this
  414. * case, check if the stack that was interrupted is the debug stack, and
  415. * if so, change the IDT so that new breakpoints will use the current
  416. * stack and not switch to the fixed address. On return of the NMI,
  417. * switch back to the original IDT.
  418. */
  419. static DEFINE_PER_CPU(int, update_debug_stack);
  420. #endif
  421. dotraplinkage notrace void
  422. do_nmi(struct pt_regs *regs, long error_code)
  423. {
  424. if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) {
  425. this_cpu_write(nmi_state, NMI_LATCHED);
  426. return;
  427. }
  428. this_cpu_write(nmi_state, NMI_EXECUTING);
  429. this_cpu_write(nmi_cr2, read_cr2());
  430. nmi_restart:
  431. #ifdef CONFIG_X86_64
  432. /*
  433. * If we interrupted a breakpoint, it is possible that
  434. * the nmi handler will have breakpoints too. We need to
  435. * change the IDT such that breakpoints that happen here
  436. * continue to use the NMI stack.
  437. */
  438. if (unlikely(is_debug_stack(regs->sp))) {
  439. debug_stack_set_zero();
  440. this_cpu_write(update_debug_stack, 1);
  441. }
  442. #endif
  443. nmi_enter();
  444. inc_irq_stat(__nmi_count);
  445. if (!ignore_nmis)
  446. default_do_nmi(regs);
  447. nmi_exit();
  448. #ifdef CONFIG_X86_64
  449. if (unlikely(this_cpu_read(update_debug_stack))) {
  450. debug_stack_reset();
  451. this_cpu_write(update_debug_stack, 0);
  452. }
  453. #endif
  454. if (unlikely(this_cpu_read(nmi_cr2) != read_cr2()))
  455. write_cr2(this_cpu_read(nmi_cr2));
  456. if (this_cpu_dec_return(nmi_state))
  457. goto nmi_restart;
  458. if (user_mode(regs))
  459. mds_user_clear_cpu_buffers();
  460. }
  461. NOKPROBE_SYMBOL(do_nmi);
  462. void stop_nmi(void)
  463. {
  464. ignore_nmis++;
  465. }
  466. void restart_nmi(void)
  467. {
  468. ignore_nmis--;
  469. }
  470. /* reset the back-to-back NMI logic */
  471. void local_touch_nmi(void)
  472. {
  473. __this_cpu_write(last_nmi_rip, 0);
  474. }
  475. EXPORT_SYMBOL_GPL(local_touch_nmi);