nmi_selftest.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * arch/x86/kernel/nmi-selftest.c
  3. *
  4. * Testsuite for NMI: IPIs
  5. *
  6. * Started by Don Zickus:
  7. * (using lib/locking-selftest.c as a guide)
  8. *
  9. * Copyright (C) 2011 Red Hat, Inc., Don Zickus <dzickus@redhat.com>
  10. */
  11. #include <linux/smp.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/percpu.h>
  16. #include <asm/apic.h>
  17. #include <asm/nmi.h>
  18. #define SUCCESS 0
  19. #define FAILURE 1
  20. #define TIMEOUT 2
  21. static int __initdata nmi_fail;
  22. /* check to see if NMI IPIs work on this machine */
  23. static DECLARE_BITMAP(nmi_ipi_mask, NR_CPUS) __initdata;
  24. static int __initdata testcase_total;
  25. static int __initdata testcase_successes;
  26. static int __initdata expected_testcase_failures;
  27. static int __initdata unexpected_testcase_failures;
  28. static int __initdata unexpected_testcase_unknowns;
  29. static int __init nmi_unk_cb(unsigned int val, struct pt_regs *regs)
  30. {
  31. unexpected_testcase_unknowns++;
  32. return NMI_HANDLED;
  33. }
  34. static void __init init_nmi_testsuite(void)
  35. {
  36. /* trap all the unknown NMIs we may generate */
  37. register_nmi_handler(NMI_UNKNOWN, nmi_unk_cb, 0, "nmi_selftest_unk",
  38. __initdata);
  39. }
  40. static void __init cleanup_nmi_testsuite(void)
  41. {
  42. unregister_nmi_handler(NMI_UNKNOWN, "nmi_selftest_unk");
  43. }
  44. static int __init test_nmi_ipi_callback(unsigned int val, struct pt_regs *regs)
  45. {
  46. int cpu = raw_smp_processor_id();
  47. if (cpumask_test_and_clear_cpu(cpu, to_cpumask(nmi_ipi_mask)))
  48. return NMI_HANDLED;
  49. return NMI_DONE;
  50. }
  51. static void __init test_nmi_ipi(struct cpumask *mask)
  52. {
  53. unsigned long timeout;
  54. if (register_nmi_handler(NMI_LOCAL, test_nmi_ipi_callback,
  55. NMI_FLAG_FIRST, "nmi_selftest", __initdata)) {
  56. nmi_fail = FAILURE;
  57. return;
  58. }
  59. /* sync above data before sending NMI */
  60. wmb();
  61. apic->send_IPI_mask(mask, NMI_VECTOR);
  62. /* Don't wait longer than a second */
  63. timeout = USEC_PER_SEC;
  64. while (!cpumask_empty(mask) && timeout--)
  65. udelay(1);
  66. /* What happens if we timeout, do we still unregister?? */
  67. unregister_nmi_handler(NMI_LOCAL, "nmi_selftest");
  68. if (!timeout)
  69. nmi_fail = TIMEOUT;
  70. return;
  71. }
  72. static void __init remote_ipi(void)
  73. {
  74. cpumask_copy(to_cpumask(nmi_ipi_mask), cpu_online_mask);
  75. cpumask_clear_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
  76. if (!cpumask_empty(to_cpumask(nmi_ipi_mask)))
  77. test_nmi_ipi(to_cpumask(nmi_ipi_mask));
  78. }
  79. static void __init local_ipi(void)
  80. {
  81. cpumask_clear(to_cpumask(nmi_ipi_mask));
  82. cpumask_set_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
  83. test_nmi_ipi(to_cpumask(nmi_ipi_mask));
  84. }
  85. static void __init reset_nmi(void)
  86. {
  87. nmi_fail = 0;
  88. }
  89. static void __init dotest(void (*testcase_fn)(void), int expected)
  90. {
  91. testcase_fn();
  92. /*
  93. * Filter out expected failures:
  94. */
  95. if (nmi_fail != expected) {
  96. unexpected_testcase_failures++;
  97. if (nmi_fail == FAILURE)
  98. printk(KERN_CONT "FAILED |");
  99. else if (nmi_fail == TIMEOUT)
  100. printk(KERN_CONT "TIMEOUT|");
  101. else
  102. printk(KERN_CONT "ERROR |");
  103. dump_stack();
  104. } else {
  105. testcase_successes++;
  106. printk(KERN_CONT " ok |");
  107. }
  108. testcase_total++;
  109. reset_nmi();
  110. }
  111. static inline void __init print_testname(const char *testname)
  112. {
  113. printk("%12s:", testname);
  114. }
  115. void __init nmi_selftest(void)
  116. {
  117. init_nmi_testsuite();
  118. /*
  119. * Run the testsuite:
  120. */
  121. printk("----------------\n");
  122. printk("| NMI testsuite:\n");
  123. printk("--------------------\n");
  124. print_testname("remote IPI");
  125. dotest(remote_ipi, SUCCESS);
  126. printk(KERN_CONT "\n");
  127. print_testname("local IPI");
  128. dotest(local_ipi, SUCCESS);
  129. printk(KERN_CONT "\n");
  130. cleanup_nmi_testsuite();
  131. if (unexpected_testcase_failures) {
  132. printk("--------------------\n");
  133. printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
  134. unexpected_testcase_failures, testcase_total);
  135. printk("-----------------------------------------------------------------\n");
  136. } else if (expected_testcase_failures && testcase_successes) {
  137. printk("--------------------\n");
  138. printk("%3d out of %3d testcases failed, as expected. |\n",
  139. expected_testcase_failures, testcase_total);
  140. printk("----------------------------------------------------\n");
  141. } else if (expected_testcase_failures && !testcase_successes) {
  142. printk("--------------------\n");
  143. printk("All %3d testcases failed, as expected. |\n",
  144. expected_testcase_failures);
  145. printk("----------------------------------------\n");
  146. } else {
  147. printk("--------------------\n");
  148. printk("Good, all %3d testcases passed! |\n",
  149. testcase_successes);
  150. printk("---------------------------------\n");
  151. }
  152. }