Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

riscv: Add support for libdw

This patch adds support for DWARF register mappings and libdw registers
initialization, which is used by perf callchain analyzing when
--call-graph=dwarf is given.

Signed-off-by: Mao Han <han_mao@c-sky.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Greentime Hu <green.hu@gmail.com>
Cc: Palmer Dabbelt <palmer@sifive.com>
Cc: linux-riscv <linux-riscv@lists.infradead.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Guo Ren <guoren@kernel.org>
Tested-by: Greentime Hu <greentime.hu@sifive.com>
Signed-off-by: Paul Walmsley <paul.walmsley@sifive.com>

authored by

Mao Han and committed by
Paul Walmsley
51bc620b 98a93b0b

+279 -1
+42
tools/arch/riscv/include/uapi/asm/perf_regs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 + /* Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. */ 3 + 4 + #ifndef _ASM_RISCV_PERF_REGS_H 5 + #define _ASM_RISCV_PERF_REGS_H 6 + 7 + enum perf_event_riscv_regs { 8 + PERF_REG_RISCV_PC, 9 + PERF_REG_RISCV_RA, 10 + PERF_REG_RISCV_SP, 11 + PERF_REG_RISCV_GP, 12 + PERF_REG_RISCV_TP, 13 + PERF_REG_RISCV_T0, 14 + PERF_REG_RISCV_T1, 15 + PERF_REG_RISCV_T2, 16 + PERF_REG_RISCV_S0, 17 + PERF_REG_RISCV_S1, 18 + PERF_REG_RISCV_A0, 19 + PERF_REG_RISCV_A1, 20 + PERF_REG_RISCV_A2, 21 + PERF_REG_RISCV_A3, 22 + PERF_REG_RISCV_A4, 23 + PERF_REG_RISCV_A5, 24 + PERF_REG_RISCV_A6, 25 + PERF_REG_RISCV_A7, 26 + PERF_REG_RISCV_S2, 27 + PERF_REG_RISCV_S3, 28 + PERF_REG_RISCV_S4, 29 + PERF_REG_RISCV_S5, 30 + PERF_REG_RISCV_S6, 31 + PERF_REG_RISCV_S7, 32 + PERF_REG_RISCV_S8, 33 + PERF_REG_RISCV_S9, 34 + PERF_REG_RISCV_S10, 35 + PERF_REG_RISCV_S11, 36 + PERF_REG_RISCV_T3, 37 + PERF_REG_RISCV_T4, 38 + PERF_REG_RISCV_T5, 39 + PERF_REG_RISCV_T6, 40 + PERF_REG_RISCV_MAX, 41 + }; 42 + #endif /* _ASM_RISCV_PERF_REGS_H */
+5 -1
tools/perf/Makefile.config
··· 60 60 LIBUNWIND_LIBS = -lunwind -lunwind-aarch64 61 61 endif 62 62 63 + ifeq ($(SRCARCH),riscv) 64 + NO_PERF_REGS := 0 65 + endif 66 + 63 67 ifeq ($(SRCARCH),csky) 64 68 NO_PERF_REGS := 0 65 69 endif ··· 86 82 # Disable it on all other architectures in case libdw unwind 87 83 # support is detected in system. Add supported architectures 88 84 # to the check. 89 - ifneq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc s390 csky)) 85 + ifneq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc s390 csky riscv)) 90 86 NO_LIBDW_DWARF_UNWIND := 1 91 87 endif 92 88
+1
tools/perf/arch/riscv/Build
··· 1 + perf-y += util/
+4
tools/perf/arch/riscv/Makefile
··· 1 + ifndef NO_DWARF 2 + PERF_HAVE_DWARF_REGS := 1 3 + endif 4 + PERF_HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET := 1
+96
tools/perf/arch/riscv/include/perf_regs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. */ 3 + 4 + #ifndef ARCH_PERF_REGS_H 5 + #define ARCH_PERF_REGS_H 6 + 7 + #include <stdlib.h> 8 + #include <linux/types.h> 9 + #include <asm/perf_regs.h> 10 + 11 + #define PERF_REGS_MASK ((1ULL << PERF_REG_RISCV_MAX) - 1) 12 + #define PERF_REGS_MAX PERF_REG_RISCV_MAX 13 + #if __riscv_xlen == 64 14 + #define PERF_SAMPLE_REGS_ABI PERF_SAMPLE_REGS_ABI_64 15 + #else 16 + #define PERF_SAMPLE_REGS_ABI PERF_SAMPLE_REGS_ABI_32 17 + #endif 18 + 19 + #define PERF_REG_IP PERF_REG_RISCV_PC 20 + #define PERF_REG_SP PERF_REG_RISCV_SP 21 + 22 + static inline const char *perf_reg_name(int id) 23 + { 24 + switch (id) { 25 + case PERF_REG_RISCV_PC: 26 + return "pc"; 27 + case PERF_REG_RISCV_RA: 28 + return "ra"; 29 + case PERF_REG_RISCV_SP: 30 + return "sp"; 31 + case PERF_REG_RISCV_GP: 32 + return "gp"; 33 + case PERF_REG_RISCV_TP: 34 + return "tp"; 35 + case PERF_REG_RISCV_T0: 36 + return "t0"; 37 + case PERF_REG_RISCV_T1: 38 + return "t1"; 39 + case PERF_REG_RISCV_T2: 40 + return "t2"; 41 + case PERF_REG_RISCV_S0: 42 + return "s0"; 43 + case PERF_REG_RISCV_S1: 44 + return "s1"; 45 + case PERF_REG_RISCV_A0: 46 + return "a0"; 47 + case PERF_REG_RISCV_A1: 48 + return "a1"; 49 + case PERF_REG_RISCV_A2: 50 + return "a2"; 51 + case PERF_REG_RISCV_A3: 52 + return "a3"; 53 + case PERF_REG_RISCV_A4: 54 + return "a4"; 55 + case PERF_REG_RISCV_A5: 56 + return "a5"; 57 + case PERF_REG_RISCV_A6: 58 + return "a6"; 59 + case PERF_REG_RISCV_A7: 60 + return "a7"; 61 + case PERF_REG_RISCV_S2: 62 + return "s2"; 63 + case PERF_REG_RISCV_S3: 64 + return "s3"; 65 + case PERF_REG_RISCV_S4: 66 + return "s4"; 67 + case PERF_REG_RISCV_S5: 68 + return "s5"; 69 + case PERF_REG_RISCV_S6: 70 + return "s6"; 71 + case PERF_REG_RISCV_S7: 72 + return "s7"; 73 + case PERF_REG_RISCV_S8: 74 + return "s8"; 75 + case PERF_REG_RISCV_S9: 76 + return "s9"; 77 + case PERF_REG_RISCV_S10: 78 + return "s10"; 79 + case PERF_REG_RISCV_S11: 80 + return "s11"; 81 + case PERF_REG_RISCV_T3: 82 + return "t3"; 83 + case PERF_REG_RISCV_T4: 84 + return "t4"; 85 + case PERF_REG_RISCV_T5: 86 + return "t5"; 87 + case PERF_REG_RISCV_T6: 88 + return "t6"; 89 + default: 90 + return NULL; 91 + } 92 + 93 + return NULL; 94 + } 95 + 96 + #endif /* ARCH_PERF_REGS_H */
+2
tools/perf/arch/riscv/util/Build
··· 1 + perf-$(CONFIG_DWARF) += dwarf-regs.o 2 + perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
+72
tools/perf/arch/riscv/util/dwarf-regs.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. 4 + * Mapping of DWARF debug register numbers into register names. 5 + */ 6 + 7 + #include <stddef.h> 8 + #include <errno.h> /* for EINVAL */ 9 + #include <string.h> /* for strcmp */ 10 + #include <dwarf-regs.h> 11 + 12 + struct pt_regs_dwarfnum { 13 + const char *name; 14 + unsigned int dwarfnum; 15 + }; 16 + 17 + #define REG_DWARFNUM_NAME(r, num) {.name = r, .dwarfnum = num} 18 + #define REG_DWARFNUM_END {.name = NULL, .dwarfnum = 0} 19 + 20 + struct pt_regs_dwarfnum riscv_dwarf_regs_table[] = { 21 + REG_DWARFNUM_NAME("%zero", 0), 22 + REG_DWARFNUM_NAME("%ra", 1), 23 + REG_DWARFNUM_NAME("%sp", 2), 24 + REG_DWARFNUM_NAME("%gp", 3), 25 + REG_DWARFNUM_NAME("%tp", 4), 26 + REG_DWARFNUM_NAME("%t0", 5), 27 + REG_DWARFNUM_NAME("%t1", 6), 28 + REG_DWARFNUM_NAME("%t2", 7), 29 + REG_DWARFNUM_NAME("%s0", 8), 30 + REG_DWARFNUM_NAME("%s1", 9), 31 + REG_DWARFNUM_NAME("%a0", 10), 32 + REG_DWARFNUM_NAME("%a1", 11), 33 + REG_DWARFNUM_NAME("%a2", 12), 34 + REG_DWARFNUM_NAME("%a3", 13), 35 + REG_DWARFNUM_NAME("%a4", 14), 36 + REG_DWARFNUM_NAME("%a5", 15), 37 + REG_DWARFNUM_NAME("%a6", 16), 38 + REG_DWARFNUM_NAME("%a7", 17), 39 + REG_DWARFNUM_NAME("%s2", 18), 40 + REG_DWARFNUM_NAME("%s3", 19), 41 + REG_DWARFNUM_NAME("%s4", 20), 42 + REG_DWARFNUM_NAME("%s5", 21), 43 + REG_DWARFNUM_NAME("%s6", 22), 44 + REG_DWARFNUM_NAME("%s7", 23), 45 + REG_DWARFNUM_NAME("%s8", 24), 46 + REG_DWARFNUM_NAME("%s9", 25), 47 + REG_DWARFNUM_NAME("%s10", 26), 48 + REG_DWARFNUM_NAME("%s11", 27), 49 + REG_DWARFNUM_NAME("%t3", 28), 50 + REG_DWARFNUM_NAME("%t4", 29), 51 + REG_DWARFNUM_NAME("%t5", 30), 52 + REG_DWARFNUM_NAME("%t6", 31), 53 + REG_DWARFNUM_END, 54 + }; 55 + 56 + #define RISCV_MAX_REGS ((sizeof(riscv_dwarf_regs_table) / \ 57 + sizeof(riscv_dwarf_regs_table[0])) - 1) 58 + 59 + const char *get_arch_regstr(unsigned int n) 60 + { 61 + return (n < RISCV_MAX_REGS) ? riscv_dwarf_regs_table[n].name : NULL; 62 + } 63 + 64 + int regs_query_register_offset(const char *name) 65 + { 66 + const struct pt_regs_dwarfnum *roff; 67 + 68 + for (roff = riscv_dwarf_regs_table; roff->name; roff++) 69 + if (!strcmp(roff->name, name)) 70 + return roff->dwarfnum; 71 + return -EINVAL; 72 + }
+57
tools/perf/arch/riscv/util/unwind-libdw.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. */ 3 + 4 + #include <elfutils/libdwfl.h> 5 + #include "../../util/unwind-libdw.h" 6 + #include "../../util/perf_regs.h" 7 + #include "../../util/event.h" 8 + 9 + bool libdw__arch_set_initial_registers(Dwfl_Thread *thread, void *arg) 10 + { 11 + struct unwind_info *ui = arg; 12 + struct regs_dump *user_regs = &ui->sample->user_regs; 13 + Dwarf_Word dwarf_regs[32]; 14 + 15 + #define REG(r) ({ \ 16 + Dwarf_Word val = 0; \ 17 + perf_reg_value(&val, user_regs, PERF_REG_RISCV_##r); \ 18 + val; \ 19 + }) 20 + 21 + dwarf_regs[0] = 0; 22 + dwarf_regs[1] = REG(RA); 23 + dwarf_regs[2] = REG(SP); 24 + dwarf_regs[3] = REG(GP); 25 + dwarf_regs[4] = REG(TP); 26 + dwarf_regs[5] = REG(T0); 27 + dwarf_regs[6] = REG(T1); 28 + dwarf_regs[7] = REG(T2); 29 + dwarf_regs[8] = REG(S0); 30 + dwarf_regs[9] = REG(S1); 31 + dwarf_regs[10] = REG(A0); 32 + dwarf_regs[11] = REG(A1); 33 + dwarf_regs[12] = REG(A2); 34 + dwarf_regs[13] = REG(A3); 35 + dwarf_regs[14] = REG(A4); 36 + dwarf_regs[15] = REG(A5); 37 + dwarf_regs[16] = REG(A6); 38 + dwarf_regs[17] = REG(A7); 39 + dwarf_regs[18] = REG(S2); 40 + dwarf_regs[19] = REG(S3); 41 + dwarf_regs[20] = REG(S4); 42 + dwarf_regs[21] = REG(S5); 43 + dwarf_regs[22] = REG(S6); 44 + dwarf_regs[23] = REG(S7); 45 + dwarf_regs[24] = REG(S8); 46 + dwarf_regs[25] = REG(S9); 47 + dwarf_regs[26] = REG(S10); 48 + dwarf_regs[27] = REG(S11); 49 + dwarf_regs[28] = REG(T3); 50 + dwarf_regs[29] = REG(T4); 51 + dwarf_regs[30] = REG(T5); 52 + dwarf_regs[31] = REG(T6); 53 + dwfl_thread_state_register_pc(thread, REG(PC)); 54 + 55 + return dwfl_thread_state_registers(thread, 0, PERF_REG_RISCV_MAX, 56 + dwarf_regs); 57 + }