1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2018 Michael Moese <mmoese@suse.de>
4  */
5 
6 /* Usage example
7  *
8  * ...
9  * #include "tst_test.h"
10  * #include "tst_taint.h"
11  * ..
12  * void setup(void)
13  * {
14  *	...
15  *	tst_taint_init(TST_TAINT_W | TST_TAINT_D));
16  *	...
17  * }
18  *
19  * void run(void)
20  * {
21  *	...
22  *	. test code here
23  *	...
24  *	if (tst_taint_check() != 0)
25  *		tst_res(TFAIL, "kernel has issues");
26  *	else
27  *		tst_res(TPASS, "kernel seems to be fine");
28  * }
29  *
30  *
31  *
32  * The above code checks, if the kernel issued a warning (TST_TAINT_W)
33  * or even died (TST_TAINT_D) during test execution.
34  * If these are set after running a test case, we most likely
35  * triggered a kernel bug.
36  */
37 
38 #ifndef TST_TAINTED_H__
39 #define TST_TAINTED_H__
40 
41 /*
42  * This are all 17 flags that are present in kernel 4.15
43  * see kernel/panic.c in kernel sources
44  *
45  * Not all of them are valid in all kernel versions.
46  */
47 #define TST_TAINT_G     (1 <<  0) /* a module with non-GPL license loaded */
48 #define TST_TAINT_F     (1 <<  1) /* a module was force-loaded */
49 #define TST_TAINT_S     (1 <<  2) /* SMP with Non-SMP kernel */
50 #define TST_TAINT_R     (1 <<  3) /* module force unloaded */
51 #define TST_TAINT_M     (1 <<  4) /* machine check error occurred */
52 #define TST_TAINT_B     (1 <<  5) /* page-release function found bad page */
53 #define TST_TAINT_U     (1 <<  6) /* user requested taint flag */
54 #define TST_TAINT_D     (1 <<  7) /* kernel died recently - OOPS or BUG */
55 #define TST_TAINT_A     (1 <<  8) /* ACPI table has been overwritten */
56 #define TST_TAINT_W     (1 <<  9) /* a warning has been issued by kernel */
57 #define TST_TAINT_C     (1 << 10) /* driver from drivers/staging was loaded */
58 #define TST_TAINT_I     (1 << 11) /* working around BIOS/Firmware bug */
59 #define TST_TAINT_O     (1 << 12) /* out of tree module loaded */
60 #define TST_TAINT_E     (1 << 13) /* unsigned module was loaded */
61 #define TST_TAINT_L     (1 << 14) /* A soft lock-up has previously occurred */
62 #define TST_TAINT_K     (1 << 15) /* kernel has been live-patched */
63 #define TST_TAINT_X	(1 << 16) /* auxiliary taint, for distro's use */
64 
65 /*
66  * Initialize and prepare support for checking tainted kernel.
67  *
68  * supply the mask of TAINT-flags you want to check, for example
69  * (TST_TAINT_W | TST_TAINT_D) when you want to check if the kernel issued
70  * a warning or even reported it died.
71  *
72  * This function tests if the requested flags are supported on the
73  * locally running kernel. In case the tainted-flags are already set by
74  * the kernel, there is no reason to continue and TCONF is generated.
75  *
76  * The mask must not be zero.
77  */
78 void tst_taint_init(unsigned int mask);
79 
80 
81 /*
82  * check if the tainted flags handed to tst_taint_init() are still not set
83  * during or after running the test.
84  * Calling this function is only allowed after tst_taint_init() was called,
85  * otherwise TBROK will be generated.
86  *
87  * returns 0 or a bitmask of the flags that currently tainted the kernel.
88  */
89 unsigned int tst_taint_check(void);
90 
91 
92 #endif /* TST_TAINTED_H__ */
93