nmi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #if defined(CONFIG_EDAC)
  20. #include <linux/edac.h>
  21. #endif
  22. #include <linux/atomic.h>
  23. #include <asm/traps.h>
  24. #include <asm/mach_traps.h>
  25. #include <asm/nmi.h>
  26. #define NMI_MAX_NAMELEN 16
  27. struct nmiaction {
  28. struct list_head list;
  29. nmi_handler_t handler;
  30. unsigned int flags;
  31. char *name;
  32. };
  33. struct nmi_desc {
  34. spinlock_t lock;
  35. struct list_head head;
  36. };
  37. static struct nmi_desc nmi_desc[NMI_MAX] =
  38. {
  39. {
  40. .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
  41. .head = LIST_HEAD_INIT(nmi_desc[0].head),
  42. },
  43. {
  44. .lock = __SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
  45. .head = LIST_HEAD_INIT(nmi_desc[1].head),
  46. },
  47. };
  48. struct nmi_stats {
  49. unsigned int normal;
  50. unsigned int unknown;
  51. unsigned int external;
  52. unsigned int swallow;
  53. };
  54. static DEFINE_PER_CPU(struct nmi_stats, nmi_stats);
  55. static int ignore_nmis;
  56. int unknown_nmi_panic;
  57. /*
  58. * Prevent NMI reason port (0x61) being accessed simultaneously, can
  59. * only be used in NMI handler.
  60. */
  61. static DEFINE_RAW_SPINLOCK(nmi_reason_lock);
  62. static int __init setup_unknown_nmi_panic(char *str)
  63. {
  64. unknown_nmi_panic = 1;
  65. return 1;
  66. }
  67. __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
  68. #define nmi_to_desc(type) (&nmi_desc[type])
  69. static int notrace __kprobes nmi_handle(unsigned int type, struct pt_regs *regs, bool b2b)
  70. {
  71. struct nmi_desc *desc = nmi_to_desc(type);
  72. struct nmiaction *a;
  73. int handled=0;
  74. rcu_read_lock();
  75. /*
  76. * NMIs are edge-triggered, which means if you have enough
  77. * of them concurrently, you can lose some because only one
  78. * can be latched at any given time. Walk the whole list
  79. * to handle those situations.
  80. */
  81. list_for_each_entry_rcu(a, &desc->head, list)
  82. handled += a->handler(type, regs);
  83. rcu_read_unlock();
  84. /* return total number of NMI events handled */
  85. return handled;
  86. }
  87. static int __setup_nmi(unsigned int type, struct nmiaction *action)
  88. {
  89. struct nmi_desc *desc = nmi_to_desc(type);
  90. unsigned long flags;
  91. spin_lock_irqsave(&desc->lock, flags);
  92. /*
  93. * most handlers of type NMI_UNKNOWN never return because
  94. * they just assume the NMI is theirs. Just a sanity check
  95. * to manage expectations
  96. */
  97. WARN_ON_ONCE(type == NMI_UNKNOWN && !list_empty(&desc->head));
  98. /*
  99. * some handlers need to be executed first otherwise a fake
  100. * event confuses some handlers (kdump uses this flag)
  101. */
  102. if (action->flags & NMI_FLAG_FIRST)
  103. list_add_rcu(&action->list, &desc->head);
  104. else
  105. list_add_tail_rcu(&action->list, &desc->head);
  106. spin_unlock_irqrestore(&desc->lock, flags);
  107. return 0;
  108. }
  109. static struct nmiaction *__free_nmi(unsigned int type, const char *name)
  110. {
  111. struct nmi_desc *desc = nmi_to_desc(type);
  112. struct nmiaction *n;
  113. unsigned long flags;
  114. spin_lock_irqsave(&desc->lock, flags);
  115. list_for_each_entry_rcu(n, &desc->head, list) {
  116. /*
  117. * the name passed in to describe the nmi handler
  118. * is used as the lookup key
  119. */
  120. if (!strcmp(n->name, name)) {
  121. WARN(in_nmi(),
  122. "Trying to free NMI (%s) from NMI context!\n", n->name);
  123. list_del_rcu(&n->list);
  124. break;
  125. }
  126. }
  127. spin_unlock_irqrestore(&desc->lock, flags);
  128. synchronize_rcu();
  129. return (n);
  130. }
  131. int register_nmi_handler(unsigned int type, nmi_handler_t handler,
  132. unsigned long nmiflags, const char *devname)
  133. {
  134. struct nmiaction *action;
  135. int retval = -ENOMEM;
  136. if (!handler)
  137. return -EINVAL;
  138. action = kzalloc(sizeof(struct nmiaction), GFP_KERNEL);
  139. if (!action)
  140. goto fail_action;
  141. action->handler = handler;
  142. action->flags = nmiflags;
  143. action->name = kstrndup(devname, NMI_MAX_NAMELEN, GFP_KERNEL);
  144. if (!action->name)
  145. goto fail_action_name;
  146. retval = __setup_nmi(type, action);
  147. if (retval)
  148. goto fail_setup_nmi;
  149. return retval;
  150. fail_setup_nmi:
  151. kfree(action->name);
  152. fail_action_name:
  153. kfree(action);
  154. fail_action:
  155. return retval;
  156. }
  157. EXPORT_SYMBOL_GPL(register_nmi_handler);
  158. void unregister_nmi_handler(unsigned int type, const char *name)
  159. {
  160. struct nmiaction *a;
  161. a = __free_nmi(type, name);
  162. if (a) {
  163. kfree(a->name);
  164. kfree(a);
  165. }
  166. }
  167. EXPORT_SYMBOL_GPL(unregister_nmi_handler);
  168. static notrace __kprobes void
  169. pci_serr_error(unsigned char reason, struct pt_regs *regs)
  170. {
  171. pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
  172. reason, smp_processor_id());
  173. /*
  174. * On some machines, PCI SERR line is used to report memory
  175. * errors. EDAC makes use of it.
  176. */
  177. #if defined(CONFIG_EDAC)
  178. if (edac_handler_set()) {
  179. edac_atomic_assert_error();
  180. return;
  181. }
  182. #endif
  183. if (panic_on_unrecovered_nmi)
  184. panic("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. static notrace __kprobes void
  191. io_check_error(unsigned char reason, struct pt_regs *regs)
  192. {
  193. unsigned long i;
  194. pr_emerg(
  195. "NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
  196. reason, smp_processor_id());
  197. show_registers(regs);
  198. if (panic_on_io_nmi)
  199. panic("NMI IOCK error: Not continuing");
  200. /* Re-enable the IOCK line, wait for a few seconds */
  201. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
  202. outb(reason, NMI_REASON_PORT);
  203. i = 20000;
  204. while (--i) {
  205. touch_nmi_watchdog();
  206. udelay(100);
  207. }
  208. reason &= ~NMI_REASON_CLEAR_IOCHK;
  209. outb(reason, NMI_REASON_PORT);
  210. }
  211. static notrace __kprobes void
  212. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  213. {
  214. int handled;
  215. /*
  216. * Use 'false' as back-to-back NMIs are dealt with one level up.
  217. * Of course this makes having multiple 'unknown' handlers useless
  218. * as only the first one is ever run (unless it can actually determine
  219. * if it caused the NMI)
  220. */
  221. handled = nmi_handle(NMI_UNKNOWN, regs, false);
  222. if (handled) {
  223. __this_cpu_add(nmi_stats.unknown, handled);
  224. return;
  225. }
  226. __this_cpu_add(nmi_stats.unknown, 1);
  227. #ifdef CONFIG_MCA
  228. /*
  229. * Might actually be able to figure out what the guilty party
  230. * is:
  231. */
  232. if (MCA_bus) {
  233. mca_handle_nmi();
  234. return;
  235. }
  236. #endif
  237. pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
  238. reason, smp_processor_id());
  239. pr_emerg("Do you have a strange power saving mode enabled?\n");
  240. if (unknown_nmi_panic || panic_on_unrecovered_nmi)
  241. panic("NMI: Not continuing");
  242. pr_emerg("Dazed and confused, but trying to continue\n");
  243. }
  244. static DEFINE_PER_CPU(bool, swallow_nmi);
  245. static DEFINE_PER_CPU(unsigned long, last_nmi_rip);
  246. static notrace __kprobes void default_do_nmi(struct pt_regs *regs)
  247. {
  248. unsigned char reason = 0;
  249. int handled;
  250. bool b2b = false;
  251. /*
  252. * CPU-specific NMI must be processed before non-CPU-specific
  253. * NMI, otherwise we may lose it, because the CPU-specific
  254. * NMI can not be detected/processed on other CPUs.
  255. */
  256. /*
  257. * Back-to-back NMIs are interesting because they can either
  258. * be two NMI or more than two NMIs (any thing over two is dropped
  259. * due to NMI being edge-triggered). If this is the second half
  260. * of the back-to-back NMI, assume we dropped things and process
  261. * more handlers. Otherwise reset the 'swallow' NMI behaviour
  262. */
  263. if (regs->ip == __this_cpu_read(last_nmi_rip))
  264. b2b = true;
  265. else
  266. __this_cpu_write(swallow_nmi, false);
  267. __this_cpu_write(last_nmi_rip, regs->ip);
  268. handled = nmi_handle(NMI_LOCAL, regs, b2b);
  269. __this_cpu_add(nmi_stats.normal, handled);
  270. if (handled) {
  271. /*
  272. * There are cases when a NMI handler handles multiple
  273. * events in the current NMI. One of these events may
  274. * be queued for in the next NMI. Because the event is
  275. * already handled, the next NMI will result in an unknown
  276. * NMI. Instead lets flag this for a potential NMI to
  277. * swallow.
  278. */
  279. if (handled > 1)
  280. __this_cpu_write(swallow_nmi, true);
  281. return;
  282. }
  283. /* Non-CPU-specific NMI: NMI sources can be processed on any CPU */
  284. raw_spin_lock(&nmi_reason_lock);
  285. reason = get_nmi_reason();
  286. if (reason & NMI_REASON_MASK) {
  287. if (reason & NMI_REASON_SERR)
  288. pci_serr_error(reason, regs);
  289. else if (reason & NMI_REASON_IOCHK)
  290. io_check_error(reason, regs);
  291. #ifdef CONFIG_X86_32
  292. /*
  293. * Reassert NMI in case it became active
  294. * meanwhile as it's edge-triggered:
  295. */
  296. reassert_nmi();
  297. #endif
  298. __this_cpu_add(nmi_stats.external, 1);
  299. raw_spin_unlock(&nmi_reason_lock);
  300. return;
  301. }
  302. raw_spin_unlock(&nmi_reason_lock);
  303. /*
  304. * Only one NMI can be latched at a time. To handle
  305. * this we may process multiple nmi handlers at once to
  306. * cover the case where an NMI is dropped. The downside
  307. * to this approach is we may process an NMI prematurely,
  308. * while its real NMI is sitting latched. This will cause
  309. * an unknown NMI on the next run of the NMI processing.
  310. *
  311. * We tried to flag that condition above, by setting the
  312. * swallow_nmi flag when we process more than one event.
  313. * This condition is also only present on the second half
  314. * of a back-to-back NMI, so we flag that condition too.
  315. *
  316. * If both are true, we assume we already processed this
  317. * NMI previously and we swallow it. Otherwise we reset
  318. * the logic.
  319. *
  320. * There are scenarios where we may accidentally swallow
  321. * a 'real' unknown NMI. For example, while processing
  322. * a perf NMI another perf NMI comes in along with a
  323. * 'real' unknown NMI. These two NMIs get combined into
  324. * one (as descibed above). When the next NMI gets
  325. * processed, it will be flagged by perf as handled, but
  326. * noone will know that there was a 'real' unknown NMI sent
  327. * also. As a result it gets swallowed. Or if the first
  328. * perf NMI returns two events handled then the second
  329. * NMI will get eaten by the logic below, again losing a
  330. * 'real' unknown NMI. But this is the best we can do
  331. * for now.
  332. */
  333. if (b2b && __this_cpu_read(swallow_nmi))
  334. __this_cpu_add(nmi_stats.swallow, 1);
  335. else
  336. unknown_nmi_error(reason, regs);
  337. }
  338. dotraplinkage notrace __kprobes void
  339. do_nmi(struct pt_regs *regs, long error_code)
  340. {
  341. nmi_enter();
  342. inc_irq_stat(__nmi_count);
  343. if (!ignore_nmis)
  344. default_do_nmi(regs);
  345. nmi_exit();
  346. }
  347. void stop_nmi(void)
  348. {
  349. ignore_nmis++;
  350. }
  351. void restart_nmi(void)
  352. {
  353. ignore_nmis--;
  354. }
  355. /* reset the back-to-back NMI logic */
  356. void local_touch_nmi(void)
  357. {
  358. __this_cpu_write(last_nmi_rip, 0);
  359. }