xref: /kvm-unit-tests/riscv/selftest.c (revision 386561f8cd050f58e9dc2ec7562014c8f1680de3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Test the framework itself. These tests confirm that setup works.
4  *
5  * Copyright (C) 2023, Ventana Micro Systems Inc., Andrew Jones <ajones@ventanamicro.com>
6  */
7 #include <libcflat.h>
8 #include <cpumask.h>
9 #include <asm/processor.h>
10 #include <asm/setup.h>
11 
12 static void check_cpus(void)
13 {
14 	int cpu;
15 
16 	for_each_present_cpu(cpu)
17 		report_info("CPU%3d: hartid=%08lx", cpu, cpus[cpu].hartid);
18 }
19 
20 static bool exceptions_work;
21 
22 static void handler(struct pt_regs *regs)
23 {
24 	exceptions_work = true;
25 	regs->epc += 2;
26 }
27 
28 static void check_exceptions(void)
29 {
30 	install_exception_handler(EXC_INST_ILLEGAL, handler);
31 	asm volatile(".4byte 0");
32 	install_exception_handler(EXC_INST_ILLEGAL, NULL);
33 	report(exceptions_work, "exceptions");
34 }
35 
36 int main(int argc, char **argv)
37 {
38 	bool r;
39 
40 	report_prefix_push("selftest");
41 
42 	report(!strncmp(argv[0], "selftest", 8), "program name");
43 
44 	if (argc > 1) {
45 		r = !strcmp(argv[1], "foo");
46 		if (argc > 2)
47 			r &= !strcmp(argv[2], "bar");
48 		if (argc > 3)
49 			r &= !strcmp(argv[3], "baz");
50 		report_info("matched %d command line parameters", argc - 1);
51 		report(r, "command line parsing");
52 	} else {
53 		report_skip("command line parsing");
54 	}
55 
56 	if (getenv("FOO")) {
57 		r = !strcmp(getenv("FOO"), "foo");
58 		r &= !strcmp(getenv("BAR"), "bar");
59 		r &= !strcmp(getenv("BAZ"), "baz");
60 		report(r, "environ parsing");
61 	} else {
62 		report_skip("environ parsing");
63 	}
64 
65 	check_exceptions();
66 	check_cpus();
67 
68 	return report_summary();
69 }
70