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