perf: sparc64 user regs and stack dump, with an arch hook
Subsystems (3)
v2 cover perf: sparc64 user regs and stack dump, with an arch hook
This adds HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP to sparc64, so that perf record --call-graph dwarf and elfutils' eu-stackprof work there.
One piece does not fit in arch code. The user stack dump copies the stack as it is in memory and assumes the call chain is there. On sparc it may not be: the sampled register window's %l/%i registers, which hold the frame pointer and return address the unwinder starts from (the CFI after save defines the CFA in terms of %i6), stay in the register file until a window spills. The kernel already deals with this wherever it exposes user stack memory: perf_callchain_user() on sparc calls flushw_user() before walking the chain, and ptrace does the same. The stack dump has no arch entry point where that could happen, and I found no correct sparc-only place:
- flushing in the sparc PMU interrupt handler misses software events (cpu-clock, tracepoints), which reach perf_event_overflow() without passing through it;
- perf_user_stack_pointer() is private to kernel/events/internal.h;
- perf_reg_abi() is called at the right time but is a query, and a flush as a side effect of it would be wrong.
Patch 1 therefore adds a no-op hook in the style of perf_arch_misc_flags(), perf_arch_prepare_ustack(), called from perf_prepare_sample() when PERF_SAMPLE_STACK_USER is requested and user regs exist. Patch 2 is the sparc64 implementation and its user. Patch 3 teaches tools/perf the sparc registers and DWARF unwinding from the dump. The matching elfutils changes, which make eu-stackprof work, are attached to the tracking issue as patches against current elfutils main:
https://github.com/sparclinux/issues/issues/99
Tested on an UltraSPARC T4-1 on 7.3-rc4: register values check out against known contents, --call-graph dwarf unwinds correctly for both cycles and cpu-clock, eu-stackprof unwinds the same chains, and perf stat/record/record -g are unchanged.
Changes since v1:
- Patch 2: truncate a 32-bit task's values only for user_mode(regs), so kernel-mode PERF_SAMPLE_REGS_INTR registers stay intact (sashiko review).
- Patch 2: document that the stack dump starts at the biased %sp.
- Patch 2: mark sparc ok in Documentation/features for perf-regs and perf-stackdump (Magnus).
- Patch 3 (tools/perf) added (Magnus).
v1: https://lore.kernel.org/all/[email protected]/
Stian Halseth (3):
perf/core: Let an arch prepare the user stack before it is dumped
sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER perf tools: Support sparc user register samples and dwarf unwinding
.../features/perf/perf-regs/arch-support.txt | 2 +- .../perf/perf-stackdump/arch-support.txt | 2 +- arch/sparc/Kconfig | 2 + arch/sparc/include/asm/perf_event.h | 3 + arch/sparc/include/uapi/asm/perf_regs.h | 33 +++++++++ arch/sparc/kernel/Makefile | 2 +- arch/sparc/kernel/perf_regs.c | 65 +++++++++++++++++ include/linux/perf_event.h | 7 ++ kernel/events/core.c | 2 + tools/arch/sparc/include/uapi/asm/perf_regs.h | 33 +++++++++ tools/perf/arch/sparc/include/perf_regs.h | 18 +++++ tools/perf/check-headers.sh | 1 + tools/perf/util/dwarf-regs-arch/Build | 1 + .../util/dwarf-regs-arch/dwarf-regs-sparc.c | 12 ++++ tools/perf/util/dwarf-regs.c | 4 ++ tools/perf/util/include/dwarf-regs.h | 1 + tools/perf/util/perf-regs-arch/Build | 1 + .../util/perf-regs-arch/perf_regs_sparc.c | 69 +++++++++++++++++++ tools/perf/util/perf_regs.c | 18 +++++ tools/perf/util/perf_regs.h | 5 ++ tools/perf/util/unwind-libdw.c | 41 ++++++++++- 21 files changed, 318 insertions(+), 4 deletions(-) create mode 100644 arch/sparc/include/uapi/asm/perf_regs.h create mode 100644 arch/sparc/kernel/perf_regs.c create mode 100644 tools/arch/sparc/include/uapi/asm/perf_regs.h create mode 100644 tools/perf/arch/sparc/include/perf_regs.h create mode 100644 tools/perf/util/dwarf-regs-arch/dwarf-regs-sparc.c create mode 100644 tools/perf/util/perf-regs-arch/perf_regs_sparc.c
Signature · 1 line
2.55.0
RFC PATCH v2 1/3 perf/core: Let an arch prepare the user stack before it is dumped
PERF_SAMPLE_STACK_USER copies the user stack as it is in memory, which assumes the whole call chain is there. On sparc the sampled window's %l and %i registers, including the frame pointer and return address an unwinder starts from, may still be in the register file: they only reach the stack when a window spills. perf_callchain_user() already handles this for the callchain by calling flushw_user() first; the user stack dump has no equivalent arch entry point.
Add perf_arch_prepare_ustack(), a no-op by default, called from perf_prepare_sample() before the dump size is computed, so an arch can write back whatever part of the user's stack state it still holds in registers. Doing this in the arch PMU interrupt handler would not do:
software events such as cpu-clock reach perf_event_overflow() without passing through it.
--- include/linux/perf_event.h | 7 +++++++ kernel/events/core.c | 2 ++ 2 files changed, 9 insertions(+)
include/linux/perf_event.h +7 -0
@@ -1815,6 +1815,13 @@ extern unsigned long perf_instruction_pointer(struct perf_event *event,#ifndef perf_arch_bpf_user_pt_regs# define perf_arch_bpf_user_pt_regs (regs) regs#endif +/* +* Called before the user stack of the current task is dumped, for an arch +* that still holds part of the user's stack state in registers. +*/ +#ifndef perf_arch_prepare_ustack +static inline void perf_arch_prepare_ustack (void ) { } +#endif #ifndef perf_arch_guest_misc_flagsstatic inline unsigned long perf_arch_guest_misc_flags (struct pt_regs *regs)
kernel/events/core.c +2 -0
@@ -8718,6 +8718,8 @@ void perf_prepare_sample(struct perf_sample_data *data,u16 header_size =perf_sample_data_size (data, event);u16 size =sizeof (u64); +if (data->regs_user.regs) +perf_arch_prepare_ustack (); stack_size =perf_sample_ustack_size (stack_size, header_size, data->regs_user.regs);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [RFC PATCH v2 1/3] perf/core: Let an arch prepare the user stack before it is dumped
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
PERF_SAMPLE_STACK_USERcopies the user stack as it is in memory, which assumes the whole call chain is there. On sparc the sampled window's %l and %i registers, including the frame pointer and return address an unwinder starts from, may still be in the register file: they only reach the stack when a window spills.perf_callchain_user()already handles this for the callchain by callingflushw_user()first; the user stack dump has no equivalent arch entry point.
Quoted 8 lines
Add
perf_arch_prepare_ustack(), a no-op by default, called fromperf_prepare_sample()before the dump size is computed, so an arch can write back whatever part of the user's stack state it still holds in registers. Doing this in the arch PMU interrupt handler would not do:
software events such as cpu-clock reachperf_event_overflow()without passing through it.Signed-off-by Stian Halseth <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RFC PATCH v2 2/3 sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
In reply to Stian HalsethSelect HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP and add the perf_regs implementation. The exposed registers mirror struct pt_regs:
%g0-%g7, %o0-%o7 (named from the user's point of view; pt_regs calls them UREG_I*), %tstate, PC, NPC and %y.
%o6 is reported as held in the register, i.e. with the 2047 stack bias for a 64-bit task, the same as ptrace and user_stack_pointer(). The stack dump therefore also starts at the biased address, as consumers of PERF_SAMPLE_STACK_USER expect the dump to begin at the sampled stack pointer value; the first 2047 bytes lie below the frame, and a sample whose %sp page is not mapped yet gets an empty dump, as on any arch with a not-present stack page.
User-mode values of a 32-bit task are truncated to 32 bits as genregs32_get() does, since the hardware does not zero-extend them. Kernel-mode registers, as seen by PERF_SAMPLE_REGS_INTR, are left alone.
perf_arch_prepare_ustack() flushes the user register windows so the sampled window's %l and %i registers are in the dumped stack, where a DWARF unwinder needs them: the CFI of a function after save defines the CFA in terms of %i6.
This makes perf record --call-graph dwarf and eu-stackprof usable on sparc64.
--- .../features/perf/perf-regs/arch-support.txt | 2 +- .../perf/perf-stackdump/arch-support.txt | 2 +- arch/sparc/Kconfig | 2 + arch/sparc/include/asm/perf_event.h | 3 + arch/sparc/include/uapi/asm/perf_regs.h | 33 ++++++++++ arch/sparc/kernel/Makefile | 2 +- arch/sparc/kernel/perf_regs.c | 65 +++++++++++++++++++ 7 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 arch/sparc/include/uapi/asm/perf_regs.h create mode 100644 arch/sparc/kernel/perf_regs.c
Documentation/features/perf/perf-regs/arch-support.txt +1 -1
@@ -23,7 +23,7 @@ | riscv: | ok | | s390: | ok | | sh: | TODO | - | sparc: | TODO | + | sparc: | ok | | um: | TODO | | x86: | ok | | xtensa: | TODO |
Documentation/features/perf/perf-stackdump/arch-support.txt +1 -1
@@ -23,7 +23,7 @@ | riscv: | ok | | s390: | ok | | sh: | TODO | - | sparc: | TODO | + | sparc: | ok | | um: | TODO | | x86: | ok | | xtensa: | TODO |
arch/sparc/Kconfig +2 -0
@@ -93,6 +93,8 @@ config SPARC64 select RTC_DRV_SUN4V select RTC_DRV_STARFIRE select HAVE_PERF_EVENTS + select HAVE_PERF_REGS + select HAVE_PERF_USER_STACK_DUMP select PERF_USE_VMALLOC select ARCH_HAVE_NMI_SAFE_CMPXCHG select HAVE_C_RECORDMCOUNT
arch/sparc/include/asm/perf_event.h +3 -0
@@ -25,6 +25,9 @@ do { \ (regs)->u_regs[UREG_I6 ] = _fp; \ (regs)->u_regs[UREG_I7 ] = _i7; \ } while (0) + +void perf_arch_prepare_ustack(void); +#define perf_arch_prepare_ustack perf_arch_prepare_ustack#endif #endif
arch/sparc/include/uapi/asm/perf_regs.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_ASM_SPARC_PERF_REGS_H +#define _UAPI_ASM_SPARC_PERF_REGS_H + +/* +* Mirrors struct pt_regs. O6 is %sp as held in the register, i.e. biased +* by 2047 for a 64-bit task. +*/ +enum perf_event_sparc_regs { +PERF_REG_SPARC_G0 , +PERF_REG_SPARC_G1 , +PERF_REG_SPARC_G2 , +PERF_REG_SPARC_G3 , +PERF_REG_SPARC_G4 , +PERF_REG_SPARC_G5 , +PERF_REG_SPARC_G6 , +PERF_REG_SPARC_G7 , +PERF_REG_SPARC_O0 , +PERF_REG_SPARC_O1 , +PERF_REG_SPARC_O2 , +PERF_REG_SPARC_O3 , +PERF_REG_SPARC_O4 , +PERF_REG_SPARC_O5 , +PERF_REG_SPARC_O6 , +PERF_REG_SPARC_O7 , +PERF_REG_SPARC_TSTATE , +PERF_REG_SPARC_PC , +PERF_REG_SPARC_NPC , +PERF_REG_SPARC_Y , +PERF_REG_SPARC_MAX +}; + +#endif /* _UAPI_ASM_SPARC_PERF_REGS_H */
arch/sparc/kernel/Makefile +1 -1
@@ -113,7 +113,7 @@ obj-$(CONFIG_AUDIT) += audit.o audit--$(CONFIG_AUDIT) := compat_audit.o obj-$(CONFIG_COMPAT) += $(audit--y) -pc--$(CONFIG_PERF_EVENTS) := perf_event.o +pc--$(CONFIG_PERF_EVENTS) := perf_event.o perf_regs.o obj-$(CONFIG_SPARC64) += $(pc--y) obj-$(CONFIG_UPROBES) += uprobes.o
arch/sparc/kernel/perf_regs.c new file +65 -0
65 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [RFC PATCH v2 2/3] sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] perf: sparc64: infoleak of uninitialized kernel stack via PERF_REG_SPARC_G0 - [Medium] perf…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] perf: sparc64: infoleak of uninitialized kernel stack via PERF_REG_SPARC_G0
- [Medium] perf: sparc64: PERF_SAMPLE_STACK_USER fails due to 2047-byte stack bias --
commit dc2931cc8aec6bb17c7d08c4adfcf954bfdf3a68
Author: Stian Halseth <[email protected]>
sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
Select HAVE_PERF_REGS and HAVE_PERF_USER_STACK_DUMP and add the perf_regs implementation. The exposed registers mirror struct pt_regs:
%g0-%g7, %o0-%o7 (named from the user's point of view; pt_regs calls them UREG_I*), %tstate, PC, NPC and %y.
Quoted 11 lines
arch/sparc/kernel/perf_regs.c new file +5
-0@@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/perf_event.h> +#include <linux/perf_regs.h> +#include <linux/sched/task_stack.h> +#include <asm/cacheflush.h>
arch/sparc/kernel/perf_regs.c line 15
Quoted 10 lines
arch/sparc/kernel/perf_regs.c new file +10
-0+#include <asm/ptrace.h> + +u64 perf_reg_value (struct pt_regs *regs,int idx) +{ +u64 val; + +switch (idx) { + case PERF_REG_SPARC_G0 ... PERF_REG_SPARC_O7: + val = regs->u_regs[idx]; +break ;
[Severity: High]
Since %g0 is hardwired to zero on the SPARC architecture, the trap entry code does not save it into the PT_V9_G0 slot (u_regs[0]) of the pt_regs structure on the kernel stack.
When an unprivileged user configures a perf event to sample PERF_SAMPLE_REGS_USER or PERF_SAMPLE_REGS_INTR and requests the %g0 register (where idx=0), does returning regs->u_regs[0] here expose uninitialized kernel stack memory to userspace per sample?
[Severity: Medium]
For PERF_REG_SPARC_O6 (%sp), returning the raw register means the stack pointer remains biased by 2047 bytes on SPARC64.
While the commit message notes this biased address is expected by consumers, when perf_output_sample_ustack() attempts to copy the stack, it will start 2047 bytes below the actual mapped stack frame.
Could this high probability (~25% for 8KB pages) of starting in an unmapped page cause __copy_from_user_inatomic() to immediately fault and abort the copy? Would this result in a 0-byte dynamic stack dump even for perfectly valid stack frames, breaking userspace DWARF unwinding and profiling tools?
Quoted 4 lines
arch/sparc/kernel/perf_regs.c new file +4
-0+case PERF_REG_SPARC_TSTATE : +return regs->tstate; +case PERF_REG_SPARC_PC : + val = regs->tpc;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [RFC PATCH v2 2/3] sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
Quoted 8 lines · On Tue, 2026-09-22 at 20:33 +0000, [email protected] wrote:
[Severity: High]
Since %g0 is hardwired to zero on the SPARC architecture, the trap entry
code does not save it into thePT_V9_G0slot (u_regs[0]) of thept_regs
structure on the kernel stack.
Quoted 5 lines
When an unprivileged user configures a perf event to sample
PERF_SAMPLE_REGS_USERorPERF_SAMPLE_REGS_INTRand requests the %g0 register (where idx=0), does returning regs->u_regs[0] here expose uninitialized kernel stack memory to userspace per sample?
Yeah, that is probably valid for trap taken in the kernel...
I checked the implementation for mips and longaarch, and I will drop %g0 from the ABI, and put PC in its slot instead.
Putting that into a v3 later.
Quoted 10 lines
[Severity: Medium]
ForPERF_REG_SPARC_O6(%sp), returning the raw register means the stack
pointer remains biased by 2047 bytes on SPARC64.While the commit message notes this biased address is expected by consumers, when
perf_output_sample_ustack()attempts to copy the stack, it
will start 2047 bytes below the actual mapped stack frame.
Quoted 8 lines
Could this high probability (~25% for 8KB pages) of starting in an unmapped
page cause__copy_from_user_inatomic()to immediately fault and abort the
copy? Would this result in a 0-byte dynamic stack dump even for perfectly
valid stack frames, breaking userspace DWARF unwinding and profiling tools?
I was thinking about setting up some real world tests to see if this is valid/producable, but I think I will instead go back on my first statement in v1, and start the dump at the stack's actual address: %o0
+ 2047 for a 64-bit stack.
Unless I'm missing something, that should clear this concern.
Then the tools and elfutils patches take their dump base from there. There aren't really any other consumers anyway, so changing this now, rather than later, is probably a good idea.
I will wait for some more feedback, and starting preparing a v3 tomorrow.
Signature · 2 lines
Best regards Stian Halseth
Re: [RFC PATCH v2 2/3] sparc64: Support PERF_SAMPLE_REGS_USER and PERF_SAMPLE_STACK_USER
Hi, correcting a typo in my previous reply.
Quoted 21 lines · Stian Halseth
Quoted 10 lines
[Severity: Medium]
ForPERF_REG_SPARC_O6(%sp), returning the raw register means the stack
pointer remains biased by 2047 bytes on SPARC64.While the commit message notes this biased address is expected by consumers, when
perf_output_sample_ustack()attempts to copy the stack, it
will start 2047 bytes below the actual mapped stack frame.Quoted 10 lines
Could this high probability (~25% for 8KB pages) of starting in an unmapped
page cause__copy_from_user_inatomic()to immediately fault and abort
the
copy? Would this result in a 0-byte dynamic stack dump even for perfectly
valid stack frames, breaking userspace DWARF unwinding and profiling
tools?
Quoted 6 lines
I was thinking about setting up some real world tests to see if this is
valid/producable, but I think I will instead go back on my first statement in v1, and start the dump at the stack's actual address:
%o0
+ 2047 for a 64-bit stack.
Typo: %o0 + 2047 should be %o6 + 2047
Getting late, good night ;-)
Quoted 10 lines
Unless I'm missing something, that should clear this concern.
Then the tools and elfutils patches take their dump base from there. There aren't really any other consumers anyway, so changing this now, rather than later, is probably a good idea.
I will wait for some more feedback, and starting preparing a v3 tomorrow.
Signature · 2 lines
Best regards Stian Halseth
RFC PATCH v2 3/3 perf tools: Support sparc user register samples and dwarf unwinding
In reply to Stian HalsethAdd the sparc perf_regs tables and the EM_SPARC/EM_SPARCV9 dispatch for register names, masks, ip/sp and the perf-to-DWARF register mapping, plus the uapi header copy.
For libdw unwinding, seed DWARF registers 16-31 (%l0-%l7, %i0-%i7) from the register save area at the top of the stack dump: the kernel flushes the sampled window there, and the CFI after save needs %i6 and %i7 to find the first caller. The dump base on sparc is the biased %sp, so read words from the dump with memcpy rather than an aligned dereference.
--- tools/arch/sparc/include/uapi/asm/perf_regs.h | 33 +++++++++ tools/perf/arch/sparc/include/perf_regs.h | 18 +++++ tools/perf/check-headers.sh | 1 + tools/perf/util/dwarf-regs-arch/Build | 1 + .../util/dwarf-regs-arch/dwarf-regs-sparc.c | 12 ++++ tools/perf/util/dwarf-regs.c | 4 ++ tools/perf/util/include/dwarf-regs.h | 1 + tools/perf/util/perf-regs-arch/Build | 1 + .../util/perf-regs-arch/perf_regs_sparc.c | 69 +++++++++++++++++++ tools/perf/util/perf_regs.c | 18 +++++ tools/perf/util/perf_regs.h | 5 ++ tools/perf/util/unwind-libdw.c | 41 ++++++++++- 12 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 tools/arch/sparc/include/uapi/asm/perf_regs.h create mode 100644 tools/perf/arch/sparc/include/perf_regs.h create mode 100644 tools/perf/util/dwarf-regs-arch/dwarf-regs-sparc.c create mode 100644 tools/perf/util/perf-regs-arch/perf_regs_sparc.c
tools/arch/sparc/include/uapi/asm/perf_regs.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_ASM_SPARC_PERF_REGS_H +#define _UAPI_ASM_SPARC_PERF_REGS_H + +/* +* Mirrors struct pt_regs. O6 is %sp as held in the register, i.e. biased +* by 2047 for a 64-bit task. +*/ +enum perf_event_sparc_regs { +PERF_REG_SPARC_G0 , +PERF_REG_SPARC_G1 , +PERF_REG_SPARC_G2 , +PERF_REG_SPARC_G3 , +PERF_REG_SPARC_G4 , +PERF_REG_SPARC_G5 , +PERF_REG_SPARC_G6 , +PERF_REG_SPARC_G7 , +PERF_REG_SPARC_O0 , +PERF_REG_SPARC_O1 , +PERF_REG_SPARC_O2 , +PERF_REG_SPARC_O3 , +PERF_REG_SPARC_O4 , +PERF_REG_SPARC_O5 , +PERF_REG_SPARC_O6 , +PERF_REG_SPARC_O7 , +PERF_REG_SPARC_TSTATE , +PERF_REG_SPARC_PC , +PERF_REG_SPARC_NPC , +PERF_REG_SPARC_Y , +PERF_REG_SPARC_MAX +}; + +#endif /* _UAPI_ASM_SPARC_PERF_REGS_H */
tools/perf/arch/sparc/include/perf_regs.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef ARCH_PERF_REGS_H +#define ARCH_PERF_REGS_H + +#include <stdlib.h> +#include <linux/types.h> +#include "../../../../arch/sparc/include/uapi/asm/perf_regs.h" + +#define PERF_REGS_MASK ((1ULL << PERF_REG_SPARC_MAX) - 1) +#define PERF_REGS_MAX PERF_REG_SPARC_MAX + +#ifdef __arch64__ +#define PERF_SAMPLE_REGS_ABI PERF_SAMPLE_REGS_ABI_64 +#else +#define PERF_SAMPLE_REGS_ABI PERF_SAMPLE_REGS_ABI_32 +#endif + +#endif /* ARCH_PERF_REGS_H */
tools/perf/check-headers.sh +1 -0
@@ -42,6 +42,7 @@ declare -a FILES=( "arch/mips/include/uapi/asm/perf_regs.h" "arch/powerpc/include/uapi/asm/perf_regs.h" "arch/s390/include/uapi/asm/perf_regs.h" + "arch/sparc/include/uapi/asm/perf_regs.h" "arch/x86/include/uapi/asm/perf_regs.h" "arch/x86/include/uapi/asm/kvm.h" "arch/x86/include/uapi/asm/svm.h"
tools/perf/util/dwarf-regs-arch/Build +1 -0
@@ -6,4 +6,5 @@ perf-util-$(CONFIG_LIBDW) += dwarf-regs-mips.o perf-util-$(CONFIG_LIBDW) += dwarf-regs-powerpc.o perf-util-$(CONFIG_LIBDW) += dwarf-regs-riscv.o perf-util-$(CONFIG_LIBDW) += dwarf-regs-s390.o +perf-util-$(CONFIG_LIBDW) += dwarf-regs-sparc.o perf-util-$(CONFIG_LIBDW) += dwarf-regs-x86.o
tools/perf/util/dwarf-regs-arch/dwarf-regs-sparc.c new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <errno.h> +#include <dwarf-regs.h> +#include "../../../arch/sparc/include/uapi/asm/perf_regs.h" + +int __get_dwarf_regnum_for_perf_regnum_sparc (int perf_regnum) +{ +if (perf_regnum <PERF_REG_SPARC_G0 || perf_regnum >PERF_REG_SPARC_O7 ) +return -ENOENT ; + +return perf_regnum; +}
tools/perf/util/dwarf-regs.c +4 -0
@@ -217,6 +217,10 @@ int get_dwarf_regnum_for_perf_regnum(int perf_regnum, unsigned int machine,case EM_S390 : reg =__get_dwarf_regnum_for_perf_regnum_s390 (perf_regnum);break ; +case EM_SPARC : +case EM_SPARCV9 : + reg =__get_dwarf_regnum_for_perf_regnum_sparc (perf_regnum); +break ;case EM_LOONGARCH : reg =__get_dwarf_regnum_for_perf_regnum_loongarch (perf_regnum);break ;
tools/perf/util/include/dwarf-regs.h +1 -0
@@ -113,6 +113,7 @@ int __get_dwarf_regnum_for_perf_regnum_loongarch(int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_powerpc (int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_riscv (int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_s390 (int perf_regnum); +int __get_dwarf_regnum_for_perf_regnum_sparc (int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_mips (int perf_regnum);/*
tools/perf/util/perf-regs-arch/Build +1 -0
@@ -6,4 +6,5 @@ perf-util-y += perf_regs_mips.o perf-util-y += perf_regs_powerpc.o perf-util-y += perf_regs_riscv.o perf-util-y += perf_regs_s390.o +perf-util-y += perf_regs_sparc.o perf-util-y += perf_regs_x86.o
tools/perf/util/perf-regs-arch/perf_regs_sparc.c new file +69 -0
69 lines not on this page · raw message
tools/perf/util/perf_regs.c +18 -0
@@ -68,6 +68,10 @@ uint64_t perf_intr_reg_mask(uint16_t e_machine)case EM_S390 : mask =__perf_reg_mask_s390 (/*intr=*/ true);break ; +case EM_SPARC : +case EM_SPARCV9 : + mask =__perf_reg_mask_sparc (/*intr=*/ true); +break ;case EM_386 :case EM_X86_64 : mask =__perf_reg_mask_x86 (/*intr=*/ true);
@@ -111,6 +115,10 @@ uint64_t perf_user_reg_mask(uint16_t e_machine)case EM_S390 : mask =__perf_reg_mask_s390 (/*intr=*/ false);break ; +case EM_SPARC : +case EM_SPARCV9 : + mask =__perf_reg_mask_sparc (/*intr=*/ false); +break ;case EM_386 :case EM_X86_64 : mask =__perf_reg_mask_x86 (/*intr=*/ false);
@@ -154,6 +162,10 @@ const char *perf_reg_name(int id, uint16_t e_machine, uint32_t e_flags)case EM_S390 : reg_name =__perf_reg_name_s390 (id);break ; +case EM_SPARC : +case EM_SPARCV9 : + reg_name =__perf_reg_name_sparc (id); +break ;case EM_386 :case EM_X86_64 : reg_name =__perf_reg_name_x86 (id);
@@ -215,6 +227,9 @@ uint64_t perf_arch_reg_ip(uint16_t e_machine)return __perf_reg_ip_riscv ();case EM_S390 :return __perf_reg_ip_s390 (); +case EM_SPARC : +case EM_SPARCV9 : +return __perf_reg_ip_sparc ();case EM_386 :case EM_X86_64 :return __perf_reg_ip_x86 ();
@@ -244,6 +259,9 @@ uint64_t perf_arch_reg_sp(uint16_t e_machine)return __perf_reg_sp_riscv ();case EM_S390 :return __perf_reg_sp_s390 (); +case EM_SPARC : +case EM_SPARCV9 : +return __perf_reg_sp_sparc ();case EM_386 :case EM_X86_64 :return __perf_reg_sp_x86 ();
tools/perf/util/perf_regs.h +5 -0
@@ -65,6 +65,11 @@ uint64_t __perf_reg_ip_s390(void);uint64_t __perf_reg_sp_s390 (void );int __perf_sdt_arg_parse_op_s390 (char *old_op,char **new_op); +uint64_t __perf_reg_mask_sparc (bool intr); +const char *__perf_reg_name_sparc (int id); +uint64_t __perf_reg_ip_sparc (void ); +uint64_t __perf_reg_sp_sparc (void ); +int __perf_sdt_arg_parse_op_x86 (char *old_op,char **new_op);uint64_t __perf_reg_mask_x86 (bool intr);const char *__perf_reg_name_x86 (int id);
tools/perf/util/unwind-libdw.c +40 -1
77 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [RFC PATCH v2 3/3] perf tools: Support sparc user register samples and dwarf unwinding
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines
Add the sparc
perf_regstables and the EM_SPARC/EM_SPARCV9 dispatch for register names, masks, ip/sp and the perf-to-DWARF register mapping, plus the uapi header copy.
Quoted 8 lines
For libdw unwinding, seed DWARF registers 16-31 (%l0-%l7, %i0-%i7) from the register save area at the top of the stack dump: the kernel flushes the sampled window there, and the CFI after
saveneeds %i6 and %i7 to find the first caller. The dump base on sparc is the biased %sp, so read words from the dump with memcpy rather than an aligned dereference.Signed-off-by Stian Halseth <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerRe: [RFC PATCH v2 3/3] perf tools: Support sparc user register samples and dwarf unwinding
In reply to Stian HalsethQuoted 389 lines · Stian Halseth
Add the sparc
perf_regstables and the EM_SPARC/EM_SPARCV9 dispatch for register names, masks, ip/sp and the perf-to-DWARF register mapping, plus the uapi header copy.For libdw unwinding, seed DWARF registers 16-31 (%l0-%l7, %i0-%i7) from the register save area at the top of the stack dump: the kernel flushes the sampled window there, and the CFI after
saveneeds %i6 and %i7 to find the first caller. The dump base on sparc is the biased %sp, so read words from the dump with memcpy rather than an aligned dereference.Signed-off-by Stian Halseth <[email protected]>--- tools/arch/sparc/include/uapi/asm/perf_regs.h | 33 +++++++++ tools/perf/arch/sparc/include/perf_regs.h | 18 +++++ tools/perf/check-headers.sh | 1 + tools/perf/util/dwarf-regs-arch/Build | 1 + .../util/dwarf-regs-arch/dwarf-regs-sparc.c | 12 ++++ tools/perf/util/dwarf-regs.c | 4 ++ tools/perf/util/include/dwarf-regs.h | 1 + tools/perf/util/perf-regs-arch/Build | 1 + .../util/perf-regs-arch/perf_regs_sparc.c | 69 +++++++++++++++++++ tools/perf/util/perf_regs.c | 18 +++++ tools/perf/util/perf_regs.h | 5 ++ tools/perf/util/unwind-libdw.c | 41 ++++++++++- 12 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 tools/arch/sparc/include/uapi/asm/perf_regs.h create mode 100644 tools/perf/arch/sparc/include/perf_regs.h create mode 100644 tools/perf/util/dwarf-regs-arch/dwarf-regs-sparc.c create mode 100644 tools/perf/util/perf-regs-arch/perf_regs_sparc.ctools/arch/sparc/include/uapi/asm/perf_regs.h new file +33
-0@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_ASM_SPARC_PERF_REGS_H +#define _UAPI_ASM_SPARC_PERF_REGS_H + +/* +* Mirrors struct pt_regs. O6 is %sp as held in the register, i.e. biased +* by 2047 for a 64-bit task. +*/ +enum perf_event_sparc_regs { +PERF_REG_SPARC_G0 , +PERF_REG_SPARC_G1 , +PERF_REG_SPARC_G2 , +PERF_REG_SPARC_G3 , +PERF_REG_SPARC_G4 , +PERF_REG_SPARC_G5 , +PERF_REG_SPARC_G6 , +PERF_REG_SPARC_G7 , +PERF_REG_SPARC_O0 , +PERF_REG_SPARC_O1 , +PERF_REG_SPARC_O2 , +PERF_REG_SPARC_O3 , +PERF_REG_SPARC_O4 , +PERF_REG_SPARC_O5 , +PERF_REG_SPARC_O6 , +PERF_REG_SPARC_O7 , +PERF_REG_SPARC_TSTATE , +PERF_REG_SPARC_PC , +PERF_REG_SPARC_NPC , +PERF_REG_SPARC_Y , +PERF_REG_SPARC_MAX +}; + +#endif /* _UAPI_ASM_SPARC_PERF_REGS_H */ tools/perf/arch/sparc/include/perf_regs.h new file +18
-0@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef ARCH_PERF_REGS_H +#define ARCH_PERF_REGS_H + +#include <stdlib.h> +#include <linux/types.h> +#include "../../../../arch/sparc/include/uapi/asm/perf_regs.h" + +#define PERF_REGS_MASK ((1ULL << PERF_REG_SPARC_MAX) - 1) +#define PERF_REGS_MAX PERF_REG_SPARC_MAX + +#ifdef __arch64__ +#define PERF_SAMPLE_REGS_ABI PERF_SAMPLE_REGS_ABI_64 +#else +#define PERF_SAMPLE_REGS_ABI PERF_SAMPLE_REGS_ABI_32 +#endif + +#endif /* ARCH_PERF_REGS_H */ tools/perf/check-headers.sh +1
-0@@ -42,6 +42,7 @@ declare -a FILES=( "arch/mips/include/uapi/asm/perf_regs.h" "arch/powerpc/include/uapi/asm/perf_regs.h" "arch/s390/include/uapi/asm/perf_regs.h" + "arch/sparc/include/uapi/asm/perf_regs.h" "arch/x86/include/uapi/asm/perf_regs.h" "arch/x86/include/uapi/asm/kvm.h" "arch/x86/include/uapi/asm/svm.h"tools/perf/util/dwarf-regs-arch/Build +1
-0@@ -6,4 +6,5 @@ perf-util-$(CONFIG_LIBDW) += dwarf-regs-mips.o perf-util-$(CONFIG_LIBDW) += dwarf-regs-powerpc.o perf-util-$(CONFIG_LIBDW) += dwarf-regs-riscv.o perf-util-$(CONFIG_LIBDW) += dwarf-regs-s390.o +perf-util-$(CONFIG_LIBDW) += dwarf-regs-sparc.o perf-util-$(CONFIG_LIBDW) += dwarf-regs-x86.otools/perf/util/dwarf-regs-arch/dwarf-regs-sparc.c new file +12
-0@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <errno.h> +#include <dwarf-regs.h> +#include "../../../arch/sparc/include/uapi/asm/perf_regs.h" + +int __get_dwarf_regnum_for_perf_regnum_sparc (int perf_regnum) +{ +if (perf_regnum <PERF_REG_SPARC_G0 || perf_regnum >PERF_REG_SPARC_O7 ) +return -ENOENT ; + +return perf_regnum; +}tools/perf/util/dwarf-regs.c +4
-0@@ -217,6 +217,10 @@ int get_dwarf_regnum_for_perf_regnum(int perf_regnum, unsigned int machine,case EM_S390 : reg =__get_dwarf_regnum_for_perf_regnum_s390 (perf_regnum);break ; +case EM_SPARC : +case EM_SPARCV9 : + reg =__get_dwarf_regnum_for_perf_regnum_sparc (perf_regnum); +break ;case EM_LOONGARCH : reg =__get_dwarf_regnum_for_perf_regnum_loongarch (perf_regnum);break ;tools/perf/util/include/dwarf-regs.h +1
-0@@ -113,6 +113,7 @@ int __get_dwarf_regnum_for_perf_regnum_loongarch(int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_powerpc (int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_riscv (int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_s390 (int perf_regnum); +int __get_dwarf_regnum_for_perf_regnum_sparc (int perf_regnum);int __get_dwarf_regnum_for_perf_regnum_mips (int perf_regnum);/* tools/perf/util/perf-regs-arch/Build +1
-0@@ -6,4 +6,5 @@ perf-util-y += perf_regs_mips.o perf-util-y += perf_regs_powerpc.o perf-util-y += perf_regs_riscv.o perf-util-y += perf_regs_s390.o +perf-util-y += perf_regs_sparc.o perf-util-y += perf_regs_x86.otools/perf/util/perf-regs-arch/perf_regs_sparc.c new file +69
-0@@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "../perf_regs.h" +#include "../../arch/sparc/include/perf_regs.h" + +uint64_t __perf_reg_mask_sparc (bool intr__maybe_unused ) +{ +return PERF_REGS_MASK ; +} + +const char *__perf_reg_name_sparc (int id) +{ +switch (id) { +case PERF_REG_SPARC_G0 : +return "g0" ; +case PERF_REG_SPARC_G1 : +return "g1" ; +case PERF_REG_SPARC_G2 : +return "g2" ; +case PERF_REG_SPARC_G3 : +return "g3" ; +case PERF_REG_SPARC_G4 : +return "g4" ; +case PERF_REG_SPARC_G5 : +return "g5" ; +case PERF_REG_SPARC_G6 : +return "g6" ; +case PERF_REG_SPARC_G7 : +return "g7" ; +case PERF_REG_SPARC_O0 : +return "o0" ; +case PERF_REG_SPARC_O1 : +return "o1" ; +case PERF_REG_SPARC_O2 : +return "o2" ; +case PERF_REG_SPARC_O3 : +return "o3" ; +case PERF_REG_SPARC_O4 : +return "o4" ; +case PERF_REG_SPARC_O5 : +return "o5" ; +case PERF_REG_SPARC_O6 : +return "sp" ; +case PERF_REG_SPARC_O7 : +return "o7" ; +case PERF_REG_SPARC_TSTATE : +return "tstate" ; +case PERF_REG_SPARC_PC : +return "pc" ; +case PERF_REG_SPARC_NPC : +return "npc" ; +case PERF_REG_SPARC_Y : +return "y" ; +default : +return NULL ; + } + +return NULL ; +} + +uint64_t __perf_reg_ip_sparc (void ) +{ +return PERF_REG_SPARC_PC ; +} + +uint64_t __perf_reg_sp_sparc (void ) +{ +return PERF_REG_SPARC_O6 ; +}tools/perf/util/perf_regs.c +18
-0@@ -68,6 +68,10 @@ uint64_t perf_intr_reg_mask(uint16_t e_machine)case EM_S390 : mask =__perf_reg_mask_s390 (/*intr=*/ true);break ; +case EM_SPARC : +case EM_SPARCV9 : + mask =__perf_reg_mask_sparc (/*intr=*/ true); +break ;case EM_386 :case EM_X86_64 : mask =__perf_reg_mask_x86 (/*intr=*/ true);@@ -111,6 +115,10 @@ uint64_t perf_user_reg_mask(uint16_t e_machine)case EM_S390 : mask =__perf_reg_mask_s390 (/*intr=*/ false);break ; +case EM_SPARC : +case EM_SPARCV9 : + mask =__perf_reg_mask_sparc (/*intr=*/ false); +break ;case EM_386 :case EM_X86_64 : mask =__perf_reg_mask_x86 (/*intr=*/ false);@@ -154,6 +162,10 @@ const char *perf_reg_name(int id, uint16_t e_machine, uint32_t e_flags)case EM_S390 : reg_name =__perf_reg_name_s390 (id);break ; +case EM_SPARC : +case EM_SPARCV9 : + reg_name =__perf_reg_name_sparc (id); +break ;case EM_386 :case EM_X86_64 : reg_name =__perf_reg_name_x86 (id);@@ -215,6 +227,9 @@ uint64_t perf_arch_reg_ip(uint16_t e_machine)return __perf_reg_ip_riscv ();case EM_S390 :return __perf_reg_ip_s390 (); +case EM_SPARC : +case EM_SPARCV9 : +return __perf_reg_ip_sparc ();case EM_386 :case EM_X86_64 :return __perf_reg_ip_x86 ();@@ -244,6 +259,9 @@ uint64_t perf_arch_reg_sp(uint16_t e_machine)return __perf_reg_sp_riscv ();case EM_S390 :return __perf_reg_sp_s390 (); +case EM_SPARC : +case EM_SPARCV9 : +return __perf_reg_sp_sparc ();case EM_386 :case EM_X86_64 :return __perf_reg_sp_x86 ();tools/perf/util/perf_regs.h +5
-0@@ -65,6 +65,11 @@ uint64_t __perf_reg_ip_s390(void);uint64_t __perf_reg_sp_s390 (void );int __perf_sdt_arg_parse_op_s390 (char *old_op,char **new_op); +uint64_t __perf_reg_mask_sparc (bool intr); +const char *__perf_reg_name_sparc (int id); +uint64_t __perf_reg_ip_sparc (void ); +uint64_t __perf_reg_sp_sparc (void ); +int __perf_sdt_arg_parse_op_x86 (char *old_op,char **new_op);uint64_t __perf_reg_mask_x86 (bool intr);const char *__perf_reg_name_x86 (int id);tools/perf/util/unwind-libdw.c +36
-1@@ -4,7 +4,9 @@#include <elfutils/libdw.h> #include <elfutils/libdwfl.h> #include <inttypes.h> +#include <elf.h> #include <errno.h> +#include <string.h> #include "debug.h" #include "dso.h" #include <dwarf-regs.h> @@ -261,12 +263,44 @@ static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word * } offset = addr - start; - *result = *(Dwarf_Word *)&stack->data[offset]; +memcpy (result, &stack->data[offset],sizeof (*result));pr_debug ("unwind: access_mem addr 0x%" PRIx64", val %lx, offset %d\n" , addr, (unsigned long )*result, offset);return true; } +/* +* The sampled window's %l and %i registers (DWARF 16-31) are not in the +* sample; the kernel flushed them to the register save area at the top of +* the dumped stack. +*/ +static void libdw_set_sparc_window_registers (struct unwind_info *ui, +struct regs_dump *user_regs, +Dwarf_Word *dwarf_regs) +{ +struct stack_dump *stack = &ui->sample->user_stack; +bool abi64 = user_regs->abi ==PERF_SAMPLE_REGS_ABI_64 ; +size_t wsize = abi64 ?sizeof (u64) :sizeof (u32); +u64 sp, offset = abi64 ?2047 :0 ; + +if (perf_reg_value (&sp, user_regs,perf_arch_reg_sp (ui->e_machine))) +return ; +if (offset +16 * wsize > stack->size) +return ; + +for (int i =0 ; i <16 ; i++) { +const char *p = &stack->data[offset + i * wsize]; +u32 w32; + +if (abi64) { +memcpy (&dwarf_regs[16 + i], p,sizeof (u64)); + }else { +memcpy (&w32, p,sizeof (w32)); + dwarf_regs[16 + i] = w32; + } + } +} +static bool libdw_set_initial_registers (Dwfl_Thread *thread,void *arg) {struct dwfl_ui_thread_info *dwfl_ui_ti = arg;@@ -276,6 +310,7 @@ static bool libdw_set_initial_registers(Dwfl_Thread *thread, void *arg)int max_dwarf_reg =0 ;bool ret;uint16_t e_machine = ui->e_machine; +bool is_sparc = e_machine ==EM_SPARC || e_machine ==EM_SPARCV9 ;int e_flags = ui->e_flags;uint64_t ip_perf_reg =perf_arch_reg_ip (e_machine);Dwarf_Word val =0 ;
tools/perf/util/unwind-libdw.c static bool libdw_set_initial_registers(Dwfl_Thread *thread, void *arg) · line 334
Quoted 5 lines
tools/perf/util/unwind-libdw.c +2
-0@@ -295,6 +330,8 @@ static bool libdw_set_initial_registers(Dwfl_Thread *thread, void *arg) max_dwarf_reg = dwarf_reg; } } +if (is_sparc && max_dwarf_reg <31 ) + max_dwarf_reg =31 ;
Thanks! I think these are great patches! At this point in the code are you setting max_dwarf_reg so that there's space for the window registers? I think adding a comment here for clarity would be worthwhile as it matches up with libdw_set_sparc_window_registers.
Thanks,
Ian
Quoted 15 lines
tools/perf/util/unwind-libdw.c +2
-1dwarf_regs =calloc (max_dwarf_reg +1 ,sizeof (*dwarf_regs));if (!dwarf_regs)@@ -313,6 +350,8 @@ static bool libdw_set_initial_registers(Dwfl_Thread *thread, void *arg) } } } +if (is_sparc) +libdw_set_sparc_window_registers (ui, user_regs, dwarf_regs);if (perf_reg_value (&val, user_regs, ip_perf_reg) ==0 )dwfl_thread_state_register_pc (thread, val); --2.55.0
Re: [RFC PATCH v2 3/3] perf tools: Support sparc user register samples and dwarf unwinding
Hi Ian,
Quoted 5 lines · Ian Rogers
Quoted 5 lines
tools/perf/util/unwind-libdw.c +0
-0@@ -295,6 +330,8 @@ static boollibdw_set_initial_registers (Dwfl_Thread *thread, void *arg) max_dwarf_reg = dwarf_reg; } }
Quoted 9 lines
tools/perf/util/unwind-libdw.c static bool libdw_set_initial_registers(Dwfl_Thread *thread, void *arg) · line 334
Quoted 2 lines
tools/perf/util/unwind-libdw.c +2
-0+ if (is_sparc && max_dwarf_reg < 31) + max_dwarf_reg = 31;Thanks! I think these are great patches! At this point in the code are
you settingmax_dwarf_regso that there's space for the window registers? I think adding a comment here for clarity would be worthwhile as it matches up withlibdw_set_sparc_window_registers.
I'm glad to hear that!
Yes. On sparc the sampled registers map only to DWARF 0-15, so without this the array would be too short for the %l/%i values that libdw_set_sparc_window_registers() writes to 16-31. I'll add a comment there in the next version.
Thanks for the review!
Signature · 2 lines
Best regards Stian Halseth