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