perf, riscv: Wire up perf trace support for RISC-V
authorBjörn Töpel <bjorn@rivosinc.com>
Thu, 24 Oct 2024 19:03:51 +0000 (12:03 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 31 Oct 2024 06:39:34 +0000 (23:39 -0700)
RISC-V does not currently support perf trace, since the system call
table is not generated.

Perform the copy/paste exercise, wiring up RISC-V system call table
generation.

Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
Tested-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Anup Patel <anup@brainfault.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: linux-riscv@lists.infradead.org
Cc: Atish Patra <atishp@rivosinc.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Link: https://lore.kernel.org/r/20241024190353.46737-1-bjorn@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/Makefile.config
tools/perf/arch/riscv/Makefile
tools/perf/arch/riscv/entry/syscalls/mksyscalltbl [new file with mode: 0755]
tools/perf/util/syscalltbl.c

index b93ed2b..854f84e 100644 (file)
@@ -31,7 +31,7 @@ $(call detected_var,SRCARCH)
 ifneq ($(NO_SYSCALL_TABLE),1)
   NO_SYSCALL_TABLE := 1
 
-  ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 powerpc arm64 s390 mips loongarch))
+  ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 powerpc arm64 s390 mips loongarch riscv))
     NO_SYSCALL_TABLE := 0
   endif
 
@@ -83,6 +83,10 @@ ifeq ($(ARCH),mips)
   LIBUNWIND_LIBS = -lunwind -lunwind-mips
 endif
 
+ifeq ($(ARCH),riscv)
+  CFLAGS += -I$(OUTPUT)arch/riscv/include/generated
+endif
+
 # So far there's only x86 and arm libdw unwind support merged in perf.
 # Disable it on all other architectures in case libdw unwind
 # support is detected in system. Add supported architectures
index 4664a78..00573af 100644 (file)
@@ -4,3 +4,25 @@ endif
 PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET := 1
 PERF_HAVE_JITDUMP := 1
 HAVE_KVM_STAT_SUPPORT := 1
+
+#
+# Syscall table generation for perf
+#
+
+out    := $(OUTPUT)arch/riscv/include/generated/asm
+header := $(out)/syscalls.c
+incpath := $(srctree)/tools
+sysdef := $(srctree)/tools/arch/riscv/include/uapi/asm/unistd.h
+sysprf := $(srctree)/tools/perf/arch/riscv/entry/syscalls/
+systbl := $(sysprf)/mksyscalltbl
+
+# Create output directory if not already present
+$(shell [ -d '$(out)' ] || mkdir -p '$(out)')
+
+$(header): $(sysdef) $(systbl)
+       $(Q)$(SHELL) '$(systbl)' '$(CC)' '$(HOSTCC)' $(incpath) $(sysdef) > $@
+
+clean::
+       $(call QUIET_CLEAN, riscv) $(RM) $(header)
+
+archheaders: $(header)
diff --git a/tools/perf/arch/riscv/entry/syscalls/mksyscalltbl b/tools/perf/arch/riscv/entry/syscalls/mksyscalltbl
new file mode 100755 (executable)
index 0000000..c59f5e8
--- /dev/null
@@ -0,0 +1,47 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Generate system call table for perf. Derived from
+# powerpc script.
+#
+# Copyright IBM Corp. 2017
+# Author(s):  Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
+# Changed by: Ravi Bangoria <ravi.bangoria@linux.vnet.ibm.com>
+# Changed by: Kim Phillips <kim.phillips@arm.com>
+# Changed by: Björn Töpel <bjorn@rivosinc.com>
+
+gcc=$1
+hostcc=$2
+incpath=$3
+input=$4
+
+if ! test -r $input; then
+       echo "Could not read input file" >&2
+       exit 1
+fi
+
+create_sc_table()
+{
+       local sc nr max_nr
+
+       while read sc nr; do
+               printf "%s\n" " [$nr] = \"$sc\","
+               max_nr=$nr
+       done
+
+       echo "#define SYSCALLTBL_RISCV_MAX_ID $max_nr"
+}
+
+create_table()
+{
+       echo "#include \"$input\""
+       echo "static const char *const syscalltbl_riscv[] = {"
+       create_sc_table
+       echo "};"
+}
+
+$gcc -E -dM -x c -I $incpath/include/uapi $input \
+       |awk '$2 ~ "__NR" && $3 !~ "__NR3264_" {
+               sub("^#define __NR(3264)?_", "");
+               print | "sort -k2 -n"}' \
+       |create_table
index 7c15dec..349986f 100644 (file)
@@ -46,6 +46,10 @@ static const char *const *syscalltbl_native = syscalltbl_mips_n64;
 #include <asm/syscalls.c>
 const int syscalltbl_native_max_id = SYSCALLTBL_LOONGARCH_MAX_ID;
 static const char *const *syscalltbl_native = syscalltbl_loongarch;
+#elif defined(__riscv)
+#include <asm/syscalls.c>
+const int syscalltbl_native_max_id = SYSCALLTBL_RISCV_MAX_ID;
+static const char *const *syscalltbl_native = syscalltbl_riscv;
 #endif
 
 struct syscall {