From: Stian Halseth <stian@itx.no>
To: "David S. Miller" <davem@davemloft.net>,
Andreas Larsson <andreas@gaisler.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
Naveen N Rao <naveen@kernel.org>,
Danish Khateeb <danishkhateeb03@gmail.com>,
sparclinux@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
linux-kernel@vger.kernel.org, Stian Halseth <stian@itx.no>
Subject: [PATCH v2 4/4] sparc64: kprobes: Handle annulled branches
Date: Fri, 13 Nov 2026 15:40:57 +0100 [thread overview]
Message-ID: <20261113144057.1697874-5-stian@itx.no> (raw)
In-Reply-To: <20261113144057.1697874-1-stian@itx.no>
The single-stepped copy of a branch has a break instruction in its
delay slot. An annulled branch that is not taken skips that slot and
runs past the copy. A taken ba,a or cbcond branches away without
reaching it.
Put a second break instruction after the delay slot. When it is hit,
resume after the original delay slot. Refuse probes on ba,a, fba,a
and cbcond.
Link: https://github.com/sparclinux/issues/issues/110
Signed-off-by: Stian Halseth <stian@itx.no>
---
arch/sparc/include/asm/kprobes.h | 2 +-
arch/sparc/kernel/kprobes.c | 43 ++++++++++++++++++++++++++++----
2 files changed, 39 insertions(+), 6 deletions(-)
diff --git a/arch/sparc/include/asm/kprobes.h b/arch/sparc/include/asm/kprobes.h
index d14206c063e25..e15fd84c88681 100644
--- a/arch/sparc/include/asm/kprobes.h
+++ b/arch/sparc/include/asm/kprobes.h
@@ -15,7 +15,7 @@
typedef u32 kprobe_opcode_t;
-#define MAX_INSN_SIZE 2
+#define MAX_INSN_SIZE 3
#define kretprobe_blacklist_size 0
diff --git a/arch/sparc/kernel/kprobes.c b/arch/sparc/kernel/kprobes.c
index 327d83188a978..dc1180d4c2338 100644
--- a/arch/sparc/kernel/kprobes.c
+++ b/arch/sparc/kernel/kprobes.c
@@ -21,7 +21,8 @@
*
* In the kprobe->ainsn.insn[] array we store the original
* instruction at index zero and a break instruction at
- * index one.
+ * index one, and another at index two for a branch that
+ * annuls its delay slot.
*
* When we hit a kprobe we:
* - Run the pre-handler
@@ -47,11 +48,34 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
+/* cbcond has no delay slot and ba,a annuls it, so when taken the
+ * single-stepped copy branches away without reaching a break instruction.
+ */
+static bool __kprobes kprobe_insn_rejected(u32 insn)
+{
+ if (insn & 0xc0000000)
+ return false;
+
+ switch ((insn >> 22) & 0x7) {
+ case 1: /* BPcc */
+ case 2: /* Bicc */
+ case 5: /* FBPfcc */
+ case 6: /* FBfcc */
+ return (insn & 0x3e000000) == 0x30000000;
+ case 3: /* BPr, cbcond */
+ return insn & 0x10000000;
+ }
+ return false;
+}
+
int __kprobes arch_prepare_kprobe(struct kprobe *p)
{
if ((unsigned long) p->addr & 0x3UL)
return -EILSEQ;
+ if (kprobe_insn_rejected(*p->addr))
+ return -EINVAL;
+
p->ainsn.insn = get_insn_slot();
if (!p->ainsn.insn)
return -ENOMEM;
@@ -62,6 +86,9 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
flushi(&p->ainsn.insn[1]);
+ p->ainsn.insn[2] = BREAKPOINT_INSTRUCTION_2;
+ flushi(&p->ainsn.insn[2]);
+
p->opcode = *p->addr;
return 0;
}
@@ -295,12 +322,18 @@ static void __kprobes resume_execution(struct kprobe *p,
{
u32 insn = p->ainsn.insn[0];
- regs->tnpc = relbranch_fixup(insn, p, regs, kcb);
+ if (regs->tpc == (unsigned long) &p->ainsn.insn[2]) {
+ /* An untaken branch annulled its delay slot. */
+ regs->tpc = kcb->kprobe_orig_tnpc + 0x4UL;
+ regs->tnpc = kcb->kprobe_orig_tnpc + 0x8UL;
+ } else {
+ regs->tnpc = relbranch_fixup(insn, p, regs, kcb);
- /* This assignment must occur after relbranch_fixup() */
- regs->tpc = kcb->kprobe_orig_tnpc;
+ /* This assignment must occur after relbranch_fixup() */
+ regs->tpc = kcb->kprobe_orig_tnpc;
- retpc_fixup(regs, insn, (unsigned long) p->addr);
+ retpc_fixup(regs, insn, (unsigned long) p->addr);
+ }
regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
kcb->kprobe_orig_tstate_pil);
--
2.43.0
prev parent reply other threads:[~2026-11-13 14:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 14:40 [PATCH v2 0/4] sparc64: kprobes: Make kprobes work on sun4v Stian Halseth
2026-11-13 14:40 ` [PATCH v2 1/4] sparc64: kprobes: Single-step from an executable instruction slot Stian Halseth
2026-11-13 14:40 ` [PATCH v2 2/4] sparc64: kprobes: Fix up register and FP branches after single-step Stian Halseth
2026-11-13 14:40 ` [PATCH v2 3/4] sparc64: kprobes: Resume after a delay slot at the saved next PC Stian Halseth
2026-11-13 14:47 ` sashiko-bot
2026-11-13 14:40 ` Stian Halseth [this message]
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=20261113144057.1697874-5-stian@itx.no \
--to=stian@itx.no \
--cc=andreas@gaisler.com \
--cc=danishkhateeb03@gmail.com \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=naveen@kernel.org \
--cc=sparclinux@vger.kernel.org \
/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