nmi.c 14 KB

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