nmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/delay.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/slab.h>
  19. #include <linux/export.h>
  20. #include <linux/mca.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. struct nmi_desc {
  30. spinlock_t lock;
  31. struct list_head head;
  32. };
  33. static struct nmi_desc nmi_desc[NMI_MAX] =
  34. {
  35. {
  36. .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
  37. .head = LIST_HEAD_INIT(nmi_desc[0].head),
  38. },
  39. {
  40. .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
  41. .head = LIST_HEAD_INIT(nmi_desc[1].head),
  42. },
  43. {
  44. .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[2].lock),
  45. .head = LIST_HEAD_INIT(nmi_desc[2].head),
  46. },
  47. {
  48. .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[3].lock),
  49. .head = LIST_HEAD_INIT(nmi_desc[3].head),
  50. },
  51. };
  52. struct nmi_stats {
  53. unsigned int normal;
  54. unsigned int unknown;
  55. unsigned int external;
  56. unsigned int swallow;
  57. };
  58. static DEFINE_PER_CPU(struct nmi_stats, nmi_stats);
  59. static int ignore_nmis;
  60. int unknown_nmi_panic;
  61. /*
  62. * Prevent NMI reason port (0x61) being accessed simultaneously, can
  63. * only be used in NMI handler.
  64. */
  65. static DEFINE_RAW_SPINLOCK(nmi_reason_lock);
  66. static int __init setup_unknown_nmi_panic(char *str)
  67. {
  68. unknown_nmi_panic = 1;
  69. return 1;
  70. }
  71. __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
  72. #define nmi_to_desc(type) (&nmi_desc[type])
  73. static int __kprobes nmi_handle(unsigned int type, struct pt_regs *regs, bool b2b)
  74. {
  75. struct nmi_desc *desc = nmi_to_desc(type);
  76. struct nmiaction *a;
  77. int handled=0;
  78. rcu_read_lock();
  79. /*
  80. * NMIs are edge-triggered, which means if you have enough
  81. * of them concurrently, you can lose some because only one
  82. * can be latched at any given time. Walk the whole list
  83. * to handle those situations.
  84. */
  85. list_for_each_entry_rcu(a, &desc->head, list)
  86. handled += a->handler(type, regs);
  87. rcu_read_unlock();
  88. /* return total number of NMI events handled */
  89. return handled;
  90. }
  91. int __register_nmi_handler(unsigned int type, struct nmiaction *action)
  92. {
  93. struct nmi_desc *desc = nmi_to_desc(type);
  94. unsigned long flags;
  95. if (!action->handler)
  96. return -EINVAL;
  97. spin_lock_irqsave(&desc->lock, flags);
  98. /*
  99. * most handlers of type NMI_UNKNOWN never return because
  100. * they just assume the NMI is theirs. Just a sanity check
  101. * to manage expectations
  102. */
  103. WARN_ON_ONCE(type == NMI_UNKNOWN && !list_empty(&desc->head));
  104. WARN_ON_ONCE(type == NMI_SERR && !list_empty(&desc->head));
  105. WARN_ON_ONCE(type == NMI_IO_CHECK && !list_empty(&desc->head));
  106. /*
  107. * some handlers need to be executed first otherwise a fake
  108. * event confuses some handlers (kdump uses this flag)
  109. */
  110. if (action->flags & NMI_FLAG_FIRST)
  111. list_add_rcu(&action->list, &desc->head);
  112. else
  113. list_add_tail_rcu(&action->list, &desc->head);
  114. spin_unlock_irqrestore(&desc->lock, flags);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(__register_nmi_handler);
  118. void unregister_nmi_handler(unsigned int type, const char *name)
  119. {
  120. struct nmi_desc *desc = nmi_to_desc(type);
  121. struct nmiaction *n;
  122. unsigned long flags;
  123. spin_lock_irqsave(&desc->lock, flags);
  124. list_for_each_entry_rcu(n, &desc->head, list) {
  125. /*
  126. * the name passed in to describe the nmi handler
  127. * is used as the lookup key
  128. */
  129. if (!strcmp(n->name, name)) {
  130. WARN(in_nmi(),
  131. "Trying to free NMI (%s) from NMI context!\n", n->name);
  132. list_del_rcu(&n->list);
  133. break;
  134. }
  135. }
  136. spin_unlock_irqrestore(&desc->lock, flags);
  137. synchronize_rcu();
  138. }
  139. EXPORT_SYMBOL_GPL(unregister_nmi_handler);
  140. static __kprobes void
  141. pci_serr_error(unsigned char reason, struct pt_regs *regs)
  142. {
  143. /* check to see if anyone registered against these types of errors */
  144. if (nmi_handle(NMI_SERR, regs, false))
  145. return;
  146. pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
  147. reason, smp_processor_id());
  148. /*
  149. * On some machines, PCI SERR line is used to report memory
  150. * errors. EDAC makes use of it.
  151. */
  152. #if defined(CONFIG_EDAC)
  153. if (edac_handler_set()) {
  154. edac_atomic_assert_error();
  155. return;
  156. }
  157. #endif
  158. if (panic_on_unrecovered_nmi)
  159. panic("NMI: Not continuing");
  160. pr_emerg("Dazed and confused, but trying to continue\n");
  161. /* Clear and disable the PCI SERR error line. */
  162. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_SERR;
  163. outb(reason, NMI_REASON_PORT);
  164. }
  165. static __kprobes void
  166. io_check_error(unsigned char reason, struct pt_regs *regs)
  167. {
  168. unsigned long i;
  169. /* check to see if anyone registered against these types of errors */
  170. if (nmi_handle(NMI_IO_CHECK, regs, false))
  171. return;
  172. pr_emerg(
  173. "NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
  174. reason, smp_processor_id());
  175. show_regs(regs);
  176. if (panic_on_io_nmi)
  177. panic("NMI IOCK error: Not continuing");
  178. /* Re-enable the IOCK line, wait for a few seconds */
  179. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
  180. outb(reason, NMI_REASON_PORT);
  181. i = 20000;
  182. while (--i) {
  183. touch_nmi_watchdog();
  184. udelay(100);
  185. }
  186. reason &= ~NMI_REASON_CLEAR_IOCHK;
  187. outb(reason, NMI_REASON_PORT);
  188. }
  189. static __kprobes void
  190. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  191. {
  192. int handled;
  193. /*
  194. * Use 'false' as back-to-back NMIs are dealt with one level up.
  195. * Of course this makes having multiple 'unknown' handlers useless
  196. * as only the first one is ever run (unless it can actually determine
  197. * if it caused the NMI)
  198. */
  199. handled = nmi_handle(NMI_UNKNOWN, regs, false);
  200. if (handled) {
  201. __this_cpu_add(nmi_stats.unknown, handled);
  202. return;
  203. }
  204. __this_cpu_add(nmi_stats.unknown, 1);
  205. #ifdef CONFIG_MCA
  206. /*
  207. * Might actually be able to figure out what the guilty party
  208. * is:
  209. */
  210. if (MCA_bus) {
  211. mca_handle_nmi();
  212. return;
  213. }
  214. #endif
  215. pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  216. reason, smp_processor_id());
  217. pr_emerg("Do you have a strange power saving mode enabled?\n");
  218. if (unknown_nmi_panic || panic_on_unrecovered_nmi)
  219. panic("NMI: Not continuing");
  220. pr_emerg("Dazed and confused, but trying to continue\n");
  221. }
  222. static DEFINE_PER_CPU(bool, swallow_nmi);
  223. static DEFINE_PER_CPU(unsigned long, last_nmi_rip);
  224. static __kprobes void default_do_nmi(struct pt_regs *regs)
  225. {
  226. unsigned char reason = 0;
  227. int handled;
  228. bool b2b = false;
  229. /*
  230. * CPU-specific NMI must be processed before non-CPU-specific
  231. * NMI, otherwise we may lose it, because the CPU-specific
  232. * NMI can not be detected/processed on other CPUs.
  233. */
  234. /*
  235. * Back-to-back NMIs are interesting because they can either
  236. * be two NMI or more than two NMIs (any thing over two is dropped
  237. * due to NMI being edge-triggered). If this is the second half
  238. * of the back-to-back NMI, assume we dropped things and process
  239. * more handlers. Otherwise reset the 'swallow' NMI behaviour
  240. */
  241. if (regs->ip == __this_cpu_read(last_nmi_rip))
  242. b2b = true;
  243. else
  244. __this_cpu_write(swallow_nmi, false);
  245. __this_cpu_write(last_nmi_rip, regs->ip);
  246. handled = nmi_handle(NMI_LOCAL, regs, b2b);
  247. __this_cpu_add(nmi_stats.normal, handled);
  248. if (handled) {
  249. /*
  250. * There are cases when a NMI handler handles multiple
  251. * events in the current NMI. One of these events may
  252. * be queued for in the next NMI. Because the event is
  253. * already handled, the next NMI will result in an unknown
  254. * NMI. Instead lets flag this for a potential NMI to
  255. * swallow.
  256. */
  257. if (handled > 1)
  258. __this_cpu_write(swallow_nmi, true);
  259. return;
  260. }
  261. /* Non-CPU-specific NMI: NMI sources can be processed on any CPU */
  262. raw_spin_lock(&nmi_reason_lock);
  263. reason = x86_platform.get_nmi_reason();
  264. if (reason & NMI_REASON_MASK) {
  265. if (reason & NMI_REASON_SERR)
  266. pci_serr_error(reason, regs);
  267. else if (reason & NMI_REASON_IOCHK)
  268. io_check_error(reason, regs);
  269. #ifdef CONFIG_X86_32
  270. /*
  271. * Reassert NMI in case it became active
  272. * meanwhile as it's edge-triggered:
  273. */
  274. reassert_nmi();
  275. #endif
  276. __this_cpu_add(nmi_stats.external, 1);
  277. raw_spin_unlock(&nmi_reason_lock);
  278. return;
  279. }
  280. raw_spin_unlock(&nmi_reason_lock);
  281. /*
  282. * Only one NMI can be latched at a time. To handle
  283. * this we may process multiple nmi handlers at once to
  284. * cover the case where an NMI is dropped. The downside
  285. * to this approach is we may process an NMI prematurely,
  286. * while its real NMI is sitting latched. This will cause
  287. * an unknown NMI on the next run of the NMI processing.
  288. *
  289. * We tried to flag that condition above, by setting the
  290. * swallow_nmi flag when we process more than one event.
  291. * This condition is also only present on the second half
  292. * of a back-to-back NMI, so we flag that condition too.
  293. *
  294. * If both are true, we assume we already processed this
  295. * NMI previously and we swallow it. Otherwise we reset
  296. * the logic.
  297. *
  298. * There are scenarios where we may accidentally swallow
  299. * a 'real' unknown NMI. For example, while processing
  300. * a perf NMI another perf NMI comes in along with a
  301. * 'real' unknown NMI. These two NMIs get combined into
  302. * one (as descibed above). When the next NMI gets
  303. * processed, it will be flagged by perf as handled, but
  304. * noone will know that there was a 'real' unknown NMI sent
  305. * also. As a result it gets swallowed. Or if the first
  306. * perf NMI returns two events handled then the second
  307. * NMI will get eaten by the logic below, again losing a
  308. * 'real' unknown NMI. But this is the best we can do
  309. * for now.
  310. */
  311. if (b2b && __this_cpu_read(swallow_nmi))
  312. __this_cpu_add(nmi_stats.swallow, 1);
  313. else
  314. unknown_nmi_error(reason, regs);
  315. }
  316. /*
  317. * NMIs can hit breakpoints which will cause it to lose its
  318. * NMI context with the CPU when the breakpoint does an iret.
  319. */
  320. #ifdef CONFIG_X86_32
  321. /*
  322. * For i386, NMIs use the same stack as the kernel, and we can
  323. * add a workaround to the iret problem in C. Simply have 3 states
  324. * the NMI can be in.
  325. *
  326. * 1) not running
  327. * 2) executing
  328. * 3) latched
  329. *
  330. * When no NMI is in progress, it is in the "not running" state.
  331. * When an NMI comes in, it goes into the "executing" state.
  332. * Normally, if another NMI is triggered, it does not interrupt
  333. * the running NMI and the HW will simply latch it so that when
  334. * the first NMI finishes, it will restart the second NMI.
  335. * (Note, the latch is binary, thus multiple NMIs triggering,
  336. * when one is running, are ignored. Only one NMI is restarted.)
  337. *
  338. * If an NMI hits a breakpoint that executes an iret, another
  339. * NMI can preempt it. We do not want to allow this new NMI
  340. * to run, but we want to execute it when the first one finishes.
  341. * We set the state to "latched", and the first NMI will perform
  342. * an cmpxchg on the state, and if it doesn't successfully
  343. * reset the state to "not running" it will restart the next
  344. * NMI.
  345. */
  346. enum nmi_states {
  347. NMI_NOT_RUNNING,
  348. NMI_EXECUTING,
  349. NMI_LATCHED,
  350. };
  351. static DEFINE_PER_CPU(enum nmi_states, nmi_state);
  352. #define nmi_nesting_preprocess(regs) \
  353. do { \
  354. if (__get_cpu_var(nmi_state) != NMI_NOT_RUNNING) { \
  355. __get_cpu_var(nmi_state) = NMI_LATCHED; \
  356. return; \
  357. } \
  358. nmi_restart: \
  359. __get_cpu_var(nmi_state) = NMI_EXECUTING; \
  360. } while (0)
  361. #define nmi_nesting_postprocess() \
  362. do { \
  363. if (cmpxchg(&__get_cpu_var(nmi_state), \
  364. NMI_EXECUTING, NMI_NOT_RUNNING) != NMI_EXECUTING) \
  365. goto nmi_restart; \
  366. } while (0)
  367. #else /* x86_64 */
  368. /*
  369. * In x86_64 things are a bit more difficult. This has the same problem
  370. * where an NMI hitting a breakpoint that calls iret will remove the
  371. * NMI context, allowing a nested NMI to enter. What makes this more
  372. * difficult is that both NMIs and breakpoints have their own stack.
  373. * When a new NMI or breakpoint is executed, the stack is set to a fixed
  374. * point. If an NMI is nested, it will have its stack set at that same
  375. * fixed address that the first NMI had, and will start corrupting the
  376. * stack. This is handled in entry_64.S, but the same problem exists with
  377. * the breakpoint stack.
  378. *
  379. * If a breakpoint is being processed, and the debug stack is being used,
  380. * if an NMI comes in and also hits a breakpoint, the stack pointer
  381. * will be set to the same fixed address as the breakpoint that was
  382. * interrupted, causing that stack to be corrupted. To handle this case,
  383. * check if the stack that was interrupted is the debug stack, and if
  384. * so, change the IDT so that new breakpoints will use the current stack
  385. * and not switch to the fixed address. On return of the NMI, switch back
  386. * to the original IDT.
  387. */
  388. static DEFINE_PER_CPU(int, update_debug_stack);
  389. static inline void nmi_nesting_preprocess(struct pt_regs *regs)
  390. {
  391. /*
  392. * If we interrupted a breakpoint, it is possible that
  393. * the nmi handler will have breakpoints too. We need to
  394. * change the IDT such that breakpoints that happen here
  395. * continue to use the NMI stack.
  396. */
  397. if (unlikely(is_debug_stack(regs->sp))) {
  398. debug_stack_set_zero();
  399. __get_cpu_var(update_debug_stack) = 1;
  400. }
  401. }
  402. static inline void nmi_nesting_postprocess(void)
  403. {
  404. if (unlikely(__get_cpu_var(update_debug_stack)))
  405. debug_stack_reset();
  406. }
  407. #endif
  408. dotraplinkage notrace __kprobes void
  409. do_nmi(struct pt_regs *regs, long error_code)
  410. {
  411. nmi_nesting_preprocess(regs);
  412. nmi_enter();
  413. inc_irq_stat(__nmi_count);
  414. if (!ignore_nmis)
  415. default_do_nmi(regs);
  416. nmi_exit();
  417. /* On i386, may loop back to preprocess */
  418. nmi_nesting_postprocess();
  419. }
  420. void stop_nmi(void)
  421. {
  422. ignore_nmis++;
  423. }
  424. void restart_nmi(void)
  425. {
  426. ignore_nmis--;
  427. }
  428. /* reset the back-to-back NMI logic */
  429. void local_touch_nmi(void)
  430. {
  431. __this_cpu_write(last_nmi_rip, 0);
  432. }