blob: a1a96df3dff11923d59d17d2dc553eb56b333b09 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Don Zickus99e8b9c2011-10-13 15:14:26 -04002/*
3 * arch/x86/kernel/nmi-selftest.c
4 *
5 * Testsuite for NMI: IPIs
6 *
7 * Started by Don Zickus:
8 * (using lib/locking-selftest.c as a guide)
9 *
10 * Copyright (C) 2011 Red Hat, Inc., Don Zickus <dzickus@redhat.com>
11 */
12
13#include <linux/smp.h>
14#include <linux/cpumask.h>
15#include <linux/delay.h>
Jan Beulichf0ba6622012-02-24 12:09:21 +000016#include <linux/init.h>
Alex Shic6ae41e2012-05-11 15:35:27 +080017#include <linux/percpu.h>
Don Zickus99e8b9c2011-10-13 15:14:26 -040018
19#include <asm/apic.h>
20#include <asm/nmi.h>
21
22#define SUCCESS 0
23#define FAILURE 1
24#define TIMEOUT 2
25
Jan Beulichf0ba6622012-02-24 12:09:21 +000026static int __initdata nmi_fail;
Don Zickus99e8b9c2011-10-13 15:14:26 -040027
28/* check to see if NMI IPIs work on this machine */
Jan Beulichf0ba6622012-02-24 12:09:21 +000029static DECLARE_BITMAP(nmi_ipi_mask, NR_CPUS) __initdata;
Don Zickus99e8b9c2011-10-13 15:14:26 -040030
Jan Beulichf0ba6622012-02-24 12:09:21 +000031static int __initdata testcase_total;
32static int __initdata testcase_successes;
33static int __initdata expected_testcase_failures;
34static int __initdata unexpected_testcase_failures;
35static int __initdata unexpected_testcase_unknowns;
Don Zickus99e8b9c2011-10-13 15:14:26 -040036
Jan Beulichf0ba6622012-02-24 12:09:21 +000037static int __init nmi_unk_cb(unsigned int val, struct pt_regs *regs)
Don Zickus99e8b9c2011-10-13 15:14:26 -040038{
39 unexpected_testcase_unknowns++;
40 return NMI_HANDLED;
41}
42
Jan Beulichf0ba6622012-02-24 12:09:21 +000043static void __init init_nmi_testsuite(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -040044{
45 /* trap all the unknown NMIs we may generate */
Li Zhong07184672012-06-18 15:56:33 -040046 register_nmi_handler(NMI_UNKNOWN, nmi_unk_cb, 0, "nmi_selftest_unk",
47 __initdata);
Don Zickus99e8b9c2011-10-13 15:14:26 -040048}
49
Jan Beulichf0ba6622012-02-24 12:09:21 +000050static void __init cleanup_nmi_testsuite(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -040051{
52 unregister_nmi_handler(NMI_UNKNOWN, "nmi_selftest_unk");
53}
54
Jan Beulichf0ba6622012-02-24 12:09:21 +000055static int __init test_nmi_ipi_callback(unsigned int val, struct pt_regs *regs)
Don Zickus99e8b9c2011-10-13 15:14:26 -040056{
57 int cpu = raw_smp_processor_id();
58
59 if (cpumask_test_and_clear_cpu(cpu, to_cpumask(nmi_ipi_mask)))
60 return NMI_HANDLED;
61
62 return NMI_DONE;
63}
64
Jan Beulichf0ba6622012-02-24 12:09:21 +000065static void __init test_nmi_ipi(struct cpumask *mask)
Don Zickus99e8b9c2011-10-13 15:14:26 -040066{
67 unsigned long timeout;
68
Li Zhong07184672012-06-18 15:56:33 -040069 if (register_nmi_handler(NMI_LOCAL, test_nmi_ipi_callback,
70 NMI_FLAG_FIRST, "nmi_selftest", __initdata)) {
Don Zickus99e8b9c2011-10-13 15:14:26 -040071 nmi_fail = FAILURE;
72 return;
73 }
74
75 /* sync above data before sending NMI */
76 wmb();
77
78 apic->send_IPI_mask(mask, NMI_VECTOR);
79
80 /* Don't wait longer than a second */
81 timeout = USEC_PER_SEC;
Dan Carpenterc133c762017-06-19 13:53:04 +030082 while (!cpumask_empty(mask) && --timeout)
Don Zickus99e8b9c2011-10-13 15:14:26 -040083 udelay(1);
84
85 /* What happens if we timeout, do we still unregister?? */
86 unregister_nmi_handler(NMI_LOCAL, "nmi_selftest");
87
88 if (!timeout)
89 nmi_fail = TIMEOUT;
90 return;
91}
92
Jan Beulichf0ba6622012-02-24 12:09:21 +000093static void __init remote_ipi(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -040094{
95 cpumask_copy(to_cpumask(nmi_ipi_mask), cpu_online_mask);
96 cpumask_clear_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
Dan Carpenter3d6240b2011-12-07 14:06:12 +030097 if (!cpumask_empty(to_cpumask(nmi_ipi_mask)))
Don Zickus565cbc32011-12-06 13:08:59 -050098 test_nmi_ipi(to_cpumask(nmi_ipi_mask));
Don Zickus99e8b9c2011-10-13 15:14:26 -040099}
100
Jan Beulichf0ba6622012-02-24 12:09:21 +0000101static void __init local_ipi(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400102{
103 cpumask_clear(to_cpumask(nmi_ipi_mask));
104 cpumask_set_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
105 test_nmi_ipi(to_cpumask(nmi_ipi_mask));
106}
107
Jan Beulichf0ba6622012-02-24 12:09:21 +0000108static void __init reset_nmi(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400109{
110 nmi_fail = 0;
111}
112
Jan Beulichf0ba6622012-02-24 12:09:21 +0000113static void __init dotest(void (*testcase_fn)(void), int expected)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400114{
115 testcase_fn();
116 /*
117 * Filter out expected failures:
118 */
119 if (nmi_fail != expected) {
120 unexpected_testcase_failures++;
121
122 if (nmi_fail == FAILURE)
Sasha Levin29d679f2012-05-08 17:56:12 +0200123 printk(KERN_CONT "FAILED |");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400124 else if (nmi_fail == TIMEOUT)
Sasha Levin29d679f2012-05-08 17:56:12 +0200125 printk(KERN_CONT "TIMEOUT|");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400126 else
Sasha Levin29d679f2012-05-08 17:56:12 +0200127 printk(KERN_CONT "ERROR |");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400128 dump_stack();
129 } else {
130 testcase_successes++;
Sasha Levin29d679f2012-05-08 17:56:12 +0200131 printk(KERN_CONT " ok |");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400132 }
133 testcase_total++;
134
135 reset_nmi();
136}
137
Jan Beulichf0ba6622012-02-24 12:09:21 +0000138static inline void __init print_testname(const char *testname)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400139{
140 printk("%12s:", testname);
141}
142
Jan Beulichf0ba6622012-02-24 12:09:21 +0000143void __init nmi_selftest(void)
Don Zickus99e8b9c2011-10-13 15:14:26 -0400144{
145 init_nmi_testsuite();
146
147 /*
148 * Run the testsuite:
149 */
150 printk("----------------\n");
151 printk("| NMI testsuite:\n");
152 printk("--------------------\n");
153
154 print_testname("remote IPI");
155 dotest(remote_ipi, SUCCESS);
Sasha Levin29d679f2012-05-08 17:56:12 +0200156 printk(KERN_CONT "\n");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400157 print_testname("local IPI");
158 dotest(local_ipi, SUCCESS);
Sasha Levin29d679f2012-05-08 17:56:12 +0200159 printk(KERN_CONT "\n");
Don Zickus99e8b9c2011-10-13 15:14:26 -0400160
161 cleanup_nmi_testsuite();
162
163 if (unexpected_testcase_failures) {
164 printk("--------------------\n");
165 printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
166 unexpected_testcase_failures, testcase_total);
167 printk("-----------------------------------------------------------------\n");
168 } else if (expected_testcase_failures && testcase_successes) {
169 printk("--------------------\n");
170 printk("%3d out of %3d testcases failed, as expected. |\n",
171 expected_testcase_failures, testcase_total);
172 printk("----------------------------------------------------\n");
173 } else if (expected_testcase_failures && !testcase_successes) {
174 printk("--------------------\n");
175 printk("All %3d testcases failed, as expected. |\n",
176 expected_testcase_failures);
177 printk("----------------------------------------\n");
178 } else {
179 printk("--------------------\n");
180 printk("Good, all %3d testcases passed! |\n",
181 testcase_successes);
182 printk("---------------------------------\n");
183 }
184}