cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

kselftest.h (7757B)


      1/* SPDX-License-Identifier: GPL-2.0 */
      2/*
      3 * kselftest.h:	low-level kselftest framework to include from
      4 *		selftest programs. When possible, please use
      5 *		kselftest_harness.h instead.
      6 *
      7 * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
      8 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
      9 *
     10 * Using this API consists of first counting how many tests your code
     11 * has to run, and then starting up the reporting:
     12 *
     13 *     ksft_print_header();
     14 *     ksft_set_plan(total_number_of_tests);
     15 *
     16 * For each test, report any progress, debugging, etc with:
     17 *
     18 *     ksft_print_msg(fmt, ...);
     19 *
     20 * and finally report the pass/fail/skip/xfail state of the test with one of:
     21 *
     22 *     ksft_test_result(condition, fmt, ...);
     23 *     ksft_test_result_pass(fmt, ...);
     24 *     ksft_test_result_fail(fmt, ...);
     25 *     ksft_test_result_skip(fmt, ...);
     26 *     ksft_test_result_xfail(fmt, ...);
     27 *     ksft_test_result_error(fmt, ...);
     28 *
     29 * When all tests are finished, clean up and exit the program with one of:
     30 *
     31 *    ksft_finished();
     32 *    ksft_exit(condition);
     33 *    ksft_exit_pass();
     34 *    ksft_exit_fail();
     35 *
     36 * If the program wants to report details on why the entire program has
     37 * failed, it can instead exit with a message (this is usually done when
     38 * the program is aborting before finishing all tests):
     39 *
     40 *    ksft_exit_fail_msg(fmt, ...);
     41 *
     42 */
     43#ifndef __KSELFTEST_H
     44#define __KSELFTEST_H
     45
     46#include <errno.h>
     47#include <stdlib.h>
     48#include <unistd.h>
     49#include <stdarg.h>
     50#include <stdio.h>
     51
     52#ifndef ARRAY_SIZE
     53#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
     54#endif
     55
     56/*
     57 * gcc cpuid.h provides __cpuid_count() since v4.4.
     58 * Clang/LLVM cpuid.h provides  __cpuid_count() since v3.4.0.
     59 *
     60 * Provide local define for tests needing __cpuid_count() because
     61 * selftests need to work in older environments that do not yet
     62 * have __cpuid_count().
     63 */
     64#ifndef __cpuid_count
     65#define __cpuid_count(level, count, a, b, c, d)				\
     66	__asm__ __volatile__ ("cpuid\n\t"				\
     67			      : "=a" (a), "=b" (b), "=c" (c), "=d" (d)	\
     68			      : "0" (level), "2" (count))
     69#endif
     70
     71/* define kselftest exit codes */
     72#define KSFT_PASS  0
     73#define KSFT_FAIL  1
     74#define KSFT_XFAIL 2
     75#define KSFT_XPASS 3
     76#define KSFT_SKIP  4
     77
     78/* counters */
     79struct ksft_count {
     80	unsigned int ksft_pass;
     81	unsigned int ksft_fail;
     82	unsigned int ksft_xfail;
     83	unsigned int ksft_xpass;
     84	unsigned int ksft_xskip;
     85	unsigned int ksft_error;
     86};
     87
     88static struct ksft_count ksft_cnt;
     89static unsigned int ksft_plan;
     90
     91static inline unsigned int ksft_test_num(void)
     92{
     93	return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
     94		ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
     95		ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
     96}
     97
     98static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
     99static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
    100static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
    101static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
    102static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
    103static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
    104
    105static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
    106static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
    107static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
    108static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
    109static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
    110static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
    111
    112static inline void ksft_print_header(void)
    113{
    114	if (!(getenv("KSFT_TAP_LEVEL")))
    115		printf("TAP version 13\n");
    116}
    117
    118static inline void ksft_set_plan(unsigned int plan)
    119{
    120	ksft_plan = plan;
    121	printf("1..%d\n", ksft_plan);
    122}
    123
    124static inline void ksft_print_cnts(void)
    125{
    126	if (ksft_plan != ksft_test_num())
    127		printf("# Planned tests != run tests (%u != %u)\n",
    128			ksft_plan, ksft_test_num());
    129	printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
    130		ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
    131		ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
    132		ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
    133}
    134
    135static inline void ksft_print_msg(const char *msg, ...)
    136{
    137	int saved_errno = errno;
    138	va_list args;
    139
    140	va_start(args, msg);
    141	printf("# ");
    142	errno = saved_errno;
    143	vprintf(msg, args);
    144	va_end(args);
    145}
    146
    147static inline void ksft_test_result_pass(const char *msg, ...)
    148{
    149	int saved_errno = errno;
    150	va_list args;
    151
    152	ksft_cnt.ksft_pass++;
    153
    154	va_start(args, msg);
    155	printf("ok %d ", ksft_test_num());
    156	errno = saved_errno;
    157	vprintf(msg, args);
    158	va_end(args);
    159}
    160
    161static inline void ksft_test_result_fail(const char *msg, ...)
    162{
    163	int saved_errno = errno;
    164	va_list args;
    165
    166	ksft_cnt.ksft_fail++;
    167
    168	va_start(args, msg);
    169	printf("not ok %d ", ksft_test_num());
    170	errno = saved_errno;
    171	vprintf(msg, args);
    172	va_end(args);
    173}
    174
    175/**
    176 * ksft_test_result() - Report test success based on truth of condition
    177 *
    178 * @condition: if true, report test success, otherwise failure.
    179 */
    180#define ksft_test_result(condition, fmt, ...) do {	\
    181	if (!!(condition))				\
    182		ksft_test_result_pass(fmt, ##__VA_ARGS__);\
    183	else						\
    184		ksft_test_result_fail(fmt, ##__VA_ARGS__);\
    185	} while (0)
    186
    187static inline void ksft_test_result_xfail(const char *msg, ...)
    188{
    189	int saved_errno = errno;
    190	va_list args;
    191
    192	ksft_cnt.ksft_xfail++;
    193
    194	va_start(args, msg);
    195	printf("ok %d # XFAIL ", ksft_test_num());
    196	errno = saved_errno;
    197	vprintf(msg, args);
    198	va_end(args);
    199}
    200
    201static inline void ksft_test_result_skip(const char *msg, ...)
    202{
    203	int saved_errno = errno;
    204	va_list args;
    205
    206	ksft_cnt.ksft_xskip++;
    207
    208	va_start(args, msg);
    209	printf("ok %d # SKIP ", ksft_test_num());
    210	errno = saved_errno;
    211	vprintf(msg, args);
    212	va_end(args);
    213}
    214
    215/* TODO: how does "error" differ from "fail" or "skip"? */
    216static inline void ksft_test_result_error(const char *msg, ...)
    217{
    218	int saved_errno = errno;
    219	va_list args;
    220
    221	ksft_cnt.ksft_error++;
    222
    223	va_start(args, msg);
    224	printf("not ok %d # error ", ksft_test_num());
    225	errno = saved_errno;
    226	vprintf(msg, args);
    227	va_end(args);
    228}
    229
    230static inline int ksft_exit_pass(void)
    231{
    232	ksft_print_cnts();
    233	exit(KSFT_PASS);
    234}
    235
    236static inline int ksft_exit_fail(void)
    237{
    238	ksft_print_cnts();
    239	exit(KSFT_FAIL);
    240}
    241
    242/**
    243 * ksft_exit() - Exit selftest based on truth of condition
    244 *
    245 * @condition: if true, exit self test with success, otherwise fail.
    246 */
    247#define ksft_exit(condition) do {	\
    248	if (!!(condition))		\
    249		ksft_exit_pass();	\
    250	else				\
    251		ksft_exit_fail();	\
    252	} while (0)
    253
    254/**
    255 * ksft_finished() - Exit selftest with success if all tests passed
    256 */
    257#define ksft_finished()			\
    258	ksft_exit(ksft_plan ==		\
    259		  ksft_cnt.ksft_pass +	\
    260		  ksft_cnt.ksft_xfail +	\
    261		  ksft_cnt.ksft_xskip)
    262
    263static inline int ksft_exit_fail_msg(const char *msg, ...)
    264{
    265	int saved_errno = errno;
    266	va_list args;
    267
    268	va_start(args, msg);
    269	printf("Bail out! ");
    270	errno = saved_errno;
    271	vprintf(msg, args);
    272	va_end(args);
    273
    274	ksft_print_cnts();
    275	exit(KSFT_FAIL);
    276}
    277
    278static inline int ksft_exit_xfail(void)
    279{
    280	ksft_print_cnts();
    281	exit(KSFT_XFAIL);
    282}
    283
    284static inline int ksft_exit_xpass(void)
    285{
    286	ksft_print_cnts();
    287	exit(KSFT_XPASS);
    288}
    289
    290static inline int ksft_exit_skip(const char *msg, ...)
    291{
    292	int saved_errno = errno;
    293	va_list args;
    294
    295	va_start(args, msg);
    296
    297	/*
    298	 * FIXME: several tests misuse ksft_exit_skip so produce
    299	 * something sensible if some tests have already been run
    300	 * or a plan has been printed.  Those tests should use
    301	 * ksft_test_result_skip or ksft_exit_fail_msg instead.
    302	 */
    303	if (ksft_plan || ksft_test_num()) {
    304		ksft_cnt.ksft_xskip++;
    305		printf("ok %d # SKIP ", 1 + ksft_test_num());
    306	} else {
    307		printf("1..0 # SKIP ");
    308	}
    309	if (msg) {
    310		errno = saved_errno;
    311		vprintf(msg, args);
    312		va_end(args);
    313	}
    314	if (ksft_test_num())
    315		ksft_print_cnts();
    316	exit(KSFT_SKIP);
    317}
    318
    319#endif /* __KSELFTEST_H */