Skip to content

Commit af71bc1

Browse files
charlie-rivospalmer-dabbelt
authored andcommitted
riscv: Add tests for riscv module loading
Add test cases for the two main groups of relocations added: SUB and SET, along with uleb128. Signed-off-by: Charlie Jenkins <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 8fd6c51 commit af71bc1

16 files changed

+366
-0
lines changed

arch/riscv/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source "arch/riscv/kernel/tests/Kconfig.debug"

arch/riscv/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ obj-y += stacktrace.o
5757
obj-y += cacheinfo.o
5858
obj-y += patch.o
5959
obj-y += probes/
60+
obj-y += tests/
6061
obj-$(CONFIG_MMU) += vdso.o vdso/
6162

6263
obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
menu "arch/riscv/kernel Testing and Coverage"
3+
4+
config AS_HAS_ULEB128
5+
def_bool $(as-instr,.reloc label$(comma) R_RISCV_SET_ULEB128$(comma) 127\n.reloc label$(comma) R_RISCV_SUB_ULEB128$(comma) 127\nlabel:\n.word 0)
6+
7+
menuconfig RUNTIME_KERNEL_TESTING_MENU
8+
bool "arch/riscv/kernel runtime Testing"
9+
def_bool y
10+
help
11+
Enable riscv kernel runtime testing.
12+
13+
if RUNTIME_KERNEL_TESTING_MENU
14+
15+
config RISCV_MODULE_LINKING_KUNIT
16+
bool "KUnit test riscv module linking at runtime" if !KUNIT_ALL_TESTS
17+
depends on KUNIT
18+
default KUNIT_ALL_TESTS
19+
help
20+
Enable this option to test riscv module linking at boot. This will
21+
enable a module called "test_module_linking".
22+
23+
KUnit tests run during boot and output the results to the debug log
24+
in TAP format (http://testanything.org/). Only useful for kernel devs
25+
running the KUnit test harness, and not intended for inclusion into a
26+
production build.
27+
28+
For more information on KUnit and unit tests in general please refer
29+
to the KUnit documentation in Documentation/dev-tools/kunit/.
30+
31+
If unsure, say N.
32+
33+
endif # RUNTIME_TESTING_MENU
34+
35+
endmenu # "arch/riscv/kernel runtime Testing"

arch/riscv/kernel/tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
obj-$(CONFIG_RISCV_MODULE_LINKING_KUNIT) += module_test/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
obj-m += test_module_linking.o
2+
3+
test_sub := test_sub6.o test_sub8.o test_sub16.o test_sub32.o test_sub64.o
4+
5+
test_set := test_set6.o test_set8.o test_set16.o test_set32.o
6+
7+
test_module_linking-objs += $(test_sub)
8+
9+
test_module_linking-objs += $(test_set)
10+
11+
ifeq ($(CONFIG_AS_HAS_ULEB128),y)
12+
test_module_linking-objs += test_uleb128.o
13+
endif
14+
15+
test_module_linking-objs += test_module_linking_main.o
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2023 Rivos Inc.
4+
*/
5+
6+
#include <linux/module.h>
7+
#include <linux/kernel.h>
8+
#include <linux/init.h>
9+
#include <kunit/test.h>
10+
11+
MODULE_LICENSE("GPL");
12+
MODULE_DESCRIPTION("Test module linking");
13+
14+
extern int test_set32(void);
15+
extern int test_set16(void);
16+
extern int test_set8(void);
17+
extern int test_set6(void);
18+
extern long test_sub64(void);
19+
extern int test_sub32(void);
20+
extern int test_sub16(void);
21+
extern int test_sub8(void);
22+
extern int test_sub6(void);
23+
24+
#ifdef CONFIG_AS_HAS_ULEB128
25+
extern int test_uleb_basic(void);
26+
extern int test_uleb_large(void);
27+
#endif
28+
29+
#define CHECK_EQ(lhs, rhs) KUNIT_ASSERT_EQ(test, lhs, rhs)
30+
31+
void run_test_set(struct kunit *test);
32+
void run_test_sub(struct kunit *test);
33+
void run_test_uleb(struct kunit *test);
34+
35+
void run_test_set(struct kunit *test)
36+
{
37+
int val32 = test_set32();
38+
int val16 = test_set16();
39+
int val8 = test_set8();
40+
int val6 = test_set6();
41+
42+
CHECK_EQ(val32, 0);
43+
CHECK_EQ(val16, 0);
44+
CHECK_EQ(val8, 0);
45+
CHECK_EQ(val6, 0);
46+
}
47+
48+
void run_test_sub(struct kunit *test)
49+
{
50+
int val64 = test_sub64();
51+
int val32 = test_sub32();
52+
int val16 = test_sub16();
53+
int val8 = test_sub8();
54+
int val6 = test_sub6();
55+
56+
CHECK_EQ(val64, 0);
57+
CHECK_EQ(val32, 0);
58+
CHECK_EQ(val16, 0);
59+
CHECK_EQ(val8, 0);
60+
CHECK_EQ(val6, 0);
61+
}
62+
63+
#ifdef CONFIG_AS_HAS_ULEB128
64+
void run_test_uleb(struct kunit *test)
65+
{
66+
int val_uleb = test_uleb_basic();
67+
int val_uleb2 = test_uleb_large();
68+
69+
CHECK_EQ(val_uleb, 0);
70+
CHECK_EQ(val_uleb2, 0);
71+
}
72+
#endif
73+
74+
static struct kunit_case __refdata riscv_module_linking_test_cases[] = {
75+
KUNIT_CASE(run_test_set),
76+
KUNIT_CASE(run_test_sub),
77+
#ifdef CONFIG_AS_HAS_ULEB128
78+
KUNIT_CASE(run_test_uleb),
79+
#endif
80+
{}
81+
};
82+
83+
static struct kunit_suite riscv_module_linking_test_suite = {
84+
.name = "riscv_checksum",
85+
.test_cases = riscv_module_linking_test_cases,
86+
};
87+
88+
kunit_test_suites(&riscv_module_linking_test_suite);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2023 Rivos Inc.
4+
*/
5+
6+
.text
7+
.global test_set16
8+
test_set16:
9+
lw a0, set16
10+
la t0, set16
11+
#ifdef CONFIG_32BIT
12+
slli t0, t0, 16
13+
srli t0, t0, 16
14+
#else
15+
slli t0, t0, 48
16+
srli t0, t0, 48
17+
#endif
18+
sub a0, a0, t0
19+
ret
20+
.data
21+
set16:
22+
.reloc set16, R_RISCV_SET16, set16
23+
.word 0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2023 Rivos Inc.
4+
*/
5+
6+
.text
7+
.global test_set32
8+
test_set32:
9+
lw a0, set32
10+
la t0, set32
11+
#ifndef CONFIG_32BIT
12+
slli t0, t0, 32
13+
srli t0, t0, 32
14+
#endif
15+
sub a0, a0, t0
16+
ret
17+
.data
18+
set32:
19+
.reloc set32, R_RISCV_SET32, set32
20+
.word 0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2023 Rivos Inc.
4+
*/
5+
6+
.text
7+
.global test_set6
8+
test_set6:
9+
lw a0, set6
10+
la t0, set6
11+
#ifdef CONFIG_32BIT
12+
slli t0, t0, 26
13+
srli t0, t0, 26
14+
#else
15+
slli t0, t0, 58
16+
srli t0, t0, 58
17+
#endif
18+
sub a0, a0, t0
19+
ret
20+
.data
21+
set6:
22+
.reloc set6, R_RISCV_SET6, set6
23+
.word 0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2023 Rivos Inc.
4+
*/
5+
6+
.text
7+
.global test_set8
8+
test_set8:
9+
lw a0, set8
10+
la t0, set8
11+
#ifdef CONFIG_32BIT
12+
slli t0, t0, 24
13+
srli t0, t0, 24
14+
#else
15+
slli t0, t0, 56
16+
srli t0, t0, 56
17+
#endif
18+
sub a0, a0, t0
19+
ret
20+
.data
21+
set8:
22+
.reloc set8, R_RISCV_SET8, set8
23+
.word 0

0 commit comments

Comments
 (0)