nmi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. /* perfctr_nmi_owner tracks the ownership of the perfctr registers:
  28. * evtsel_nmi_owner tracks the ownership of the event selection
  29. * - different performance counters/ event selection may be reserved for
  30. * different subsystems this reservation system just tries to coordinate
  31. * things a little
  32. */
  33. static DEFINE_PER_CPU(unsigned, perfctr_nmi_owner);
  34. static DEFINE_PER_CPU(unsigned, evntsel_nmi_owner[2]);
  35. /* this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
  36. * offset from MSR_P4_BSU_ESCR0. It will be the max for all platforms (for now)
  37. */
  38. #define NMI_MAX_COUNTER_BITS 66
  39. /* nmi_active:
  40. * >0: the lapic NMI watchdog is active, but can be disabled
  41. * <0: the lapic NMI watchdog has not been set up, and cannot
  42. * be enabled
  43. * 0: the lapic NMI watchdog is disabled, but can be enabled
  44. */
  45. atomic_t nmi_active = ATOMIC_INIT(0); /* oprofile uses this */
  46. int panic_on_timeout;
  47. unsigned int nmi_watchdog = NMI_DEFAULT;
  48. static unsigned int nmi_hz = HZ;
  49. struct nmi_watchdog_ctlblk {
  50. int enabled;
  51. u64 check_bit;
  52. unsigned int cccr_msr;
  53. unsigned int perfctr_msr; /* the MSR to reset in NMI handler */
  54. unsigned int evntsel_msr; /* the MSR to select the events to handle */
  55. };
  56. static DEFINE_PER_CPU(struct nmi_watchdog_ctlblk, nmi_watchdog_ctlblk);
  57. /* local prototypes */
  58. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu);
  59. /* converts an msr to an appropriate reservation bit */
  60. static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
  61. {
  62. /* returns the bit offset of the performance counter register */
  63. switch (boot_cpu_data.x86_vendor) {
  64. case X86_VENDOR_AMD:
  65. return (msr - MSR_K7_PERFCTR0);
  66. case X86_VENDOR_INTEL:
  67. return (msr - MSR_P4_BPU_PERFCTR0);
  68. }
  69. return 0;
  70. }
  71. /* converts an msr to an appropriate reservation bit */
  72. static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
  73. {
  74. /* returns the bit offset of the event selection register */
  75. switch (boot_cpu_data.x86_vendor) {
  76. case X86_VENDOR_AMD:
  77. return (msr - MSR_K7_EVNTSEL0);
  78. case X86_VENDOR_INTEL:
  79. return (msr - MSR_P4_BSU_ESCR0);
  80. }
  81. return 0;
  82. }
  83. /* checks for a bit availability (hack for oprofile) */
  84. int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
  85. {
  86. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  87. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  88. }
  89. /* checks the an msr for availability */
  90. int avail_to_resrv_perfctr_nmi(unsigned int msr)
  91. {
  92. unsigned int counter;
  93. counter = nmi_perfctr_msr_to_bit(msr);
  94. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  95. return (!test_bit(counter, &__get_cpu_var(perfctr_nmi_owner)));
  96. }
  97. int reserve_perfctr_nmi(unsigned int msr)
  98. {
  99. unsigned int counter;
  100. counter = nmi_perfctr_msr_to_bit(msr);
  101. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  102. if (!test_and_set_bit(counter, &__get_cpu_var(perfctr_nmi_owner)))
  103. return 1;
  104. return 0;
  105. }
  106. void release_perfctr_nmi(unsigned int msr)
  107. {
  108. unsigned int counter;
  109. counter = nmi_perfctr_msr_to_bit(msr);
  110. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  111. clear_bit(counter, &__get_cpu_var(perfctr_nmi_owner));
  112. }
  113. int reserve_evntsel_nmi(unsigned int msr)
  114. {
  115. unsigned int counter;
  116. counter = nmi_evntsel_msr_to_bit(msr);
  117. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  118. if (!test_and_set_bit(counter, &__get_cpu_var(evntsel_nmi_owner)))
  119. return 1;
  120. return 0;
  121. }
  122. void release_evntsel_nmi(unsigned int msr)
  123. {
  124. unsigned int counter;
  125. counter = nmi_evntsel_msr_to_bit(msr);
  126. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  127. clear_bit(counter, &__get_cpu_var(evntsel_nmi_owner));
  128. }
  129. static __cpuinit inline int nmi_known_cpu(void)
  130. {
  131. switch (boot_cpu_data.x86_vendor) {
  132. case X86_VENDOR_AMD:
  133. return boot_cpu_data.x86 == 15;
  134. case X86_VENDOR_INTEL:
  135. return boot_cpu_data.x86 == 15;
  136. }
  137. return 0;
  138. }
  139. /* Run after command line and cpu_init init, but before all other checks */
  140. void nmi_watchdog_default(void)
  141. {
  142. if (nmi_watchdog != NMI_DEFAULT)
  143. return;
  144. if (nmi_known_cpu())
  145. nmi_watchdog = NMI_LOCAL_APIC;
  146. else
  147. nmi_watchdog = NMI_IO_APIC;
  148. }
  149. #ifdef CONFIG_SMP
  150. /* The performance counters used by NMI_LOCAL_APIC don't trigger when
  151. * the CPU is idle. To make sure the NMI watchdog really ticks on all
  152. * CPUs during the test make them busy.
  153. */
  154. static __init void nmi_cpu_busy(void *data)
  155. {
  156. volatile int *endflag = data;
  157. local_irq_enable_in_hardirq();
  158. /* Intentionally don't use cpu_relax here. This is
  159. to make sure that the performance counter really ticks,
  160. even if there is a simulator or similar that catches the
  161. pause instruction. On a real HT machine this is fine because
  162. all other CPUs are busy with "useless" delay loops and don't
  163. care if they get somewhat less cycles. */
  164. while (*endflag == 0)
  165. barrier();
  166. }
  167. #endif
  168. int __init check_nmi_watchdog (void)
  169. {
  170. volatile int endflag = 0;
  171. int *counts;
  172. int cpu;
  173. if ((nmi_watchdog == NMI_NONE) || (nmi_watchdog == NMI_DEFAULT))
  174. return 0;
  175. if (!atomic_read(&nmi_active))
  176. return 0;
  177. counts = kmalloc(NR_CPUS * sizeof(int), GFP_KERNEL);
  178. if (!counts)
  179. return -1;
  180. printk(KERN_INFO "testing NMI watchdog ... ");
  181. #ifdef CONFIG_SMP
  182. if (nmi_watchdog == NMI_LOCAL_APIC)
  183. smp_call_function(nmi_cpu_busy, (void *)&endflag, 0, 0);
  184. #endif
  185. for (cpu = 0; cpu < NR_CPUS; cpu++)
  186. counts[cpu] = cpu_pda(cpu)->__nmi_count;
  187. local_irq_enable();
  188. mdelay((10*1000)/nmi_hz); // wait 10 ticks
  189. for_each_online_cpu(cpu) {
  190. if (!per_cpu(nmi_watchdog_ctlblk, cpu).enabled)
  191. continue;
  192. if (cpu_pda(cpu)->__nmi_count - counts[cpu] <= 5) {
  193. printk("CPU#%d: NMI appears to be stuck (%d->%d)!\n",
  194. cpu,
  195. counts[cpu],
  196. cpu_pda(cpu)->__nmi_count);
  197. per_cpu(nmi_watchdog_ctlblk, cpu).enabled = 0;
  198. atomic_dec(&nmi_active);
  199. }
  200. }
  201. if (!atomic_read(&nmi_active)) {
  202. kfree(counts);
  203. atomic_set(&nmi_active, -1);
  204. return -1;
  205. }
  206. endflag = 1;
  207. printk("OK.\n");
  208. /* now that we know it works we can reduce NMI frequency to
  209. something more reasonable; makes a difference in some configs */
  210. if (nmi_watchdog == NMI_LOCAL_APIC)
  211. nmi_hz = 1;
  212. kfree(counts);
  213. return 0;
  214. }
  215. int __init setup_nmi_watchdog(char *str)
  216. {
  217. int nmi;
  218. if (!strncmp(str,"panic",5)) {
  219. panic_on_timeout = 1;
  220. str = strchr(str, ',');
  221. if (!str)
  222. return 1;
  223. ++str;
  224. }
  225. get_option(&str, &nmi);
  226. if ((nmi >= NMI_INVALID) || (nmi < NMI_NONE))
  227. return 0;
  228. if ((nmi == NMI_LOCAL_APIC) && (nmi_known_cpu() == 0))
  229. return 0; /* no lapic support */
  230. nmi_watchdog = nmi;
  231. return 1;
  232. }
  233. __setup("nmi_watchdog=", setup_nmi_watchdog);
  234. static void disable_lapic_nmi_watchdog(void)
  235. {
  236. BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
  237. if (atomic_read(&nmi_active) <= 0)
  238. return;
  239. on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
  240. BUG_ON(atomic_read(&nmi_active) != 0);
  241. }
  242. static void enable_lapic_nmi_watchdog(void)
  243. {
  244. BUG_ON(nmi_watchdog != NMI_LOCAL_APIC);
  245. /* are we already enabled */
  246. if (atomic_read(&nmi_active) != 0)
  247. return;
  248. /* are we lapic aware */
  249. if (nmi_known_cpu() <= 0)
  250. return;
  251. on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
  252. touch_nmi_watchdog();
  253. }
  254. void disable_timer_nmi_watchdog(void)
  255. {
  256. BUG_ON(nmi_watchdog != NMI_IO_APIC);
  257. if (atomic_read(&nmi_active) <= 0)
  258. return;
  259. disable_irq(0);
  260. on_each_cpu(stop_apic_nmi_watchdog, NULL, 0, 1);
  261. BUG_ON(atomic_read(&nmi_active) != 0);
  262. }
  263. void enable_timer_nmi_watchdog(void)
  264. {
  265. BUG_ON(nmi_watchdog != NMI_IO_APIC);
  266. if (atomic_read(&nmi_active) == 0) {
  267. touch_nmi_watchdog();
  268. on_each_cpu(setup_apic_nmi_watchdog, NULL, 0, 1);
  269. enable_irq(0);
  270. }
  271. }
  272. #ifdef CONFIG_PM
  273. static int nmi_pm_active; /* nmi_active before suspend */
  274. static int lapic_nmi_suspend(struct sys_device *dev, pm_message_t state)
  275. {
  276. /* only CPU0 goes here, other CPUs should be offline */
  277. nmi_pm_active = atomic_read(&nmi_active);
  278. stop_apic_nmi_watchdog(NULL);
  279. BUG_ON(atomic_read(&nmi_active) != 0);
  280. return 0;
  281. }
  282. static int lapic_nmi_resume(struct sys_device *dev)
  283. {
  284. /* only CPU0 goes here, other CPUs should be offline */
  285. if (nmi_pm_active > 0) {
  286. setup_apic_nmi_watchdog(NULL);
  287. touch_nmi_watchdog();
  288. }
  289. return 0;
  290. }
  291. static struct sysdev_class nmi_sysclass = {
  292. set_kset_name("lapic_nmi"),
  293. .resume = lapic_nmi_resume,
  294. .suspend = lapic_nmi_suspend,
  295. };
  296. static struct sys_device device_lapic_nmi = {
  297. .id = 0,
  298. .cls = &nmi_sysclass,
  299. };
  300. static int __init init_lapic_nmi_sysfs(void)
  301. {
  302. int error;
  303. /* should really be a BUG_ON but b/c this is an
  304. * init call, it just doesn't work. -dcz
  305. */
  306. if (nmi_watchdog != NMI_LOCAL_APIC)
  307. return 0;
  308. if ( atomic_read(&nmi_active) < 0 )
  309. return 0;
  310. error = sysdev_class_register(&nmi_sysclass);
  311. if (!error)
  312. error = sysdev_register(&device_lapic_nmi);
  313. return error;
  314. }
  315. /* must come after the local APIC's device_initcall() */
  316. late_initcall(init_lapic_nmi_sysfs);
  317. #endif /* CONFIG_PM */
  318. /*
  319. * Activate the NMI watchdog via the local APIC.
  320. * Original code written by Keith Owens.
  321. */
  322. /* Note that these events don't tick when the CPU idles. This means
  323. the frequency varies with CPU load. */
  324. #define K7_EVNTSEL_ENABLE (1 << 22)
  325. #define K7_EVNTSEL_INT (1 << 20)
  326. #define K7_EVNTSEL_OS (1 << 17)
  327. #define K7_EVNTSEL_USR (1 << 16)
  328. #define K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING 0x76
  329. #define K7_NMI_EVENT K7_EVENT_CYCLES_PROCESSOR_IS_RUNNING
  330. static int setup_k7_watchdog(void)
  331. {
  332. unsigned int perfctr_msr, evntsel_msr;
  333. unsigned int evntsel;
  334. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  335. perfctr_msr = MSR_K7_PERFCTR0;
  336. evntsel_msr = MSR_K7_EVNTSEL0;
  337. if (!reserve_perfctr_nmi(perfctr_msr))
  338. goto fail;
  339. if (!reserve_evntsel_nmi(evntsel_msr))
  340. goto fail1;
  341. /* Simulator may not support it */
  342. if (checking_wrmsrl(evntsel_msr, 0UL))
  343. goto fail2;
  344. wrmsrl(perfctr_msr, 0UL);
  345. evntsel = K7_EVNTSEL_INT
  346. | K7_EVNTSEL_OS
  347. | K7_EVNTSEL_USR
  348. | K7_NMI_EVENT;
  349. /* setup the timer */
  350. wrmsr(evntsel_msr, evntsel, 0);
  351. wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
  352. apic_write(APIC_LVTPC, APIC_DM_NMI);
  353. evntsel |= K7_EVNTSEL_ENABLE;
  354. wrmsr(evntsel_msr, evntsel, 0);
  355. wd->perfctr_msr = perfctr_msr;
  356. wd->evntsel_msr = evntsel_msr;
  357. wd->cccr_msr = 0; //unused
  358. wd->check_bit = 1ULL<<63;
  359. return 1;
  360. fail2:
  361. release_evntsel_nmi(evntsel_msr);
  362. fail1:
  363. release_perfctr_nmi(perfctr_msr);
  364. fail:
  365. return 0;
  366. }
  367. static void stop_k7_watchdog(void)
  368. {
  369. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  370. wrmsr(wd->evntsel_msr, 0, 0);
  371. release_evntsel_nmi(wd->evntsel_msr);
  372. release_perfctr_nmi(wd->perfctr_msr);
  373. }
  374. /* Note that these events don't tick when the CPU idles. This means
  375. the frequency varies with CPU load. */
  376. #define MSR_P4_MISC_ENABLE_PERF_AVAIL (1<<7)
  377. #define P4_ESCR_EVENT_SELECT(N) ((N)<<25)
  378. #define P4_ESCR_OS (1<<3)
  379. #define P4_ESCR_USR (1<<2)
  380. #define P4_CCCR_OVF_PMI0 (1<<26)
  381. #define P4_CCCR_OVF_PMI1 (1<<27)
  382. #define P4_CCCR_THRESHOLD(N) ((N)<<20)
  383. #define P4_CCCR_COMPLEMENT (1<<19)
  384. #define P4_CCCR_COMPARE (1<<18)
  385. #define P4_CCCR_REQUIRED (3<<16)
  386. #define P4_CCCR_ESCR_SELECT(N) ((N)<<13)
  387. #define P4_CCCR_ENABLE (1<<12)
  388. #define P4_CCCR_OVF (1<<31)
  389. /* Set up IQ_COUNTER0 to behave like a clock, by having IQ_CCCR0 filter
  390. CRU_ESCR0 (with any non-null event selector) through a complemented
  391. max threshold. [IA32-Vol3, Section 14.9.9] */
  392. static int setup_p4_watchdog(void)
  393. {
  394. unsigned int perfctr_msr, evntsel_msr, cccr_msr;
  395. unsigned int evntsel, cccr_val;
  396. unsigned int misc_enable, dummy;
  397. unsigned int ht_num;
  398. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  399. rdmsr(MSR_IA32_MISC_ENABLE, misc_enable, dummy);
  400. if (!(misc_enable & MSR_P4_MISC_ENABLE_PERF_AVAIL))
  401. return 0;
  402. #ifdef CONFIG_SMP
  403. /* detect which hyperthread we are on */
  404. if (smp_num_siblings == 2) {
  405. unsigned int ebx, apicid;
  406. ebx = cpuid_ebx(1);
  407. apicid = (ebx >> 24) & 0xff;
  408. ht_num = apicid & 1;
  409. } else
  410. #endif
  411. ht_num = 0;
  412. /* performance counters are shared resources
  413. * assign each hyperthread its own set
  414. * (re-use the ESCR0 register, seems safe
  415. * and keeps the cccr_val the same)
  416. */
  417. if (!ht_num) {
  418. /* logical cpu 0 */
  419. perfctr_msr = MSR_P4_IQ_PERFCTR0;
  420. evntsel_msr = MSR_P4_CRU_ESCR0;
  421. cccr_msr = MSR_P4_IQ_CCCR0;
  422. cccr_val = P4_CCCR_OVF_PMI0 | P4_CCCR_ESCR_SELECT(4);
  423. } else {
  424. /* logical cpu 1 */
  425. perfctr_msr = MSR_P4_IQ_PERFCTR1;
  426. evntsel_msr = MSR_P4_CRU_ESCR0;
  427. cccr_msr = MSR_P4_IQ_CCCR1;
  428. cccr_val = P4_CCCR_OVF_PMI1 | P4_CCCR_ESCR_SELECT(4);
  429. }
  430. if (!reserve_perfctr_nmi(perfctr_msr))
  431. goto fail;
  432. if (!reserve_evntsel_nmi(evntsel_msr))
  433. goto fail1;
  434. evntsel = P4_ESCR_EVENT_SELECT(0x3F)
  435. | P4_ESCR_OS
  436. | P4_ESCR_USR;
  437. cccr_val |= P4_CCCR_THRESHOLD(15)
  438. | P4_CCCR_COMPLEMENT
  439. | P4_CCCR_COMPARE
  440. | P4_CCCR_REQUIRED;
  441. wrmsr(evntsel_msr, evntsel, 0);
  442. wrmsr(cccr_msr, cccr_val, 0);
  443. wrmsrl(perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
  444. apic_write(APIC_LVTPC, APIC_DM_NMI);
  445. cccr_val |= P4_CCCR_ENABLE;
  446. wrmsr(cccr_msr, cccr_val, 0);
  447. wd->perfctr_msr = perfctr_msr;
  448. wd->evntsel_msr = evntsel_msr;
  449. wd->cccr_msr = cccr_msr;
  450. wd->check_bit = 1ULL<<39;
  451. return 1;
  452. fail1:
  453. release_perfctr_nmi(perfctr_msr);
  454. fail:
  455. return 0;
  456. }
  457. static void stop_p4_watchdog(void)
  458. {
  459. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  460. wrmsr(wd->cccr_msr, 0, 0);
  461. wrmsr(wd->evntsel_msr, 0, 0);
  462. release_evntsel_nmi(wd->evntsel_msr);
  463. release_perfctr_nmi(wd->perfctr_msr);
  464. }
  465. void setup_apic_nmi_watchdog(void *unused)
  466. {
  467. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  468. /* only support LOCAL and IO APICs for now */
  469. if ((nmi_watchdog != NMI_LOCAL_APIC) &&
  470. (nmi_watchdog != NMI_IO_APIC))
  471. return;
  472. if (wd->enabled == 1)
  473. return;
  474. /* cheap hack to support suspend/resume */
  475. /* if cpu0 is not active neither should the other cpus */
  476. if ((smp_processor_id() != 0) && (atomic_read(&nmi_active) <= 0))
  477. return;
  478. if (nmi_watchdog == NMI_LOCAL_APIC) {
  479. switch (boot_cpu_data.x86_vendor) {
  480. case X86_VENDOR_AMD:
  481. if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
  482. return;
  483. if (!setup_k7_watchdog())
  484. return;
  485. break;
  486. case X86_VENDOR_INTEL:
  487. if (!setup_p4_watchdog())
  488. return;
  489. break;
  490. default:
  491. return;
  492. }
  493. }
  494. wd->enabled = 1;
  495. atomic_inc(&nmi_active);
  496. }
  497. void stop_apic_nmi_watchdog(void *unused)
  498. {
  499. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  500. /* only support LOCAL and IO APICs for now */
  501. if ((nmi_watchdog != NMI_LOCAL_APIC) &&
  502. (nmi_watchdog != NMI_IO_APIC))
  503. return;
  504. if (wd->enabled == 0)
  505. return;
  506. if (nmi_watchdog == NMI_LOCAL_APIC) {
  507. switch (boot_cpu_data.x86_vendor) {
  508. case X86_VENDOR_AMD:
  509. if (strstr(boot_cpu_data.x86_model_id, "Screwdriver"))
  510. return;
  511. stop_k7_watchdog();
  512. break;
  513. case X86_VENDOR_INTEL:
  514. stop_p4_watchdog();
  515. break;
  516. default:
  517. return;
  518. }
  519. }
  520. wd->enabled = 0;
  521. atomic_dec(&nmi_active);
  522. }
  523. /*
  524. * the best way to detect whether a CPU has a 'hard lockup' problem
  525. * is to check it's local APIC timer IRQ counts. If they are not
  526. * changing then that CPU has some problem.
  527. *
  528. * as these watchdog NMI IRQs are generated on every CPU, we only
  529. * have to check the current processor.
  530. */
  531. static DEFINE_PER_CPU(unsigned, last_irq_sum);
  532. static DEFINE_PER_CPU(local_t, alert_counter);
  533. static DEFINE_PER_CPU(int, nmi_touch);
  534. void touch_nmi_watchdog (void)
  535. {
  536. if (nmi_watchdog > 0) {
  537. unsigned cpu;
  538. /*
  539. * Tell other CPUs to reset their alert counters. We cannot
  540. * do it ourselves because the alert count increase is not
  541. * atomic.
  542. */
  543. for_each_present_cpu (cpu)
  544. per_cpu(nmi_touch, cpu) = 1;
  545. }
  546. touch_softlockup_watchdog();
  547. }
  548. int __kprobes nmi_watchdog_tick(struct pt_regs * regs, unsigned reason)
  549. {
  550. int sum;
  551. int touched = 0;
  552. struct nmi_watchdog_ctlblk *wd = &__get_cpu_var(nmi_watchdog_ctlblk);
  553. u64 dummy;
  554. int rc=0;
  555. /* check for other users first */
  556. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT)
  557. == NOTIFY_STOP) {
  558. rc = 1;
  559. touched = 1;
  560. }
  561. sum = read_pda(apic_timer_irqs);
  562. if (__get_cpu_var(nmi_touch)) {
  563. __get_cpu_var(nmi_touch) = 0;
  564. touched = 1;
  565. }
  566. #ifdef CONFIG_X86_MCE
  567. /* Could check oops_in_progress here too, but it's safer
  568. not too */
  569. if (atomic_read(&mce_entry) > 0)
  570. touched = 1;
  571. #endif
  572. /* if the apic timer isn't firing, this cpu isn't doing much */
  573. if (!touched && __get_cpu_var(last_irq_sum) == sum) {
  574. /*
  575. * Ayiee, looks like this CPU is stuck ...
  576. * wait a few IRQs (5 seconds) before doing the oops ...
  577. */
  578. local_inc(&__get_cpu_var(alert_counter));
  579. if (local_read(&__get_cpu_var(alert_counter)) == 5*nmi_hz)
  580. die_nmi("NMI Watchdog detected LOCKUP on CPU %d\n", regs,
  581. panic_on_timeout);
  582. } else {
  583. __get_cpu_var(last_irq_sum) = sum;
  584. local_set(&__get_cpu_var(alert_counter), 0);
  585. }
  586. /* see if the nmi watchdog went off */
  587. if (wd->enabled) {
  588. if (nmi_watchdog == NMI_LOCAL_APIC) {
  589. rdmsrl(wd->perfctr_msr, dummy);
  590. if (dummy & wd->check_bit){
  591. /* this wasn't a watchdog timer interrupt */
  592. goto done;
  593. }
  594. /* only Intel uses the cccr msr */
  595. if (wd->cccr_msr != 0) {
  596. /*
  597. * P4 quirks:
  598. * - An overflown perfctr will assert its interrupt
  599. * until the OVF flag in its CCCR is cleared.
  600. * - LVTPC is masked on interrupt and must be
  601. * unmasked by the LVTPC handler.
  602. */
  603. rdmsrl(wd->cccr_msr, dummy);
  604. dummy &= ~P4_CCCR_OVF;
  605. wrmsrl(wd->cccr_msr, dummy);
  606. apic_write(APIC_LVTPC, APIC_DM_NMI);
  607. }
  608. /* start the cycle over again */
  609. wrmsrl(wd->perfctr_msr, -((u64)cpu_khz * 1000 / nmi_hz));
  610. rc = 1;
  611. } else if (nmi_watchdog == NMI_IO_APIC) {
  612. /* don't know how to accurately check for this.
  613. * just assume it was a watchdog timer interrupt
  614. * This matches the old behaviour.
  615. */
  616. rc = 1;
  617. } else
  618. printk(KERN_WARNING "Unknown enabled NMI hardware?!\n");
  619. }
  620. done:
  621. return rc;
  622. }
  623. asmlinkage __kprobes void do_nmi(struct pt_regs * regs, long error_code)
  624. {
  625. nmi_enter();
  626. add_pda(__nmi_count,1);
  627. default_do_nmi(regs);
  628. nmi_exit();
  629. }
  630. int do_nmi_callback(struct pt_regs * regs, int cpu)
  631. {
  632. #ifdef CONFIG_SYSCTL
  633. if (unknown_nmi_panic)
  634. return unknown_nmi_panic_callback(regs, cpu);
  635. #endif
  636. return 0;
  637. }
  638. #ifdef CONFIG_SYSCTL
  639. static int unknown_nmi_panic_callback(struct pt_regs *regs, int cpu)
  640. {
  641. unsigned char reason = get_nmi_reason();
  642. char buf[64];
  643. sprintf(buf, "NMI received for unknown reason %02x\n", reason);
  644. die_nmi(buf, regs, 1); /* Always panic here */
  645. return 0;
  646. }
  647. /*
  648. * proc handler for /proc/sys/kernel/nmi
  649. */
  650. int proc_nmi_enabled(struct ctl_table *table, int write, struct file *file,
  651. void __user *buffer, size_t *length, loff_t *ppos)
  652. {
  653. int old_state;
  654. nmi_watchdog_enabled = (atomic_read(&nmi_active) > 0) ? 1 : 0;
  655. old_state = nmi_watchdog_enabled;
  656. proc_dointvec(table, write, file, buffer, length, ppos);
  657. if (!!old_state == !!nmi_watchdog_enabled)
  658. return 0;
  659. if (atomic_read(&nmi_active) < 0) {
  660. printk( KERN_WARNING "NMI watchdog is permanently disabled\n");
  661. return -EIO;
  662. }
  663. /* if nmi_watchdog is not set yet, then set it */
  664. nmi_watchdog_default();
  665. if (nmi_watchdog == NMI_LOCAL_APIC) {
  666. if (nmi_watchdog_enabled)
  667. enable_lapic_nmi_watchdog();
  668. else
  669. disable_lapic_nmi_watchdog();
  670. } else {
  671. printk( KERN_WARNING
  672. "NMI watchdog doesn't know what hardware to touch\n");
  673. return -EIO;
  674. }
  675. return 0;
  676. }
  677. #endif
  678. EXPORT_SYMBOL(nmi_active);
  679. EXPORT_SYMBOL(nmi_watchdog);
  680. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi);
  681. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
  682. EXPORT_SYMBOL(reserve_perfctr_nmi);
  683. EXPORT_SYMBOL(release_perfctr_nmi);
  684. EXPORT_SYMBOL(reserve_evntsel_nmi);
  685. EXPORT_SYMBOL(release_evntsel_nmi);
  686. EXPORT_SYMBOL(disable_timer_nmi_watchdog);
  687. EXPORT_SYMBOL(enable_timer_nmi_watchdog);
  688. EXPORT_SYMBOL(touch_nmi_watchdog);