nmi.c 16 KB

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