nmi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. * linux/arch/x86_64/nmi.c
  3. *
  4. * NMI watchdog support on APIC systems
  5. *
  6. * Started by Ingo Molnar <mingo@redhat.com>
  7. *
  8. * Fixes:
  9. * Mikael Pettersson : AMD K7 support for local APIC NMI watchdog.
  10. * Mikael Pettersson : Power Management for local APIC NMI watchdog.
  11. * Pavel Machek and
  12. * Mikael Pettersson : PM converted to driver model. Disable/enable API.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/sysdev.h>
  19. #include <linux/nmi.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/kprobes.h>
  22. #include <asm/smp.h>
  23. #include <asm/nmi.h>
  24. #include <asm/proto.h>
  25. #include <asm/kdebug.h>
  26. #include <asm/mce.h>
  27. #include <asm/intel_arch_perfmon.h>
  28. /*
  29. * lapic_nmi_owner tracks the ownership of the lapic NMI hardware:
  30. * - it may be reserved by some other driver, or not
  31. * - when not reserved by some other driver, it may be used for
  32. * the NMI watchdog, or not
  33. *
  34. * This is maintained separately from nmi_active because the NMI
  35. * watchdog may also be driven from the I/O APIC timer.
  36. */
  37. static DEFINE_SPINLOCK(lapic_nmi_owner_lock);
  38. static unsigned int lapic_nmi_owner;
  39. #define LAPIC_NMI_WATCHDOG (1<<0)
  40. #define LAPIC_NMI_RESERVED (1<<1)
  41. /* nmi_active:
  42. * +1: the lapic NMI watchdog is active, but can be disabled
  43. * 0: the lapic NMI watchdog has not been set up, and cannot
  44. * be enabled
  45. * -1: the lapic NMI watchdog is disabled, but can be enabled
  46. */
  47. int nmi_active; /* oprofile uses this */
  48. int panic_on_timeout;
  49. unsigned int nmi_watchdog = NMI_DEFAULT;
  50. static unsigned int nmi_hz = HZ;
  51. static unsigned int nmi_perfctr_msr; /* the MSR to reset in NMI handler */
  52. static unsigned int nmi_p4_cccr_val;
  53. /* Note that these events don't tick when the CPU idles. This means
  54. the frequency varies with CPU load. */
  55. #define K7_EVNTSEL_ENABLE (1 << 22)
  56. #define K7_EVNTSEL_INT (1 << 20)
  57. #define K7_EVNTSEL_OS (1 << 17)
  58. #define K7_EVNTSEL_USR (1 << 16)
  59. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  60. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  61. #define ARCH_PERFMON_NMI_EVENT_SEL ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL
  62. #define ARCH_PERFMON_NMI_EVENT_UMASK ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK
  63. #define MSR_P4_MISC_ENABLE 0x1A0
  64. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
  65. #define MSR_P4_MISC_ENABLE_PEBS_UNAVAIL (1<<12)
  66. #define MSR_P4_PERFCTR0 0x300
  67. #define MSR_P4_CCCR0 0x360
  68. #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
  69. #define P4_ESCR_OS (1<<3)
  70. #define P4_ESCR_USR (1<<2)
  71. #define P4_CCCR_OVF_PMI0 (1<<26)
  72. #define P4_CCCR_OVF_PMI1 (1<<27)
  73. #define P4_CCCR_THRESHOLD(N) ((N)<<20)
  74. #define P4_CCCR_COMPLEMENT (1<<19)
  75. #define P4_CCCR_COMPARE (1<<18)
  76. #define P4_CCCR_REQUIRED (3<<16)
  77. #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
  78. #define P4_CCCR_ENABLE (1<<12)
  79. /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  80. CRU_ESCR0 (with any non-null event selector) through a complemented
  81. max threshold. [IA32-Vol3, Section 14.9.9] */
  82. #define MSR_P4_IQ_COUNTER0 0x30C
  83. #define P4_NMI_CRU_ESCR0 (P4_ESCR_EVENT_SELECT(0x3F)|P4_ESCR_OS|P4_ESCR_USR)
  84. #define P4_NMI_IQ_CCCR0 \
  85. (P4_CCCR_OVF_PMI0|P4_CCCR_THRESHOLD(15)|P4_CCCR_COMPLEMENT| \
  86. P4_CCCR_COMPARE|P4_CCCR_REQUIRED|P4_CCCR_ESCR_SELECT(4)|P4_CCCR_ENABLE)
  87. static __cpuinit inline int nmi_known_cpu(void)
  88. {
  89. switch (boot_cpu_data.x86_vendor) {
  90. case X86_VENDOR_AMD:
  91. return boot_cpu_data.x86 == 15;
  92. case X86_VENDOR_INTEL:
  93. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  94. return 1;
  95. else
  96. return (boot_cpu_data.x86 == 15);
  97. }
  98. return 0;
  99. }
  100. /* Run after command line and cpu_init init, but before all other checks */
  101. void __cpuinit nmi_watchdog_default(void)
  102. {
  103. if (nmi_watchdog != NMI_DEFAULT)
  104. return;
  105. if (nmi_known_cpu())
  106. nmi_watchdog = NMI_LOCAL_APIC;
  107. else
  108. nmi_watchdog = NMI_IO_APIC;
  109. }
  110. #ifdef CONFIG_SMP
  111. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  112. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  113. * CPUs during the test make them busy.
  114. */
  115. static __init void nmi_cpu_busy(void *data)
  116. {
  117. volatile int *endflag = data;
  118. local_irq_enable_in_hardirq();
  119. /* Intentionally don't use cpu_relax here. This is
  120. to make sure that the performance counter really ticks,
  121. even if there is a simulator or similar that catches the
  122. pause instruction. On a real HT machine this is fine because
  123. all other CPUs are busy with "useless" delay loops and don't
  124. care if they get somewhat less cycles. */
  125. while (*endflag == 0)
  126. barrier();
  127. }
  128. #endif
  129. int __init check_nmi_watchdog (void)
  130. {
  131. volatile int endflag = 0;
  132. int *counts;
  133. int cpu;
  134. counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  135. if (!counts)
  136. return -1;
  137. printk(KERN_INFO "testing NMI watchdog ... ");
  138. #ifdef CONFIG_SMP
  139. if (nmi_watchdog == NMI_LOCAL_APIC)
  140. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  141. #endif
  142. for (cpu = 0; cpu < NR_CPUS; cpu++)
  143. counts[cpu] = cpu_pda(cpu)->__nmi_count;
  144. local_irq_enable();
  145. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  146. for_each_online_cpu(cpu) {
  147. if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
  148. endflag = 1;
  149. printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  150. cpu,
  151. counts[cpu],
  152. cpu_pda(cpu)->__nmi_count);
  153. nmi_active = 0;
  154. lapic_nmi_owner &= ~LAPIC_NMI_WATCHDOG;
  155. nmi_perfctr_msr = 0;
  156. kfree(counts);
  157. return -1;
  158. }
  159. }
  160. endflag = 1;
  161. printk("OK.\n");
  162. /* now that we know it works we can reduce NMI frequency to
  163. something more reasonable; makes a difference in some configs */
  164. if (nmi_watchdog == NMI_LOCAL_APIC)
  165. nmi_hz = 1;
  166. kfree(counts);
  167. return 0;
  168. }
  169. int __init setup_nmi_watchdog(char *str)
  170. {
  171. int nmi;
  172. if (!strncmp(str,"panic",5)) {
  173. panic_on_timeout = 1;
  174. str = strchr(str, ',');
  175. if (!str)
  176. return 1;
  177. ++str;
  178. }
  179. get_option(&str, &nmi);
  180. if (nmi >= NMI_INVALID)
  181. return 0;
  182. nmi_watchdog = nmi;
  183. return 1;
  184. }
  185. __setup("nmi_watchdog=", setup_nmi_watchdog);
  186. static void disable_intel_arch_watchdog(void);
  187. static void disable_lapic_nmi_watchdog(void)
  188. {
  189. if (nmi_active <= 0)
  190. return;
  191. switch (boot_cpu_data.x86_vendor) {
  192. case X86_VENDOR_AMD:
  193. wrmsr(MSR_K7_EVNTSEL0, 0, 0);
  194. break;
  195. case X86_VENDOR_INTEL:
  196. if (boot_cpu_data.x86 == 15) {
  197. wrmsr(MSR_P4_IQ_CCCR0, 0, 0);
  198. wrmsr(MSR_P4_CRU_ESCR0, 0, 0);
  199. } else if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  200. disable_intel_arch_watchdog();
  201. }
  202. break;
  203. }
  204. nmi_active = -1;
  205. /* tell do_nmi() and others that we're not active any more */
  206. nmi_watchdog = 0;
  207. }
  208. static void enable_lapic_nmi_watchdog(void)
  209. {
  210. if (nmi_active < 0) {
  211. nmi_watchdog = NMI_LOCAL_APIC;
  212. touch_nmi_watchdog();
  213. setup_apic_nmi_watchdog();
  214. }
  215. }
  216. int reserve_lapic_nmi(void)
  217. {
  218. unsigned int old_owner;
  219. spin_lock(&lapic_nmi_owner_lock);
  220. old_owner = lapic_nmi_owner;
  221. lapic_nmi_owner |= LAPIC_NMI_RESERVED;
  222. spin_unlock(&lapic_nmi_owner_lock);
  223. if (old_owner & LAPIC_NMI_RESERVED)
  224. return -EBUSY;
  225. if (old_owner & LAPIC_NMI_WATCHDOG)
  226. disable_lapic_nmi_watchdog();
  227. return 0;
  228. }
  229. void release_lapic_nmi(void)
  230. {
  231. unsigned int new_owner;
  232. spin_lock(&lapic_nmi_owner_lock);
  233. new_owner = lapic_nmi_owner & ~LAPIC_NMI_RESERVED;
  234. lapic_nmi_owner = new_owner;
  235. spin_unlock(&lapic_nmi_owner_lock);
  236. if (new_owner & LAPIC_NMI_WATCHDOG)
  237. enable_lapic_nmi_watchdog();
  238. }
  239. void disable_timer_nmi_watchdog(void)
  240. {
  241. if ((nmi_watchdog != NMI_IO_APIC) || (nmi_active <= 0))
  242. return;
  243. disable_irq(0);
  244. unset_nmi_callback();
  245. nmi_active = -1;
  246. nmi_watchdog = NMI_NONE;
  247. }
  248. void enable_timer_nmi_watchdog(void)
  249. {
  250. if (nmi_active < 0) {
  251. nmi_watchdog = NMI_IO_APIC;
  252. touch_nmi_watchdog();
  253. nmi_active = 1;
  254. enable_irq(0);
  255. }
  256. }
  257. #ifdef CONFIG_PM
  258. static int nmi_pm_active; /* nmi_active before suspend */
  259. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  260. {
  261. nmi_pm_active = nmi_active;
  262. disable_lapic_nmi_watchdog();
  263. return 0;
  264. }
  265. static int lapic_nmi_resume(struct sys_device *dev)
  266. {
  267. if (nmi_pm_active > 0)
  268. enable_lapic_nmi_watchdog();
  269. return 0;
  270. }
  271. static struct sysdev_class nmi_sysclass = {
  272. set_kset_name("lapic_nmi"),
  273. .resume = lapic_nmi_resume,
  274. .suspend = lapic_nmi_suspend,
  275. };
  276. static struct sys_device device_lapic_nmi = {
  277. .id = 0,
  278. .cls = &nmi_sysclass,
  279. };
  280. static int __init init_lapic_nmi_sysfs(void)
  281. {
  282. int error;
  283. if (nmi_active == 0 || nmi_watchdog != NMI_LOCAL_APIC)
  284. return 0;
  285. error = sysdev_class_register(&nmi_sysclass);
  286. if (!error)
  287. error = sysdev_register(&device_lapic_nmi);
  288. return error;
  289. }
  290. /* must come after the local APIC's device_initcall() */
  291. late_initcall(init_lapic_nmi_sysfs);
  292. #endif /* CONFIG_PM */
  293. /*
  294. * Activate the NMI watchdog via the local APIC.
  295. * Original code written by Keith Owens.
  296. */
  297. static void clear_msr_range(unsigned int base, unsigned int n)
  298. {
  299. unsigned int i;
  300. for(i = 0; i < n; ++i)
  301. wrmsr(base+i, 0, 0);
  302. }
  303. static void setup_k7_watchdog(void)
  304. {
  305. int i;
  306. unsigned int evntsel;
  307. nmi_perfctr_msr = MSR_K7_PERFCTR0;
  308. for(i = 0; i < 4; ++i) {
  309. /* Simulator may not support it */
  310. if (checking_wrmsrl(MSR_K7_EVNTSEL0+i, 0UL)) {
  311. nmi_perfctr_msr = 0;
  312. return;
  313. }
  314. wrmsrl(MSR_K7_PERFCTR0+i, 0UL);
  315. }
  316. evntsel = K7_EVNTSEL_INT
  317. | K7_EVNTSEL_OS
  318. | K7_EVNTSEL_USR
  319. | K7_NMI_EVENT;
  320. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  321. wrmsrl(MSR_K7_PERFCTR0, -((u64)cpu_khz * 1000 / nmi_hz));
  322. apic_write(APIC_LVTPC, APIC_DM_NMI);
  323. evntsel |= K7_EVNTSEL_ENABLE;
  324. wrmsr(MSR_K7_EVNTSEL0, evntsel, 0);
  325. }
  326. static void disable_intel_arch_watchdog(void)
  327. {
  328. unsigned ebx;
  329. /*
  330. * Check whether the Architectural PerfMon supports
  331. * Unhalted Core Cycles Event or not.
  332. * NOTE: Corresponding bit = 0 in ebp indicates event present.
  333. */
  334. ebx = cpuid_ebx(10);
  335. if (!(ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
  336. wrmsr(MSR_ARCH_PERFMON_EVENTSEL0, 0, 0);
  337. }
  338. static int setup_intel_arch_watchdog(void)
  339. {
  340. unsigned int evntsel;
  341. unsigned ebx;
  342. /*
  343. * Check whether the Architectural PerfMon supports
  344. * Unhalted Core Cycles Event or not.
  345. * NOTE: Corresponding bit = 0 in ebp indicates event present.
  346. */
  347. ebx = cpuid_ebx(10);
  348. if ((ebx & ARCH_PERFMON_UNHALTED_CORE_CYCLES_PRESENT))
  349. return 0;
  350. nmi_perfctr_msr = MSR_ARCH_PERFMON_PERFCTR0;
  351. clear_msr_range(MSR_ARCH_PERFMON_EVENTSEL0, 2);
  352. clear_msr_range(MSR_ARCH_PERFMON_PERFCTR0, 2);
  353. evntsel = ARCH_PERFMON_EVENTSEL_INT
  354. | ARCH_PERFMON_EVENTSEL_OS
  355. | ARCH_PERFMON_EVENTSEL_USR
  356. | ARCH_PERFMON_NMI_EVENT_SEL
  357. | ARCH_PERFMON_NMI_EVENT_UMASK;
  358. wrmsr(MSR_ARCH_PERFMON_EVENTSEL0, evntsel, 0);
  359. wrmsrl(MSR_ARCH_PERFMON_PERFCTR0, -((u64)cpu_khz * 1000 / nmi_hz));
  360. apic_write(APIC_LVTPC, APIC_DM_NMI);
  361. evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
  362. wrmsr(MSR_ARCH_PERFMON_EVENTSEL0, evntsel, 0);
  363. return 1;
  364. }
  365. static int setup_p4_watchdog(void)
  366. {
  367. unsigned int misc_enable, dummy;
  368. rdmsr(MSR_P4_MISC_ENABLE, misc_enable, dummy);
  369. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  370. return 0;
  371. nmi_perfctr_msr = MSR_P4_IQ_COUNTER0;
  372. nmi_p4_cccr_val = P4_NMI_IQ_CCCR0;
  373. #ifdef CONFIG_SMP
  374. if (smp_num_siblings == 2)
  375. nmi_p4_cccr_val |= P4_CCCR_OVF_PMI1;
  376. #endif
  377. if (!(misc_enable & MSR_P4_MISC_ENABLE_PEBS_UNAVAIL))
  378. clear_msr_range(0x3F1, 2);
  379. /* MSR 0x3F0 seems to have a default value of 0xFC00, but current
  380. docs doesn't fully define it, so leave it alone for now. */
  381. if (boot_cpu_data.x86_model >= 0x3) {
  382. /* MSR_P4_IQ_ESCR0/1 (0x3ba/0x3bb) removed */
  383. clear_msr_range(0x3A0, 26);
  384. clear_msr_range(0x3BC, 3);
  385. } else {
  386. clear_msr_range(0x3A0, 31);
  387. }
  388. clear_msr_range(0x3C0, 6);
  389. clear_msr_range(0x3C8, 6);
  390. clear_msr_range(0x3E0, 2);
  391. clear_msr_range(MSR_P4_CCCR0, 18);
  392. clear_msr_range(MSR_P4_PERFCTR0, 18);
  393. wrmsr(MSR_P4_CRU_ESCR0, P4_NMI_CRU_ESCR0, 0);
  394. wrmsr(MSR_P4_IQ_CCCR0, P4_NMI_IQ_CCCR0 & ~P4_CCCR_ENABLE, 0);
  395. Dprintk("setting P4_IQ_COUNTER0 to 0x%08lx\n", -(cpu_khz * 1000UL / nmi_hz));
  396. wrmsrl(MSR_P4_IQ_COUNTER0, -((u64)cpu_khz * 1000 / nmi_hz));
  397. apic_write(APIC_LVTPC, APIC_DM_NMI);
  398. wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
  399. return 1;
  400. }
  401. void setup_apic_nmi_watchdog(void)
  402. {
  403. switch (boot_cpu_data.x86_vendor) {
  404. case X86_VENDOR_AMD:
  405. if (boot_cpu_data.x86 != 15)
  406. return;
  407. if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
  408. return;
  409. setup_k7_watchdog();
  410. break;
  411. case X86_VENDOR_INTEL:
  412. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  413. if (!setup_intel_arch_watchdog())
  414. return;
  415. } else if (boot_cpu_data.x86 == 15) {
  416. if (!setup_p4_watchdog())
  417. return;
  418. } else {
  419. return;
  420. }
  421. break;
  422. default:
  423. return;
  424. }
  425. lapic_nmi_owner = LAPIC_NMI_WATCHDOG;
  426. nmi_active = 1;
  427. }
  428. /*
  429. * the best way to detect whether a CPU has a 'hard lockup' problem
  430. * is to check it's local APIC timer IRQ counts. If they are not
  431. * changing then that CPU has some problem.
  432. *
  433. * as these watchdog NMI IRQs are generated on every CPU, we only
  434. * have to check the current processor.
  435. */
  436. static DEFINE_PER_CPU(unsigned, last_irq_sum);
  437. static DEFINE_PER_CPU(local_t, alert_counter);
  438. static DEFINE_PER_CPU(int, nmi_touch);
  439. void touch_nmi_watchdog (void)
  440. {
  441. if (nmi_watchdog > 0) {
  442. unsigned cpu;
  443. /*
  444. * Tell other CPUs to reset their alert counters. We cannot
  445. * do it ourselves because the alert count increase is not
  446. * atomic.
  447. */
  448. for_each_present_cpu (cpu)
  449. per_cpu(nmi_touch, cpu) = 1;
  450. }
  451. touch_softlockup_watchdog();
  452. }
  453. void __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
  454. {
  455. int sum;
  456. int touched = 0;
  457. sum = read_pda(apic_timer_irqs);
  458. if (__get_cpu_var(nmi_touch)) {
  459. __get_cpu_var(nmi_touch) = 0;
  460. touched = 1;
  461. }
  462. #ifdef CONFIG_X86_MCE
  463. /* Could check oops_in_progress here too, but it's safer
  464. not too */
  465. if (atomic_read(&mce_entry) > 0)
  466. touched = 1;
  467. #endif
  468. if (!touched && __get_cpu_var(last_irq_sum) == sum) {
  469. /*
  470. * Ayiee, looks like this CPU is stuck ...
  471. * wait a few IRQs (5 seconds) before doing the oops ...
  472. */
  473. local_inc(&__get_cpu_var(alert_counter));
  474. if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz) {
  475. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  476. == NOTIFY_STOP) {
  477. local_set(&__get_cpu_var(alert_counter), 0);
  478. return;
  479. }
  480. die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs);
  481. }
  482. } else {
  483. __get_cpu_var(last_irq_sum) = sum;
  484. local_set(&__get_cpu_var(alert_counter), 0);
  485. }
  486. if (nmi_perfctr_msr) {
  487. if (nmi_perfctr_msr == MSR_P4_IQ_COUNTER0) {
  488. /*
  489. * P4 quirks:
  490. * - An overflown perfctr will assert its interrupt
  491. * until the OVF flag in its CCCR is cleared.
  492. * - LVTPC is masked on interrupt and must be
  493. * unmasked by the LVTPC handler.
  494. */
  495. wrmsr(MSR_P4_IQ_CCCR0, nmi_p4_cccr_val, 0);
  496. apic_write(APIC_LVTPC, APIC_DM_NMI);
  497. } else if (nmi_perfctr_msr == MSR_ARCH_PERFMON_PERFCTR0) {
  498. /*
  499. * For Intel based architectural perfmon
  500. * - LVTPC is masked on interrupt and must be
  501. * unmasked by the LVTPC handler.
  502. */
  503. apic_write(APIC_LVTPC, APIC_DM_NMI);
  504. }
  505. wrmsrl(nmi_perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
  506. }
  507. }
  508. static __kprobes int dummy_nmi_callback(struct pt_regs * regs, int cpu)
  509. {
  510. return 0;
  511. }
  512. static nmi_callback_t nmi_callback = dummy_nmi_callback;
  513. asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
  514. {
  515. int cpu = safe_smp_processor_id();
  516. nmi_enter();
  517. add_pda(__nmi_count,1);
  518. if (!rcu_dereference(nmi_callback)(regs, cpu))
  519. default_do_nmi(regs);
  520. nmi_exit();
  521. }
  522. void set_nmi_callback(nmi_callback_t callback)
  523. {
  524. vmalloc_sync_all();
  525. rcu_assign_pointer(nmi_callback, callback);
  526. }
  527. EXPORT_SYMBOL_GPL(set_nmi_callback);
  528. void unset_nmi_callback(void)
  529. {
  530. nmi_callback = dummy_nmi_callback;
  531. }
  532. EXPORT_SYMBOL_GPL(unset_nmi_callback);
  533. #ifdef CONFIG_SYSCTL
  534. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  535. {
  536. unsigned char reason = get_nmi_reason();
  537. char buf[64];
  538. if (!(reason & 0xc0)) {
  539. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  540. die_nmi(buf,regs);
  541. }
  542. return 0;
  543. }
  544. /*
  545. * proc handler for /proc/sys/kernel/unknown_nmi_panic
  546. */
  547. int proc_unknown_nmi_panic(struct ctl_table *table, int write, struct file *file,
  548. void __user *buffer, size_t *length, loff_t *ppos)
  549. {
  550. int old_state;
  551. old_state = unknown_nmi_panic;
  552. proc_dointvec(table, write, file, buffer, length, ppos);
  553. if (!!old_state == !!unknown_nmi_panic)
  554. return 0;
  555. if (unknown_nmi_panic) {
  556. if (reserve_lapic_nmi() < 0) {
  557. unknown_nmi_panic = 0;
  558. return -EBUSY;
  559. } else {
  560. set_nmi_callback(unknown_nmi_panic_callback);
  561. }
  562. } else {
  563. release_lapic_nmi();
  564. unset_nmi_callback();
  565. }
  566. return 0;
  567. }
  568. #endif
  569. EXPORT_SYMBOL(nmi_active);
  570. EXPORT_SYMBOL(nmi_watchdog);
  571. EXPORT_SYMBOL(reserve_lapic_nmi);
  572. EXPORT_SYMBOL(release_lapic_nmi);
  573. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  574. EXPORT_SYMBOL(enable_timer_nmi_watchdog);
  575. EXPORT_SYMBOL(touch_nmi_watchdog);