[PATCH v1 16/23] perf dwarf-regs: Add loongarch perf to dwarf register number mapping functions

Ian Rogers
2026-01-17T05:29:49Z

These functions allow the generic initial register state code in
unwind-libdw to be used.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/dwarf-regs-arch/Build | 1 +
.../dwarf-regs-arch/dwarf-regs-loongarch.c | 12 ++++
tools/perf/util/dwarf-regs.c | 3 +
tools/perf/util/include/dwarf-regs.h | 1 +
tools/perf/util/unwind-libdw-arch/Build | 1 -
.../unwind-libdw-loongarch.c | 57 -------------------
tools/perf/util/unwind-libdw.c | 5 +-
tools/perf/util/unwind-libdw.h | 1 -
8 files changed, 18 insertions(+), 63 deletions(-)
create mode 100644 tools/perf/util/dwarf-regs-arch/dwarf-regs-loongarch.c
delete mode 100644 tools/perf/util/unwind-libdw-arch/unwind-libdw-loongarch.c

diff --git a/tools/perf/util/dwarf-regs-arch/Build b/tools/perf/util/dwarf-regs-arch/Build
index 3f19a9ec47c7..188359376ea5 100644
--- a/tools/perf/util/dwarf-regs-arch/Build
+++ b/tools/perf/util/dwarf-regs-arch/Build
@@ -1,5 +1,6 @@
perf-util-$(CONFIG_LIBDW) += dwarf-regs-arm64.o
perf-util-$(CONFIG_LIBDW) += dwarf-regs-arm.o
perf-util-$(CONFIG_LIBDW) += dwarf-regs-csky.o
+perf-util-$(CONFIG_LIBDW) += dwarf-regs-loongarch.o
perf-util-$(CONFIG_LIBDW) += dwarf-regs-powerpc.o
perf-util-$(CONFIG_LIBDW) += dwarf-regs-x86.o
diff --git a/tools/perf/util/dwarf-regs-arch/dwarf-regs-loongarch.c b/tools/perf/util/dwarf-regs-arch/dwarf-regs-loongarch.c
new file mode 100644
index 000000000000..203077b740a0
--- /dev/null
+++ b/tools/perf/util/dwarf-regs-arch/dwarf-regs-loongarch.c
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <dwarf-regs.h>
+#include "../../../arch/loongarch/include/uapi/asm/perf_regs.h"
+
+int __get_dwarf_regnum_for_perf_regnum_loongarch(int perf_regnum)
+{
+ if (perf_regnum < 0 || perf_regnum >= PERF_REG_LOONGARCH_MAX)
+ return -ENOENT;
+
+ return perf_regnum;
+}
diff --git a/tools/perf/util/dwarf-regs.c b/tools/perf/util/dwarf-regs.c
index 7fa0930fd298..033218f14b36 100644
--- a/tools/perf/util/dwarf-regs.c
+++ b/tools/perf/util/dwarf-regs.c
@@ -205,6 +205,9 @@ int get_dwarf_regnum_for_perf_regnum(int perf_regnum, unsigned int machine,
case EM_CSKY:
reg = __get_dwarf_regnum_for_perf_regnum_csky(perf_regnum, flags);
break;
+ case EM_LOONGARCH:
+ reg = __get_dwarf_regnum_for_perf_regnum_loongarch(perf_regnum);
+ break;
default:
pr_err("ELF MACHINE %x is not supported.\n", machine);
return -ENOENT;
diff --git a/tools/perf/util/include/dwarf-regs.h b/tools/perf/util/include/dwarf-regs.h
index 7780bc07e70e..bec15fb53e73 100644
--- a/tools/perf/util/include/dwarf-regs.h
+++ b/tools/perf/util/include/dwarf-regs.h
@@ -109,6 +109,7 @@ int __get_dwarf_regnum_for_perf_regnum_arm(int perf_regnum);
int __get_dwarf_regnum_for_perf_regnum_arm64(int perf_regnum);

int __get_dwarf_regnum_for_perf_regnum_csky(int perf_regnum, unsigned int flags);
+int __get_dwarf_regnum_for_perf_regnum_loongarch(int perf_regnum);

/*
* get_dwarf_regnum - Returns DWARF regnum from register name
diff --git a/tools/perf/util/unwind-libdw-arch/Build b/tools/perf/util/unwind-libdw-arch/Build
index 5fa1754fca8d..62a4cbf2dca8 100644
--- a/tools/perf/util/unwind-libdw-arch/Build
+++ b/tools/perf/util/unwind-libdw-arch/Build
@@ -1,4 +1,3 @@
-perf-util-y += unwind-libdw-loongarch.o
perf-util-y += unwind-libdw-powerpc.o
perf-util-y += unwind-libdw-riscv.o
perf-util-y += unwind-libdw-s390.o
diff --git a/tools/perf/util/unwind-libdw-arch/unwind-libdw-loongarch.c b/tools/perf/util/unwind-libdw-arch/unwind-libdw-loongarch.c
deleted file mode 100644
index 5fca673508be..000000000000
--- a/tools/perf/util/unwind-libdw-arch/unwind-libdw-loongarch.c
+++ /dev/null
@@ -1,57 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/* Copyright (C) 2020-2023 Loongson Technology Corporation Limited */
-
-#include <elfutils/libdwfl.h>
-#include "../arch/loongarch/include/uapi/asm/perf_regs.h"
-#include "util/unwind-libdw.h"
-#include "util/perf_regs.h"
-#include "util/sample.h"
-
-bool libdw_set_initial_registers_loongarch(Dwfl_Thread *thread, void *arg)
-{
- struct unwind_info *ui = arg;
- struct regs_dump *user_regs = perf_sample__user_regs(ui->sample);
- Dwarf_Word dwarf_regs[PERF_REG_LOONGARCH_MAX];
-
-#define REG(r) ({ \
- Dwarf_Word val = 0; \
- perf_reg_value(&val, user_regs, PERF_REG_LOONGARCH_##r); \
- val; \
-})
-
- dwarf_regs[0] = 0;
- dwarf_regs[1] = REG(R1);
- dwarf_regs[2] = REG(R2);
- dwarf_regs[3] = REG(R3);
- dwarf_regs[4] = REG(R4);
- dwarf_regs[5] = REG(R5);
- dwarf_regs[6] = REG(R6);
- dwarf_regs[7] = REG(R7);
- dwarf_regs[8] = REG(R8);
- dwarf_regs[9] = REG(R9);
- dwarf_regs[10] = REG(R10);
- dwarf_regs[11] = REG(R11);
- dwarf_regs[12] = REG(R12);
- dwarf_regs[13] = REG(R13);
- dwarf_regs[14] = REG(R14);
- dwarf_regs[15] = REG(R15);
- dwarf_regs[16] = REG(R16);
- dwarf_regs[17] = REG(R17);
- dwarf_regs[18] = REG(R18);
- dwarf_regs[19] = REG(R19);
- dwarf_regs[20] = REG(R20);
- dwarf_regs[21] = REG(R21);
- dwarf_regs[22] = REG(R22);
- dwarf_regs[23] = REG(R23);
- dwarf_regs[24] = REG(R24);
- dwarf_regs[25] = REG(R25);
- dwarf_regs[26] = REG(R26);
- dwarf_regs[27] = REG(R27);
- dwarf_regs[28] = REG(R28);
- dwarf_regs[29] = REG(R29);
- dwarf_regs[30] = REG(R30);
- dwarf_regs[31] = REG(R31);
- dwfl_thread_state_register_pc(thread, REG(PC));
-
- return dwfl_thread_state_registers(thread, 0, PERF_REG_LOONGARCH_MAX, dwarf_regs);
-}
diff --git a/tools/perf/util/unwind-libdw.c b/tools/perf/util/unwind-libdw.c
index a193163da707..9c8dad643cd0 100644
--- a/tools/perf/util/unwind-libdw.c
+++ b/tools/perf/util/unwind-libdw.c
@@ -292,16 +292,13 @@ static const Dwfl_Thread_Callbacks callbacks_generic = {
.set_initial_registers = libdw_set_initial_registers_generic,
};

-DEFINE_DWFL_THREAD_CALLBACKS(loongarch);
DEFINE_DWFL_THREAD_CALLBACKS(powerpc);
DEFINE_DWFL_THREAD_CALLBACKS(riscv);
DEFINE_DWFL_THREAD_CALLBACKS(s390);

static const Dwfl_Thread_Callbacks *get_thread_callbacks(const char *arch)
{
- if (!strcmp(arch, "loongarch"))
- return &callbacks_loongarch;
- else if (!strcmp(arch, "powerpc"))
+ if (!strcmp(arch, "powerpc"))
return &callbacks_powerpc;
else if (!strcmp(arch, "riscv"))
return &callbacks_riscv;
diff --git a/tools/perf/util/unwind-libdw.h b/tools/perf/util/unwind-libdw.h
index ee56f1e827e5..9d177d70f15c 100644
--- a/tools/perf/util/unwind-libdw.h
+++ b/tools/perf/util/unwind-libdw.h
@@ -9,7 +9,6 @@ struct machine;
struct perf_sample;
struct thread;

-bool libdw_set_initial_registers_loongarch(Dwfl_Thread *thread, void *arg);
bool libdw_set_initial_registers_mips(Dwfl_Thread *thread, void *arg);
bool libdw_set_initial_registers_powerpc(Dwfl_Thread *thread, void *arg);
bool libdw_set_initial_registers_riscv(Dwfl_Thread *thread, void *arg);
--
2.52.0.457.g6b5491de43-goog