From: Chen Zhongjin <chenzhongjin@huawei.com>
To: <linux-kernel@vger.kernel.org>, <linux-riscv@lists.infradead.org>,
<linux-perf-users@vger.kernel.org>
Cc: <paul.walmsley@sifive.com>, <palmer@dabbelt.com>,
<aou@eecs.berkeley.edu>, <peterz@infradead.org>,
<mingo@redhat.com>, <acme@kernel.org>, <mark.rutland@arm.com>,
<alexander.shishkin@linux.intel.com>, <namhyung@kernel.org>,
<jolsa@kernel.org>, <guoren@kernel.org>, <frederic@kernel.org>,
<vincent.chen@sifive.com>, <ardb@kernel.org>,
<mhiramat@kernel.org>, <rostedt@goodmis.org>,
<keescook@chromium.org>, <catalin.marinas@arm.com>,
<chenzhongjin@huawei.com>
Subject: [PATCH for-next v2 3/4] riscv: stacktrace: Save pt_regs in ENCODE_FRAME_POINTER
Date: Wed, 21 Sep 2022 20:51:26 +0800 [thread overview]
Message-ID: <20220921125128.33913-4-chenzhongjin@huawei.com> (raw)
In-Reply-To: <20220921125128.33913-1-chenzhongjin@huawei.com>
To support stack unwinding when there is a pt_regs on stack, the position
of pt_regs is nessesary. Because for some functions, compiler only push
s0/fp on stack without ra.
As the situation described in
commit f766f77a74f5("riscv/stacktrace: Fix stack output without ra on the stack top")
When irq happens there, the function frame looks like:
prev function | ... |
| |
normal function +-----------------+
| ra to prev |
| s0 of prev |
| ... |<-+
leaf function +-----------------+ |
| s0 of normal | |
| empty slot | |
irq pt_regs +-----------------+ |
| epc (ra to leaf)| |
| ra (ra to norm)| |
| ... | |
| s0 of leaf |--+
| ... |
+-----------------+
When unwinding from unwinding from leaf to normal, beacause (ra to norm)
is saved in pt_regs, but not stackframe of leaf, we have to get pt_regs
for that.
To get pt_regs position on stack, we can save the encoded *pt_regs
in s0, as x86 architecture did. Then we can get s0, epc and ra
easily.
Add ENCODE_FRAME_POINTER in irq, ftrace and kretprobe trampoline entries.
For ftrace and kretprobes we should also set pt_regs as kernel mode
so that they are not mistaken as user_mode pt_regs.
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
Reviewed-by: Guo Ren <guoren@kernel.org>
---
arch/riscv/include/asm/frame.h | 45 +++++++++++++++++++
arch/riscv/kernel/entry.S | 3 ++
arch/riscv/kernel/mcount-dyn.S | 7 +++
arch/riscv/kernel/probes/kprobes_trampoline.S | 7 +++
4 files changed, 62 insertions(+)
create mode 100644 arch/riscv/include/asm/frame.h
diff --git a/arch/riscv/include/asm/frame.h b/arch/riscv/include/asm/frame.h
new file mode 100644
index 000000000000..2a1f45cf3a4e
--- /dev/null
+++ b/arch/riscv/include/asm/frame.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_RISCV_FRAME_H
+#define _ASM_RISCV_FRAME_H
+
+#include <asm/asm.h>
+
+#ifdef CONFIG_FRAME_POINTER
+
+#ifdef __ASSEMBLY__
+
+/*
+ * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
+ * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
+ * is just setting the LSB, which makes it an invalid stack address and is also
+ * a signal to the unwinder that it's a pt_regs pointer in disguise.
+ *
+ * This macro must be used when sp point to pt_regs
+ */
+.macro ENCODE_FRAME_POINTER
+ add s0, sp, 0x1
+.endm
+
+#else /* !__ASSEMBLY__ */
+
+#define ENCODE_FRAME_POINTER \
+ "add s0, sp, 0x1\n\t"
+
+#endif /* __ASSEMBLY__ */
+
+#else /* !CONFIG_FRAME_POINTER */
+
+#ifdef __ASSEMBLY__
+
+.macro ENCODE_FRAME_POINTER ptregs_offset=0
+.endm
+
+#else /* !__ASSEMBLY */
+
+#define ENCODE_FRAME_POINTER
+
+#endif /* !__ASSEMBLY */
+
+#endif /* CONFIG_FRAME_POINTER */
+
+#endif /* _ASM_RISCV_FRAME_H */
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index b9eda3fcbd6d..ecb15c7430b4 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -13,6 +13,7 @@
#include <asm/thread_info.h>
#include <asm/asm-offsets.h>
#include <asm/errata_list.h>
+#include <asm/frame.h>
#if !IS_ENABLED(CONFIG_PREEMPTION)
.set resume_kernel, restore_all
@@ -95,6 +96,8 @@ _save_context:
REG_S s4, PT_CAUSE(sp)
REG_S s5, PT_TP(sp)
+ ENCODE_FRAME_POINTER
+
/*
* Set the scratch register to 0, so that if a recursive exception
* occurs, the exception vector knows it came from the kernel
diff --git a/arch/riscv/kernel/mcount-dyn.S b/arch/riscv/kernel/mcount-dyn.S
index d171eca623b6..a362521e030e 100644
--- a/arch/riscv/kernel/mcount-dyn.S
+++ b/arch/riscv/kernel/mcount-dyn.S
@@ -10,6 +10,8 @@
#include <asm/asm-offsets.h>
#include <asm-generic/export.h>
#include <asm/ftrace.h>
+#include <asm/frame.h>
+#include <asm/csr.h>
.text
@@ -172,6 +174,11 @@ ENDPROC(ftrace_caller)
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
ENTRY(ftrace_regs_caller)
SAVE_ALL
+#ifdef CONFIG_FRAME_POINTER
+ li s0, SR_PP
+ REG_S s0, PT_STATUS(sp)
+ ENCODE_FRAME_POINTER
+#endif
addi a0, ra, -FENTRY_RA_OFFSET
la a1, function_trace_op
diff --git a/arch/riscv/kernel/probes/kprobes_trampoline.S b/arch/riscv/kernel/probes/kprobes_trampoline.S
index 7bdb09ded39b..70760a16784f 100644
--- a/arch/riscv/kernel/probes/kprobes_trampoline.S
+++ b/arch/riscv/kernel/probes/kprobes_trampoline.S
@@ -6,6 +6,8 @@
#include <asm/asm.h>
#include <asm/asm-offsets.h>
+#include <asm/frame.h>
+#include <asm/csr.h>
.text
.altmacro
@@ -78,6 +80,11 @@
ENTRY(__kretprobe_trampoline)
addi sp, sp, -(PT_SIZE_ON_STACK)
save_all_base_regs
+#ifdef CONFIG_FRAME_POINTER
+ li s0, SR_PP
+ REG_S s0, PT_STATUS(sp)
+ ENCODE_FRAME_POINTER
+#endif
move a0, sp /* pt_regs */
--
2.17.1
next prev parent reply other threads:[~2022-09-21 12:55 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-21 12:51 [PATCH for-next v2 0/4] riscv: Improvments for stacktrace Chen Zhongjin
2022-09-21 12:51 ` [PATCH for-next v2 1/4] riscv: stacktrace: Replace walk_stackframe with arch_stack_walk Chen Zhongjin
2022-09-21 12:51 ` [PATCH for-next v2 2/4] riscv: stacktrace: Introduce unwind functions Chen Zhongjin
2022-09-21 12:51 ` Chen Zhongjin [this message]
2022-09-21 12:51 ` [PATCH for-next v2 4/4] riscv: stacktrace: Implement stacktrace for irq Chen Zhongjin
2022-09-21 14:02 ` [PATCH for-next v2 0/4] riscv: Improvments for stacktrace Mark Rutland
2022-09-22 8:22 ` Chen Zhongjin
2022-09-23 13:34 ` Mark Rutland
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220921125128.33913-4-chenzhongjin@huawei.com \
--to=chenzhongjin@huawei.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=frederic@kernel.org \
--cc=guoren@kernel.org \
--cc=jolsa@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=vincent.chen@sifive.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox