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>
15 #include <linux/init.h>
16 #include <linux/percpu.h>
25 static int __initdata nmi_fail;
27 /* check to see if NMI IPIs work on this machine */
28 static DECLARE_BITMAP(nmi_ipi_mask, NR_CPUS) __initdata;
30 static int __initdata testcase_total;
31 static int __initdata testcase_successes;
32 static int __initdata expected_testcase_failures;
33 static int __initdata unexpected_testcase_failures;
34 static int __initdata unexpected_testcase_unknowns;
36 static int __init nmi_unk_cb(unsigned int val, struct pt_regs *regs)
38 unexpected_testcase_unknowns++;
42 static void __init init_nmi_testsuite(void)
44 /* trap all the unknown NMIs we may generate */
45 register_nmi_handler_initonly(NMI_UNKNOWN, nmi_unk_cb, 0, "nmi_selftest_unk");
48 static void __init cleanup_nmi_testsuite(void)
50 unregister_nmi_handler(NMI_UNKNOWN, "nmi_selftest_unk");
53 static int __init test_nmi_ipi_callback(unsigned int val, struct pt_regs *regs)
55 int cpu = raw_smp_processor_id();
57 if (cpumask_test_and_clear_cpu(cpu, to_cpumask(nmi_ipi_mask)))
63 static void __init test_nmi_ipi(struct cpumask *mask)
65 unsigned long timeout;
67 if (register_nmi_handler_initonly(NMI_LOCAL, test_nmi_ipi_callback,
68 NMI_FLAG_FIRST, "nmi_selftest")) {
73 /* sync above data before sending NMI */
76 apic->send_IPI_mask(mask, NMI_VECTOR);
78 /* Don't wait longer than a second */
79 timeout = USEC_PER_SEC;
80 while (!cpumask_empty(mask) && timeout--)
83 /* What happens if we timeout, do we still unregister?? */
84 unregister_nmi_handler(NMI_LOCAL, "nmi_selftest");
91 static void __init remote_ipi(void)
93 cpumask_copy(to_cpumask(nmi_ipi_mask), cpu_online_mask);
94 cpumask_clear_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
95 if (!cpumask_empty(to_cpumask(nmi_ipi_mask)))
96 test_nmi_ipi(to_cpumask(nmi_ipi_mask));
99 static void __init local_ipi(void)
101 cpumask_clear(to_cpumask(nmi_ipi_mask));
102 cpumask_set_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
103 test_nmi_ipi(to_cpumask(nmi_ipi_mask));
106 static void __init reset_nmi(void)
111 static void __init dotest(void (*testcase_fn)(void), int expected)
115 * Filter out expected failures:
117 if (nmi_fail != expected) {
118 unexpected_testcase_failures++;
120 if (nmi_fail == FAILURE)
121 printk(KERN_CONT "FAILED |");
122 else if (nmi_fail == TIMEOUT)
123 printk(KERN_CONT "TIMEOUT|");
125 printk(KERN_CONT "ERROR |");
128 testcase_successes++;
129 printk(KERN_CONT " ok |");
136 static inline void __init print_testname(const char *testname)
138 printk("%12s:", testname);
141 void __init nmi_selftest(void)
143 init_nmi_testsuite();
148 printk("----------------\n");
149 printk("| NMI testsuite:\n");
150 printk("--------------------\n");
152 print_testname("remote IPI");
153 dotest(remote_ipi, SUCCESS);
154 printk(KERN_CONT "\n");
155 print_testname("local IPI");
156 dotest(local_ipi, SUCCESS);
157 printk(KERN_CONT "\n");
159 cleanup_nmi_testsuite();
161 if (unexpected_testcase_failures) {
162 printk("--------------------\n");
163 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
164 unexpected_testcase_failures, testcase_total);
165 printk("-----------------------------------------------------------------\n");
166 } else if (expected_testcase_failures && testcase_successes) {
167 printk("--------------------\n");
168 printk("%3d out of %3d testcases failed, as expected. |\n",
169 expected_testcase_failures, testcase_total);
170 printk("----------------------------------------------------\n");
171 } else if (expected_testcase_failures && !testcase_successes) {
172 printk("--------------------\n");
173 printk("All %3d testcases failed, as expected. |\n",
174 expected_testcase_failures);
175 printk("----------------------------------------\n");
177 printk("--------------------\n");
178 printk("Good, all %3d testcases passed! |\n",
180 printk("---------------------------------\n");