nmi_selftest.c 4.4 KB
Newer Older
D
Don Zickus 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * arch/x86/kernel/nmi-selftest.c
 *
 * Testsuite for NMI: IPIs
 *
 * Started by Don Zickus:
 * (using lib/locking-selftest.c as a guide)
 *
 *   Copyright (C) 2011 Red Hat, Inc., Don Zickus <dzickus@redhat.com>
 */

#include <linux/smp.h>
#include <linux/cpumask.h>
#include <linux/delay.h>
15
#include <linux/init.h>
16
#include <linux/percpu.h>
D
Don Zickus 已提交
17 18 19 20 21 22 23 24

#include <asm/apic.h>
#include <asm/nmi.h>

#define SUCCESS		0
#define FAILURE		1
#define TIMEOUT		2

25
static int __initdata nmi_fail;
D
Don Zickus 已提交
26 27

/* check to see if NMI IPIs work on this machine */
28
static DECLARE_BITMAP(nmi_ipi_mask, NR_CPUS) __initdata;
D
Don Zickus 已提交
29

30 31 32 33 34
static int __initdata testcase_total;
static int __initdata testcase_successes;
static int __initdata expected_testcase_failures;
static int __initdata unexpected_testcase_failures;
static int __initdata unexpected_testcase_unknowns;
D
Don Zickus 已提交
35

36
static int __init nmi_unk_cb(unsigned int val, struct pt_regs *regs)
D
Don Zickus 已提交
37 38 39 40 41
{
	unexpected_testcase_unknowns++;
	return NMI_HANDLED;
}

42
static void __init init_nmi_testsuite(void)
D
Don Zickus 已提交
43 44
{
	/* trap all the unknown NMIs we may generate */
45
	register_nmi_handler_initonly(NMI_UNKNOWN, nmi_unk_cb, 0, "nmi_selftest_unk");
D
Don Zickus 已提交
46 47
}

48
static void __init cleanup_nmi_testsuite(void)
D
Don Zickus 已提交
49 50 51 52
{
	unregister_nmi_handler(NMI_UNKNOWN, "nmi_selftest_unk");
}

53
static int __init test_nmi_ipi_callback(unsigned int val, struct pt_regs *regs)
D
Don Zickus 已提交
54 55 56 57 58 59 60 61 62
{
        int cpu = raw_smp_processor_id();

        if (cpumask_test_and_clear_cpu(cpu, to_cpumask(nmi_ipi_mask)))
                return NMI_HANDLED;

        return NMI_DONE;
}

63
static void __init test_nmi_ipi(struct cpumask *mask)
D
Don Zickus 已提交
64 65 66
{
	unsigned long timeout;

67
	if (register_nmi_handler_initonly(NMI_LOCAL, test_nmi_ipi_callback,
D
Don Zickus 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
				 NMI_FLAG_FIRST, "nmi_selftest")) {
		nmi_fail = FAILURE;
		return;
	}

	/* sync above data before sending NMI */
	wmb();

	apic->send_IPI_mask(mask, NMI_VECTOR);

	/* Don't wait longer than a second */
	timeout = USEC_PER_SEC;
	while (!cpumask_empty(mask) && timeout--)
	        udelay(1);

	/* What happens if we timeout, do we still unregister?? */
	unregister_nmi_handler(NMI_LOCAL, "nmi_selftest");

	if (!timeout)
		nmi_fail = TIMEOUT;
	return;
}

91
static void __init remote_ipi(void)
D
Don Zickus 已提交
92 93 94
{
	cpumask_copy(to_cpumask(nmi_ipi_mask), cpu_online_mask);
	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));
D
Don Zickus 已提交
97 98
}

99
static void __init local_ipi(void)
D
Don Zickus 已提交
100 101 102 103 104 105
{
	cpumask_clear(to_cpumask(nmi_ipi_mask));
	cpumask_set_cpu(smp_processor_id(), to_cpumask(nmi_ipi_mask));
	test_nmi_ipi(to_cpumask(nmi_ipi_mask));
}

106
static void __init reset_nmi(void)
D
Don Zickus 已提交
107 108 109 110
{
	nmi_fail = 0;
}

111
static void __init dotest(void (*testcase_fn)(void), int expected)
D
Don Zickus 已提交
112 113 114 115 116 117 118 119 120
{
	testcase_fn();
	/*
	 * Filter out expected failures:
	 */
	if (nmi_fail != expected) {
		unexpected_testcase_failures++;

		if (nmi_fail == FAILURE)
121
			printk(KERN_CONT "FAILED |");
D
Don Zickus 已提交
122
		else if (nmi_fail == TIMEOUT)
123
			printk(KERN_CONT "TIMEOUT|");
D
Don Zickus 已提交
124
		else
125
			printk(KERN_CONT "ERROR  |");
D
Don Zickus 已提交
126 127 128
		dump_stack();
	} else {
		testcase_successes++;
129
		printk(KERN_CONT "  ok  |");
D
Don Zickus 已提交
130 131 132 133 134 135
	}
	testcase_total++;

	reset_nmi();
}

136
static inline void __init print_testname(const char *testname)
D
Don Zickus 已提交
137 138 139 140
{
	printk("%12s:", testname);
}

141
void __init nmi_selftest(void)
D
Don Zickus 已提交
142 143 144 145 146 147 148 149 150 151 152 153
{
	init_nmi_testsuite();

        /*
	 * Run the testsuite:
	 */
	printk("----------------\n");
	printk("| NMI testsuite:\n");
	printk("--------------------\n");

	print_testname("remote IPI");
	dotest(remote_ipi, SUCCESS);
154
	printk(KERN_CONT "\n");
D
Don Zickus 已提交
155 156
	print_testname("local IPI");
	dotest(local_ipi, SUCCESS);
157
	printk(KERN_CONT "\n");
D
Don Zickus 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

	cleanup_nmi_testsuite();

	if (unexpected_testcase_failures) {
		printk("--------------------\n");
		printk("BUG: %3d unexpected failures (out of %3d) - debugging disabled! |\n",
			unexpected_testcase_failures, testcase_total);
		printk("-----------------------------------------------------------------\n");
	} else if (expected_testcase_failures && testcase_successes) {
		printk("--------------------\n");
		printk("%3d out of %3d testcases failed, as expected. |\n",
			expected_testcase_failures, testcase_total);
		printk("----------------------------------------------------\n");
	} else if (expected_testcase_failures && !testcase_successes) {
		printk("--------------------\n");
		printk("All %3d testcases failed, as expected. |\n",
			expected_testcase_failures);
		printk("----------------------------------------\n");
	} else {
		printk("--------------------\n");
		printk("Good, all %3d testcases passed! |\n",
			testcase_successes);
		printk("---------------------------------\n");
	}
}