nmi.c 14 KB

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