2 * arch/x86/kernel/nmi-selftest.c
4 * Testsuite for NMI: IPIs
6 * Started by Don Zickus:
7 * (using lib/locking-selftest.c as a guide)
9 * Copyright (C) 2011 Red Hat, Inc., Don Zickus <dzickus@redhat.com>
12 #include <linux/smp.h>
13 #include <linux/cpumask.h>
14 #include <linux/delay.h>
25 /* check to see if NMI IPIs work on this machine */
26 static DECLARE_BITMAP(nmi_ipi_mask
, NR_CPUS
) __read_mostly
;
28 static int testcase_total
;
29 static int testcase_successes
;
30 static int expected_testcase_failures
;
31 static int unexpected_testcase_failures
;
32 static int unexpected_testcase_unknowns
;
34 static int nmi_unk_cb(unsigned int val
, struct pt_regs
*regs
)
36 unexpected_testcase_unknowns
++;
40 static void init_nmi_testsuite(void)
42 /* trap all the unknown NMIs we may generate */
43 register_nmi_handler(NMI_UNKNOWN
, nmi_unk_cb
, 0, "nmi_selftest_unk");
46 static void cleanup_nmi_testsuite(void)
48 unregister_nmi_handler(NMI_UNKNOWN
, "nmi_selftest_unk");
51 static int test_nmi_ipi_callback(unsigned int val
, struct pt_regs
*regs
)
53 int cpu
= raw_smp_processor_id();
55 if (cpumask_test_and_clear_cpu(cpu
, to_cpumask(nmi_ipi_mask
)))
61 static void test_nmi_ipi(struct cpumask
*mask
)
63 unsigned long timeout
;
65 if (register_nmi_handler(NMI_LOCAL
, test_nmi_ipi_callback
,
66 NMI_FLAG_FIRST
, "nmi_selftest")) {
71 /* sync above data before sending NMI */
74 apic
->send_IPI_mask(mask
, NMI_VECTOR
);
76 /* Don't wait longer than a second */
77 timeout
= USEC_PER_SEC
;
78 while (!cpumask_empty(mask
) && timeout
--)
81 /* What happens if we timeout, do we still unregister?? */
82 unregister_nmi_handler(NMI_LOCAL
, "nmi_selftest");
89 static void remote_ipi(void)
91 cpumask_copy(to_cpumask(nmi_ipi_mask
), cpu_online_mask
);
92 cpumask_clear_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask
));
93 if (!cpumask_empty(to_cpumask(nmi_ipi_mask
)))
94 test_nmi_ipi(to_cpumask(nmi_ipi_mask
));
97 static void local_ipi(void)
99 cpumask_clear(to_cpumask(nmi_ipi_mask
));
100 cpumask_set_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask
));
101 test_nmi_ipi(to_cpumask(nmi_ipi_mask
));
104 static void reset_nmi(void)
109 static void dotest(void (*testcase_fn
)(void), int expected
)
113 * Filter out expected failures:
115 if (nmi_fail
!= expected
) {
116 unexpected_testcase_failures
++;
118 if (nmi_fail
== FAILURE
)
120 else if (nmi_fail
== TIMEOUT
)
126 testcase_successes
++;
134 static inline void print_testname(const char *testname
)
136 printk("%12s:", testname
);
139 void nmi_selftest(void)
141 init_nmi_testsuite();
146 printk("----------------\n");
147 printk("| NMI testsuite:\n");
148 printk("--------------------\n");
150 print_testname("remote IPI");
151 dotest(remote_ipi
, SUCCESS
);
153 print_testname("local IPI");
154 dotest(local_ipi
, SUCCESS
);
157 cleanup_nmi_testsuite();
159 if (unexpected_testcase_failures
) {
160 printk("--------------------\n");
161 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
162 unexpected_testcase_failures
, testcase_total
);
163 printk("-----------------------------------------------------------------\n");
164 } else if (expected_testcase_failures
&& testcase_successes
) {
165 printk("--------------------\n");
166 printk("%3d out of %3d testcases failed, as expected. |\n",
167 expected_testcase_failures
, testcase_total
);
168 printk("----------------------------------------------------\n");
169 } else if (expected_testcase_failures
&& !testcase_successes
) {
170 printk("--------------------\n");
171 printk("All %3d testcases failed, as expected. |\n",
172 expected_testcase_failures
);
173 printk("----------------------------------------\n");
175 printk("--------------------\n");
176 printk("Good, all %3d testcases passed! |\n",
178 printk("---------------------------------\n");