Skip to content

Commit 287dcc2

Browse files
evangreenpalmer-dabbelt
authored andcommitted
selftests: Test the new RISC-V hwprobe interface
This adds a test for the recently added RISC-V interface for probing hardware capabilities. It happens to be the first selftest we have for RISC-V, so I've added some infrastructure for those as well. Co-developed-by: Palmer Dabbelt <palmer@rivosinc.com> Signed-off-by: Evan Green <evan@rivosinc.com> Link: https://lore.kernel.org/r/20230407231103.2622178-6-evan@rivosinc.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 62a31d6 commit 287dcc2

5 files changed

Lines changed: 171 additions & 0 deletions

File tree

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ TARGETS += pstore
6363
TARGETS += ptrace
6464
TARGETS += openat2
6565
TARGETS += resctrl
66+
TARGETS += riscv
6667
TARGETS += rlimits
6768
TARGETS += rseq
6869
TARGETS += rtc
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# Originally tools/testing/arm64/Makefile
3+
4+
# When ARCH not overridden for crosscompiling, lookup machine
5+
ARCH ?= $(shell uname -m 2>/dev/null || echo not)
6+
7+
ifneq (,$(filter $(ARCH),riscv))
8+
RISCV_SUBTARGETS ?= hwprobe
9+
else
10+
RISCV_SUBTARGETS :=
11+
endif
12+
13+
CFLAGS := -Wall -O2 -g
14+
15+
# A proper top_srcdir is needed by KSFT(lib.mk)
16+
top_srcdir = $(realpath ../../../../)
17+
18+
# Additional include paths needed by kselftest.h and local headers
19+
CFLAGS += -I$(top_srcdir)/tools/testing/selftests/
20+
21+
CFLAGS += $(KHDR_INCLUDES)
22+
23+
export CFLAGS
24+
export top_srcdir
25+
26+
all:
27+
@for DIR in $(RISCV_SUBTARGETS); do \
28+
BUILD_TARGET=$(OUTPUT)/$$DIR; \
29+
mkdir -p $$BUILD_TARGET; \
30+
$(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \
31+
done
32+
33+
install: all
34+
@for DIR in $(RISCV_SUBTARGETS); do \
35+
BUILD_TARGET=$(OUTPUT)/$$DIR; \
36+
$(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \
37+
done
38+
39+
run_tests: all
40+
@for DIR in $(RISCV_SUBTARGETS); do \
41+
BUILD_TARGET=$(OUTPUT)/$$DIR; \
42+
$(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \
43+
done
44+
45+
# Avoid any output on non riscv on emit_tests
46+
emit_tests: all
47+
@for DIR in $(RISCV_SUBTARGETS); do \
48+
BUILD_TARGET=$(OUTPUT)/$$DIR; \
49+
$(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \
50+
done
51+
52+
clean:
53+
@for DIR in $(RISCV_SUBTARGETS); do \
54+
BUILD_TARGET=$(OUTPUT)/$$DIR; \
55+
$(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \
56+
done
57+
58+
.PHONY: all clean install run_tests emit_tests
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# Copyright (C) 2021 ARM Limited
3+
# Originally tools/testing/arm64/abi/Makefile
4+
5+
TEST_GEN_PROGS := hwprobe
6+
7+
include ../../lib.mk
8+
9+
$(OUTPUT)/hwprobe: hwprobe.c sys_hwprobe.S
10+
$(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <stddef.h>
3+
#include <asm/hwprobe.h>
4+
5+
/*
6+
* Rather than relying on having a new enough libc to define this, just do it
7+
* ourselves. This way we don't need to be coupled to a new-enough libc to
8+
* contain the call.
9+
*/
10+
long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
11+
size_t cpu_count, unsigned long *cpus, unsigned int flags);
12+
13+
int main(int argc, char **argv)
14+
{
15+
struct riscv_hwprobe pairs[8];
16+
unsigned long cpus;
17+
long out;
18+
19+
/* Fake the CPU_SET ops. */
20+
cpus = -1;
21+
22+
/*
23+
* Just run a basic test: pass enough pairs to get up to the base
24+
* behavior, and then check to make sure it's sane.
25+
*/
26+
for (long i = 0; i < 8; i++)
27+
pairs[i].key = i;
28+
out = riscv_hwprobe(pairs, 8, 1, &cpus, 0);
29+
if (out != 0)
30+
return -1;
31+
for (long i = 0; i < 4; ++i) {
32+
/* Fail if the kernel claims not to recognize a base key. */
33+
if ((i < 4) && (pairs[i].key != i))
34+
return -2;
35+
36+
if (pairs[i].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR)
37+
continue;
38+
39+
if (pairs[i].value & RISCV_HWPROBE_BASE_BEHAVIOR_IMA)
40+
continue;
41+
42+
return -3;
43+
}
44+
45+
/*
46+
* This should also work with a NULL CPU set, but should not work
47+
* with an improperly supplied CPU set.
48+
*/
49+
out = riscv_hwprobe(pairs, 8, 0, 0, 0);
50+
if (out != 0)
51+
return -4;
52+
53+
out = riscv_hwprobe(pairs, 8, 0, &cpus, 0);
54+
if (out == 0)
55+
return -5;
56+
57+
out = riscv_hwprobe(pairs, 8, 1, 0, 0);
58+
if (out == 0)
59+
return -6;
60+
61+
/*
62+
* Check that keys work by providing one that we know exists, and
63+
* checking to make sure the resultig pair is what we asked for.
64+
*/
65+
pairs[0].key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR;
66+
out = riscv_hwprobe(pairs, 1, 1, &cpus, 0);
67+
if (out != 0)
68+
return -7;
69+
if (pairs[0].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR)
70+
return -8;
71+
72+
/*
73+
* Check that an unknown key gets overwritten with -1,
74+
* but doesn't block elements after it.
75+
*/
76+
pairs[0].key = 0x5555;
77+
pairs[1].key = 1;
78+
pairs[1].value = 0xAAAA;
79+
out = riscv_hwprobe(pairs, 2, 0, 0, 0);
80+
if (out != 0)
81+
return -9;
82+
83+
if (pairs[0].key != -1)
84+
return -10;
85+
86+
if ((pairs[1].key != 1) || (pairs[1].value == 0xAAAA))
87+
return -11;
88+
89+
return 0;
90+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (C) 2023 Rivos, Inc */
3+
4+
.text
5+
.global riscv_hwprobe
6+
riscv_hwprobe:
7+
# Put __NR_riscv_hwprobe in the syscall number register, then just shim
8+
# back the kernel's return. This doesn't do any sort of errno
9+
# handling, the caller can deal with it.
10+
li a7, 258
11+
ecall
12+
ret

0 commit comments

Comments
 (0)