nmi_selftest.c 4.3 KB

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