Skip to content

Commit 2fa5ebe

Browse files
committed
tools/perf: Add basic support for LoongArch
Add basic support for LoongArch, which is very similar to the MIPS version. Signed-off-by: Ming Wang <wangming01@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent 22f367a commit 2fa5ebe

21 files changed

Lines changed: 518 additions & 3 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
#ifndef _ASM_LOONGARCH_PERF_REGS_H
3+
#define _ASM_LOONGARCH_PERF_REGS_H
4+
5+
enum perf_event_loongarch_regs {
6+
PERF_REG_LOONGARCH_PC,
7+
PERF_REG_LOONGARCH_R1,
8+
PERF_REG_LOONGARCH_R2,
9+
PERF_REG_LOONGARCH_R3,
10+
PERF_REG_LOONGARCH_R4,
11+
PERF_REG_LOONGARCH_R5,
12+
PERF_REG_LOONGARCH_R6,
13+
PERF_REG_LOONGARCH_R7,
14+
PERF_REG_LOONGARCH_R8,
15+
PERF_REG_LOONGARCH_R9,
16+
PERF_REG_LOONGARCH_R10,
17+
PERF_REG_LOONGARCH_R11,
18+
PERF_REG_LOONGARCH_R12,
19+
PERF_REG_LOONGARCH_R13,
20+
PERF_REG_LOONGARCH_R14,
21+
PERF_REG_LOONGARCH_R15,
22+
PERF_REG_LOONGARCH_R16,
23+
PERF_REG_LOONGARCH_R17,
24+
PERF_REG_LOONGARCH_R18,
25+
PERF_REG_LOONGARCH_R19,
26+
PERF_REG_LOONGARCH_R20,
27+
PERF_REG_LOONGARCH_R21,
28+
PERF_REG_LOONGARCH_R22,
29+
PERF_REG_LOONGARCH_R23,
30+
PERF_REG_LOONGARCH_R24,
31+
PERF_REG_LOONGARCH_R25,
32+
PERF_REG_LOONGARCH_R26,
33+
PERF_REG_LOONGARCH_R27,
34+
PERF_REG_LOONGARCH_R28,
35+
PERF_REG_LOONGARCH_R29,
36+
PERF_REG_LOONGARCH_R30,
37+
PERF_REG_LOONGARCH_R31,
38+
PERF_REG_LOONGARCH_MAX,
39+
};
40+
#endif /* _ASM_LOONGARCH_PERF_REGS_H */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
/*
3+
* Copyright (C) 2020-2023 Loongson Technology Corporation Limited
4+
*/
5+
6+
#define __ARCH_WANT_SYS_CLONE
7+
#define __ARCH_WANT_SYS_CLONE3
8+
9+
#include <asm-generic/unistd.h>

tools/perf/Makefile.config

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ifneq ($(NO_SYSCALL_TABLE),1)
3838
NO_SYSCALL_TABLE := 0
3939
endif
4040
else
41-
ifeq ($(SRCARCH),$(filter $(SRCARCH),powerpc arm64 s390 mips))
41+
ifeq ($(SRCARCH),$(filter $(SRCARCH),powerpc arm64 s390 mips loongarch))
4242
NO_SYSCALL_TABLE := 0
4343
endif
4444
endif
@@ -80,6 +80,12 @@ ifeq ($(SRCARCH),arm64)
8080
LIBUNWIND_LIBS = -lunwind -lunwind-aarch64
8181
endif
8282

83+
ifeq ($(SRCARCH),loongarch)
84+
NO_PERF_REGS := 0
85+
CFLAGS += -I$(OUTPUT)arch/loongarch/include/generated
86+
LIBUNWIND_LIBS = -lunwind -lunwind-loongarch64
87+
endif
88+
8389
ifeq ($(SRCARCH),riscv)
8490
NO_PERF_REGS := 0
8591
endif
@@ -107,7 +113,7 @@ endif
107113
# Disable it on all other architectures in case libdw unwind
108114
# support is detected in system. Add supported architectures
109115
# to the check.
110-
ifneq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc s390 csky riscv))
116+
ifneq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc s390 csky riscv loongarch))
111117
NO_LIBDW_DWARF_UNWIND := 1
112118
endif
113119

@@ -129,7 +135,7 @@ endef
129135
ifdef LIBUNWIND_DIR
130136
LIBUNWIND_CFLAGS = -I$(LIBUNWIND_DIR)/include
131137
LIBUNWIND_LDFLAGS = -L$(LIBUNWIND_DIR)/lib
132-
LIBUNWIND_ARCHS = x86 x86_64 arm aarch64 debug-frame-arm debug-frame-aarch64
138+
LIBUNWIND_ARCHS = x86 x86_64 arm aarch64 debug-frame-arm debug-frame-aarch64 loongarch
133139
$(foreach libunwind_arch,$(LIBUNWIND_ARCHS),$(call libunwind_arch_set_flags,$(libunwind_arch)))
134140
endif
135141

tools/perf/arch/loongarch/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
perf-y += util/

tools/perf/arch/loongarch/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
ifndef NO_DWARF
3+
PERF_HAVE_DWARF_REGS := 1
4+
endif
5+
PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET := 1
6+
PERF_HAVE_JITDUMP := 1
7+
8+
#
9+
# Syscall table generation for perf
10+
#
11+
12+
out := $(OUTPUT)arch/loongarch/include/generated/asm
13+
header := $(out)/syscalls.c
14+
incpath := $(srctree)/tools
15+
sysdef := $(srctree)/tools/arch/loongarch/include/uapi/asm/unistd.h
16+
sysprf := $(srctree)/tools/perf/arch/loongarch/entry/syscalls/
17+
systbl := $(sysprf)/mksyscalltbl
18+
19+
# Create output directory if not already present
20+
_dummy := $(shell [ -d '$(out)' ] || mkdir -p '$(out)')
21+
22+
$(header): $(sysdef) $(systbl)
23+
$(Q)$(SHELL) '$(systbl)' '$(CC)' '$(HOSTCC)' $(incpath) $(sysdef) > $@
24+
25+
clean::
26+
$(call QUIET_CLEAN, loongarch) $(RM) $(header)
27+
28+
archheaders: $(header)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Perf annotate functions.
4+
*
5+
* Copyright (C) 2020-2023 Loongson Technology Corporation Limited
6+
*/
7+
8+
static
9+
struct ins_ops *loongarch__associate_ins_ops(struct arch *arch, const char *name)
10+
{
11+
struct ins_ops *ops = NULL;
12+
13+
if (!strncmp(name, "beqz", 4) ||
14+
!strncmp(name, "bnez", 4) ||
15+
!strncmp(name, "beq", 3) ||
16+
!strncmp(name, "bne", 3) ||
17+
!strncmp(name, "blt", 3) ||
18+
!strncmp(name, "bge", 3) ||
19+
!strncmp(name, "bltu", 4) ||
20+
!strncmp(name, "bgeu", 4) ||
21+
!strncmp(name, "bl", 2))
22+
ops = &call_ops;
23+
else if (!strncmp(name, "jirl", 4))
24+
ops = &ret_ops;
25+
else if (name[0] == 'b')
26+
ops = &jump_ops;
27+
else
28+
return NULL;
29+
30+
arch__associate_ins_ops(arch, name, ops);
31+
32+
return ops;
33+
}
34+
35+
static
36+
int loongarch__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
37+
{
38+
if (!arch->initialized) {
39+
arch->associate_instruction_ops = loongarch__associate_ins_ops;
40+
arch->initialized = true;
41+
arch->objdump.comment_char = '#';
42+
}
43+
44+
return 0;
45+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Generate system call table for perf. Derived from
5+
# powerpc script.
6+
#
7+
# Author(s): Ming Wang <wangming01@loongson.cn>
8+
# Author(s): Huacai Chen <chenhuacai@loongson.cn>
9+
# Copyright (C) 2020-2023 Loongson Technology Corporation Limited
10+
11+
gcc=$1
12+
hostcc=$2
13+
incpath=$3
14+
input=$4
15+
16+
if ! test -r $input; then
17+
echo "Could not read input file" >&2
18+
exit 1
19+
fi
20+
21+
create_table_from_c()
22+
{
23+
local sc nr last_sc
24+
25+
create_table_exe=`mktemp ${TMPDIR:-/tmp}/create-table-XXXXXX`
26+
27+
{
28+
29+
cat <<-_EoHEADER
30+
#include <stdio.h>
31+
#include "$input"
32+
int main(int argc, char *argv[])
33+
{
34+
_EoHEADER
35+
36+
while read sc nr; do
37+
printf "%s\n" " printf(\"\\t[%d] = \\\"$sc\\\",\\n\", $nr);"
38+
last_sc=$nr
39+
done
40+
41+
printf "%s\n" " printf(\"#define SYSCALLTBL_LOONGARCH_MAX_ID %d\\n\", $last_sc);"
42+
printf "}\n"
43+
44+
} | $hostcc -I $incpath/include/uapi -o $create_table_exe -x c -
45+
46+
$create_table_exe
47+
48+
rm -f $create_table_exe
49+
}
50+
51+
create_table()
52+
{
53+
echo "static const char *syscalltbl_loongarch[] = {"
54+
create_table_from_c
55+
echo "};"
56+
}
57+
58+
$gcc -E -dM -x c -I $incpath/include/uapi $input \
59+
|sed -ne 's/^#define __NR_//p' \
60+
|sort -t' ' -k2 -n \
61+
|create_table
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* dwarf-regs-table.h : Mapping of DWARF debug register numbers into
4+
* register names.
5+
*
6+
* Copyright (C) 2020-2023 Loongson Technology Corporation Limited
7+
*/
8+
9+
#ifdef DEFINE_DWARF_REGSTR_TABLE
10+
static const char * const loongarch_regstr_tbl[] = {
11+
"%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
12+
"%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
13+
"%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
14+
"%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
15+
};
16+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef ARCH_PERF_REGS_H
3+
#define ARCH_PERF_REGS_H
4+
5+
#include <stdlib.h>
6+
#include <linux/types.h>
7+
#include <asm/perf_regs.h>
8+
9+
#define PERF_REGS_MAX PERF_REG_LOONGARCH_MAX
10+
#define PERF_REG_IP PERF_REG_LOONGARCH_PC
11+
#define PERF_REG_SP PERF_REG_LOONGARCH_R3
12+
13+
#define PERF_REGS_MASK ((1ULL << PERF_REG_LOONGARCH_MAX) - 1)
14+
15+
#endif /* ARCH_PERF_REGS_H */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
perf-y += perf_regs.o
2+
3+
perf-$(CONFIG_DWARF) += dwarf-regs.o
4+
perf-$(CONFIG_LOCAL_LIBUNWIND) += unwind-libunwind.o
5+
perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o

0 commit comments

Comments
 (0)