Skip to contentSkip to conversation

openrisc: Add support for KProbes

RFC Superseded Sahil Siddiq 4 participants 21 messages Apr 7 – Oct 13

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover openrisc: Add support for KProbes

Hi,

This series adds basic support for KProbes on OpenRISC. There are a few changes that I would still like to add and test before this can be considered for merging. I was hoping to get some feedback on the changes made so far. The implementation in this series is based on KProbes for LoongArch, MIPS and RISC-V.

The current state of the series allows traps to be inserted dynamically in the kernel. A KProbe can be inserted via a kernel module whose init/exit functions are used to register/unregister a KProbe. A pre- handler and post-handler can also be provisioned in the module, which are associated with the KProbe and triggered when the probe is hit. See the documentation on KProbes for a detailed explanation [1].

The following are yet to be implemented for OpenRISC:
1. kretprobes
2. kprobe-based event tracing
3. ftrace, and kprobe features that depend on ftrace (particularly, dynamic tracing)

I hope to submit a patch for kretprobes soon (possibly in a revision of this series).

I wrote a couple of kernel modules to test these changes. They can be found here [2]. I also ran test_kprobes located at ./lib/tests/ against these changes [3]. The results are as shown below:

/home # insmod test_kprobes.ko
KTAP version 1
1..1
KTAP version 1
# Subtest: kprobes_test
# module: test_kprobes
1..3
ok 1 test_kprobe
ok 2 test_kprobes
ok 3 test_kprobe_missed
# kprobes_test: pass:3 fail:0 skip:0 total:3
# Totals: pass:3 fail:0 skip:0 total:3
ok 1 kprobes_test
/home #

When compiling the kernel, the following options should be enabled:
1. CONFIG_HAVE_KPROBES=y
2. CONFIG_KPROBES=y

Also ensure that CONFIG_KPROBE_EVENTS is disabled.

To compile /lib/tests/test_kprobes.c, add the following to .config:
1. CONFIG_KUNIT=y
2. CONFIG_DEBUG_KERNEL=y
3. CONFIG_KPROBES_SANITY_TEST=m

The first commit cleans up and reorganizes existing functions, fixes a few issues with instruction simulation, and introduces new structures and macros that will be used by KProbes and other tracing facilities in the future.

The second commit adds support for KProbes. Currently, I have implemented this in such a way that KProbes can't be used to probe a few "blacklisted" instructions. Probes can't be inserted in a delay slot either (similar to MIPS). I have also added a few asm functions to the blacklist that I think should not be probed. For e.g., "memset" and "_trap_handler" have been blacklisted because probing them causes the kernel to hang. However, I am not sure if other functions in "entry.S" need to be added as well to the blacklist.

Thanks,
Sahil

[1] https://www.kernel.org/doc/html/latest/trace/kprobes.html
[2] https://github.com/valdaarhun/or-dev/tree/main/home
[3] https://github.com/openrisc/linux/blob/for-next/lib/tests/test_kprobes.c

Sahil Siddiq (2):
openrisc: Add utilities and clean up simulation of instructions
openrisc: Add KProbes

 arch/openrisc/Kconfig                   |   1 +
 arch/openrisc/configs/or1ksim_defconfig |   2 +
 arch/openrisc/configs/virt_defconfig    |   2 +
 arch/openrisc/include/asm/asm.h         |  22 ++
 arch/openrisc/include/asm/break.h       |  19 ++
 arch/openrisc/include/asm/insn-def.h    |  61 +++-
 arch/openrisc/include/asm/kprobes.h     |  76 +++++
 arch/openrisc/include/asm/spr_defs.h    |   1 +
 arch/openrisc/kernel/Makefile           |   3 +-
 arch/openrisc/kernel/entry.S            |  16 +
 arch/openrisc/kernel/insn.c             |  74 +++++
 arch/openrisc/kernel/jump_label.c       |   2 +-
 arch/openrisc/kernel/kprobes.c          | 381 ++++++++++++++++++++++++
 arch/openrisc/kernel/traps.c            |  67 ++---
 arch/openrisc/lib/memcpy.c              |   2 +
 arch/openrisc/lib/memset.S              |   4 +
 arch/openrisc/mm/fault.c                |   5 +
 samples/kprobes/kprobe_example.c        |   8 +
 18 files changed, 701 insertions(+), 45 deletions(-)
 create mode 100644 arch/openrisc/include/asm/asm.h
 create mode 100644 arch/openrisc/include/asm/break.h
 create mode 100644 arch/openrisc/include/asm/kprobes.h
 create mode 100644 arch/openrisc/kernel/insn.c
 create mode 100644 arch/openrisc/kernel/kprobes.c
Signature · 1 line
2.53.0
#1 lore raw

RFC 1/2 openrisc: Add utilities and clean up simulation of instructions

Introduce new instruction-related utilities and macros for OpenRISC. This is in preparation for patches that add tracing support such as KProbes.

Simulate l.adrp. Fix bugs in simulation of l.jal and l.jalr. Earlier, PC was being updated and then saved in the link register r9, resulting in a corrupted page table (bad page map in process). Instead, update PC after storing it in r9.

Move instruction simulation to its own file to enable reuse. Clean it up and replace hardcoded values with computed expressions.

---
 arch/openrisc/include/asm/insn-def.h | 61 +++++++++++++++++++++--
 arch/openrisc/include/asm/spr_defs.h |  1 +
 arch/openrisc/kernel/Makefile        |  2 +-
 arch/openrisc/kernel/insn.c          | 74 ++++++++++++++++++++++++++++
 arch/openrisc/kernel/jump_label.c    |  2 +-
 arch/openrisc/kernel/traps.c         | 41 +--------------
 6 files changed, 136 insertions(+), 45 deletions(-)
 create mode 100644 arch/openrisc/kernel/insn.c
arch/openrisc/include/asm/insn-def.h +57 -4
index 1e0c028a5b95..c98f9770c52e 100644
@@ -3,13 +3,66 @@
  * Copyright (C) 2025 Chen Miao
  */
 
+#include <asm/spr.h>
+#include <asm/spr_defs.h>
+
 #ifndef __ASM_OPENRISC_INSN_DEF_H
 #define __ASM_OPENRISC_INSN_DEF_H
 
-/* or1k instructions are always 32 bits. */
-#define	OPENRISC_INSN_SIZE		4
-
 /* or1k nop instruction code */
-#define OPENRISC_INSN_NOP     0x15000000U
+#define INSN_NOP	0x15000000U
+
+#define INSN_CSYNC	0x23000000U
+#define INSN_MSYNC	0x22000000U
+#define INSN_PSYNC	0x22800000U
+
+#define OPCODE_TRAP	0x21000000U
+#define OPCODE_SYS	0x20000000U
+#define OPCODE_MACRC	0x18010000U
+
+struct pt_regs;
+
+enum six_bit_opcodes {
+	l_rfe = 0x09,
+	l_lwa = 0x1b,
+	l_mfspr = 0x2d,
+	l_mtspr = 0x30,
+	l_swa = 0x33,
+	l_j = 0x00,
+	l_jal = 0x01,
+	l_adrp = 0x02,
+	l_bnf = 0x03,
+	l_bf = 0x04,
+	l_jr = 0x11,
+	l_jalr = 0x12,
+};
+
+struct insn {
+	unsigned int opcode: 6;
+	unsigned int operands: 26;
+};
+
+union openrisc_instruction {
+	unsigned int word;
+	struct insn opcodes_6bit;
+};
+
+#define OPENRISC_INSN_SIZE  (sizeof(union openrisc_instruction))
+
+/* Helpers for working with l.trap */
+static inline unsigned long __emit_trap(unsigned int code)
+{
+	return (code & 0xffff) | OPCODE_TRAP;
+}
+
+static inline bool has_delay_slot(void)
+{
+	unsigned int cpucfgr = mfspr(SPR_CPUCFGR);
+
+	return !(cpucfgr & SPR_CPUCFGR_ND);
+}
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp);
+void simulate_branch(struct pt_regs *regs, unsigned int jmp, bool has_delay_slot);
 
 #endif /* __ASM_OPENRISC_INSN_DEF_H */
arch/openrisc/include/asm/spr_defs.h +1 -0
index f0b6b492e9f4..5d13a9b96263 100644
@@ -179,6 +179,7 @@
 #define SPR_CPUCFGR_OF32S  0x00000080  /* ORFPX32 supported */
 #define SPR_CPUCFGR_OF64S  0x00000100  /* ORFPX64 supported */
 #define SPR_CPUCFGR_OV64S  0x00000200  /* ORVDX64 supported */
+#define SPR_CPUCFGR_ND     0x00000400  /* No delay slot */
 #define SPR_CPUCFGR_RES	   0xfffffc00  /* Reserved */
 
 /*
arch/openrisc/kernel/Makefile +1 -1
index 19e0eb94f2eb..150779fbf010 100644
@@ -5,7 +5,7 @@
 
 always-$(KBUILD_BUILTIN)	:= vmlinux.lds
 
-obj-y	:= head.o setup.o or32_ksyms.o process.o dma.o \
+obj-y	:= head.o insn.o setup.o or32_ksyms.o process.o dma.o \
 	   traps.o time.o irq.o entry.o ptrace.o signal.o \
 	   sys_call_table.o unwinder.o cacheinfo.o
 
arch/openrisc/kernel/insn.c new file +74 -0

74 lines not on this page · raw message

arch/openrisc/kernel/jump_label.c +1 -1
index ab7137c23b46..fe082eb847a4 100644
@@ -34,7 +34,7 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
 
 		insn = offset;
 	} else {
-		insn = OPENRISC_INSN_NOP;
+		insn = INSN_NOP;
 	}
 
 	if (early_boot_irqs_disabled)
arch/openrisc/kernel/traps.c +2 -39

55 lines not on this page · raw message

Signature · 1 line
2.53.0
#18 lore raw

RFC 2/2 openrisc: Add KProbes

In reply to Sahil Siddiq

Add KProbes support for OpenRISC. This work is primarily based on similar work done for LoongArch, MIPS and RISC-V.

KProbes make it possible to trap at almost any address in the kernel to collect performance/debugging info.

---
 arch/openrisc/Kconfig                   |   1 +
 arch/openrisc/configs/or1ksim_defconfig |   2 +
 arch/openrisc/configs/virt_defconfig    |   2 +
 arch/openrisc/include/asm/asm.h         |  22 ++
 arch/openrisc/include/asm/break.h       |  19 ++
 arch/openrisc/include/asm/kprobes.h     |  76 +++++
 arch/openrisc/kernel/Makefile           |   1 +
 arch/openrisc/kernel/entry.S            |  16 +
 arch/openrisc/kernel/kprobes.c          | 381 ++++++++++++++++++++++++
 arch/openrisc/kernel/traps.c            |  26 ++
 arch/openrisc/lib/memcpy.c              |   2 +
 arch/openrisc/lib/memset.S              |   4 +
 arch/openrisc/mm/fault.c                |   5 +
 samples/kprobes/kprobe_example.c        |   8 +
 14 files changed, 565 insertions(+)
 create mode 100644 arch/openrisc/include/asm/asm.h
 create mode 100644 arch/openrisc/include/asm/break.h
 create mode 100644 arch/openrisc/include/asm/kprobes.h
 create mode 100644 arch/openrisc/kernel/kprobes.c
arch/openrisc/Kconfig +1 -0
index 9156635dd264..d240533b424b 100644
@@ -27,6 +27,7 @@ config OPENRISC
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_PCI
+	select HAVE_KPROBES
 	select HAVE_UID16
 	select HAVE_PAGE_SIZE_8KB
 	select HAVE_REGS_AND_STACK_ACCESS_API
arch/openrisc/configs/or1ksim_defconfig +2 -0
index 769705ac24d5..24d2915e7609 100644
@@ -10,7 +10,9 @@ CONFIG_EXPERT=y
 # CONFIG_KALLSYMS is not set
 CONFIG_BUILTIN_DTB_NAME="or1ksim"
 CONFIG_HZ_100=y
+CONFIG_OPENRISC=y
 CONFIG_JUMP_LABEL=y
+CONFIG_KPROBES=y
 CONFIG_MODULES=y
 # CONFIG_BLOCK is not set
 CONFIG_SLUB_TINY=y
arch/openrisc/configs/virt_defconfig +2 -0
index 0b9979b35ca8..2eccb506032f 100644
@@ -11,8 +11,10 @@ CONFIG_OPENRISC_HAVE_INST_SEXT=y
 CONFIG_NR_CPUS=8
 CONFIG_SMP=y
 CONFIG_HZ_100=y
+CONFIG_OPENRISC=y
 # CONFIG_OPENRISC_NO_SPR_SR_DSX is not set
 CONFIG_JUMP_LABEL=y
+CONFIG_KPROBES=y
 # CONFIG_COMPAT_BRK is not set
 CONFIG_NET=y
 CONFIG_PACKET=y
arch/openrisc/include/asm/asm.h new file +22 -0
new file mode 100644
index 000000000000..1a9c8bbb4430
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Macros for OpenRISC asm
+ *
+ * Linux architectural port borrowing nearly verbatim from
+ * LoongArch and Arm. All original copyrights apply as per
+ * the original source declaration.
+ */
+
+#ifndef __ASM_ASM_H
+#define __ASM_ASM_H
+
+#ifdef CONFIG_KPROBES
+#define _ASM_NOKPROBE(symbol)			\
+	.pushsection "_kprobe_blacklist", "aw";	\
+	.long symbol;				\
+	.popsection
+#else
+#define _ASM_NOKPROBE(symbol)
+#endif
+
+#endif /* __ASM_ASM_H */
arch/openrisc/include/asm/break.h new file +19 -0
new file mode 100644
index 000000000000..4bd04f4dd17a
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * OpenRISC trap codes used internally by the kernel
+ *
+ * Linux architectural port borrowing liberally from similar works of
+ * others.  All original copyrights apply as per the original source
+ * declaration.
+ *
+ * Modifications for the OpenRISC architecture:
+ * Copyright (C) 2026 Sahil Siddiq <[email protected]>
+ */
+
+#ifndef __ASM_BREAK_H
+#define __ASM_BREAK_H
+
+#define BRK_KPROBE_BP       512     /* Kprobe break */
+#define BRK_KPROBE_SSTEPBP  1024    /* Kprobe single-step software implementation */
+
+#endif /* __ASM_BREAK_H */
arch/openrisc/include/asm/kprobes.h new file +76 -0

76 lines not on this page · raw message

arch/openrisc/kernel/Makefile +1 -0
index 150779fbf010..2ac824867963 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_SMP)		+= smp.o sync-timer.o
 obj-$(CONFIG_STACKTRACE)	+= stacktrace.o
 obj-$(CONFIG_MODULES)		+= module.o
 obj-$(CONFIG_OF)		+= prom.o
+obj-$(CONFIG_KPROBES)		+= kprobes.o
 obj-y	+= patching.o
 
 clean:
arch/openrisc/kernel/entry.S +16 -0
index c7e90b09645e..cd28bf1f7a3b 100644
@@ -15,6 +15,7 @@
 #include <linux/linkage.h>
 #include <linux/pgtable.h>
 
+#include <asm/asm.h>
 #include <asm/processor.h>
 #include <asm/unistd.h>
 #include <asm/thread_info.h>
@@ -640,6 +641,7 @@ ENTRY(_sys_call_handler)
 	/* r30 is the only register we clobber in the fast path */
 	/* r30 already saved */
 /*	l.sw    PT_GPR30(r1),r30 */
+_ASM_NOKPROBE(_sys_call_handler)
 
 _syscall_check_trace_enter:
 	/* syscalls run with interrupts enabled */
@@ -652,12 +654,14 @@ _syscall_check_trace_enter:
 	l.sfne	r30,r0
 	l.bf	_syscall_trace_enter
 	 l.nop
+_ASM_NOKPROBE(_syscall_check_trace_enter)
 
 _syscall_check:
 	/* Ensure that the syscall number is reasonable */
 	l.sfgeui r11,__NR_syscalls
 	l.bf	_syscall_badsys
 	 l.nop
+_ASM_NOKPROBE(_syscall_check)
 
 _syscall_call:
 	l.movhi r29,hi(sys_call_table)
@@ -668,12 +672,14 @@ _syscall_call:
 
 	l.jalr  r29
 	 l.nop
+_ASM_NOKPROBE(_syscall_call)
 
 _syscall_return:
 	/* All syscalls return here... just pay attention to ret_from_fork
 	 * which does it in a round-about way.
 	 */
 	l.sw    PT_GPR11(r1),r11           // save return value
+_ASM_NOKPROBE(_syscall_return)
 
 #if 0
 _syscall_debug:
@@ -708,6 +714,7 @@ _syscall_check_trace_leave:
 	l.sfne	r30,r0
 	l.bf	_syscall_trace_leave
 	 l.nop
+_ASM_NOKPROBE(_syscall_check_trace_leave)
 
 /* This is where the exception-return code begins... interrupts need to be
  * disabled the rest of the way here because we can't afford to miss any
@@ -744,6 +751,7 @@ _syscall_check_work:
 	/* _work_pending needs to be called with interrupts disabled */
 	l.j	_work_pending
 	 l.nop
+_ASM_NOKPROBE(_syscall_check_work)
 
 _syscall_resume_userspace:
 //	ENABLE_INTERRUPTS(r29)
@@ -800,6 +808,7 @@ _syscall_resume_userspace:
 	l.mtspr r0,r13,SPR_EPCR_BASE
 	l.mtspr r0,r15,SPR_ESR_BASE
 	l.rfe
+_ASM_NOKPROBE(_syscall_resume_userspace)
 
 /* End of hot path!
  * Keep the below tracing and error handling out of the hot path...
@@ -829,6 +838,7 @@ _syscall_trace_enter:
 
 	l.j	_syscall_check
 	 l.lwz	r8,PT_GPR8(r1)
+_ASM_NOKPROBE(_syscall_trace_enter)
 
 _syscall_trace_leave:
 	l.jal	do_syscall_trace_leave
@@ -836,6 +846,7 @@ _syscall_trace_leave:
 
 	l.j	_syscall_check_work
 	 l.nop
+_ASM_NOKPROBE(_syscall_trace_leave)
 
 _syscall_badsys:
 	/* Here we effectively pretend to have executed an imaginary
@@ -845,6 +856,7 @@ _syscall_badsys:
 	 */
 	l.j	_syscall_return
 	 l.addi	r11,r0,-ENOSYS
+_ASM_NOKPROBE(_syscall_badsys)
 
 /******* END SYSCALL HANDLING *******/
 
@@ -870,6 +882,7 @@ EXCEPTION_ENTRY(_trap_handler)
 
 	l.j     _ret_from_exception
 	 l.nop
+_ASM_NOKPROBE(_trap_handler)
 
 /* ---[ 0xf00: Reserved exception ]-------------------------------------- */
 
@@ -1004,6 +1017,8 @@ ENTRY(_ret_from_exception)
 	 l.nop
 	l.j	_resume_userspace
 	 l.nop
+_ASM_NOKPROBE(_ret_from_exception)
+_ASM_NOKPROBE(_ret_from_intr)
 
 ENTRY(ret_from_fork)
 	l.jal	schedule_tail
@@ -1038,6 +1053,7 @@ ENTRY(ret_from_fork)
 
 	l.j	_syscall_return
 	 l.nop
+_ASM_NOKPROBE(ret_from_fork)
 
 /* ========================================================[ switch ] === */
 
arch/openrisc/kernel/kprobes.c new file +381 -0

381 lines not on this page · raw message

arch/openrisc/kernel/traps.c +26 -0
index ee87a3af34fc..ae6bfcca388e 100644
@@ -30,10 +30,12 @@
 #include <linux/kallsyms.h>
 #include <linux/uaccess.h>
 
+#include <asm/break.h>
 #include <asm/bug.h>
 #include <asm/fpu.h>
 #include <asm/insn-def.h>
 #include <asm/io.h>
+#include <asm/kprobes.h>
 #include <asm/processor.h>
 #include <asm/unwinder.h>
 #include <asm/sections.h>
@@ -216,10 +218,34 @@ asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
 	if (user_mode(regs)) {
 		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->pc);
 	} else {
+		unsigned int trap = *(unsigned int *)regs->pc, cpucfgr = mfspr(SPR_CPUCFGR), bcode;
+
+		/*
+		 * Trap instruction was probably removed and no further processing
+		 * is required.
+		 */
+		if ((trap & 0xffff0000) != OPCODE_TRAP)
+			return;
+
+		bcode = (trap & 0xffff);
+		switch (bcode) {
+		case BRK_KPROBE_BP:
+			if (kprobe_breakpoint_handler(regs))
+				return;
+			break;
+		case BRK_KPROBE_SSTEPBP:
+			if (kprobe_singlestep_handler(regs))
+				return;
+			break;
+		default:
+			break;
+		}
+
 		pr_emerg("KERNEL: Illegal trap exception 0x%.8lx\n", regs->pc);
 		die("Die:", regs, SIGILL);
 	}
 }
+NOKPROBE_SYMBOL(do_trap)
 
 asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address)
 {
arch/openrisc/lib/memcpy.c +2 -0
index e2af9b510804..701262a40134 100644
@@ -16,6 +16,7 @@
 
 #include <linux/export.h>
 
+#include <linux/kprobes.h>
 #include <linux/string.h>
 
 #ifdef CONFIG_OR1K_1200
@@ -122,4 +123,5 @@ void *memcpy(void *dest, __const void *src, __kernel_size_t n)
 }
 #endif
 
+NOKPROBE_SYMBOL(memcpy);
 EXPORT_SYMBOL(memcpy);
arch/openrisc/lib/memset.S +4 -0
index c3ac2a8b68d3..14e63af12d73 100644
@@ -9,6 +9,8 @@
  * Copyright (C) 2015 Olof Kindgren <[email protected]>
  */
 
+#include <asm/asm.h>
+
 	.global memset
 	.type	memset, @function
 memset:
@@ -92,3 +94,5 @@ memset:
 
 4:	l.jr		r9
 	 l.ori		r11, r3, 0
+
+_ASM_NOKPROBE(memset)
arch/openrisc/mm/fault.c +5 -0
index 29e232d78d82..5263a832562f 100644
@@ -14,6 +14,7 @@
 #include <linux/mm.h>
 #include <linux/interrupt.h>
 #include <linux/extable.h>
+#include <linux/kprobes.h>
 #include <linux/sched/signal.h>
 #include <linux/perf_event.h>
 
@@ -55,6 +56,9 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
 
 	tsk = current;
 
+	if (kprobe_page_fault(regs, vector))
+		return;
+
 	/*
 	 * We fault-in kernel-space virtual memory on-demand. The
 	 * 'reference' page table is init_mm.pgd.
@@ -351,3 +355,4 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
 		return;
 	}
 }
+NOKPROBE_SYMBOL(do_page_fault)
samples/kprobes/kprobe_example.c +8 -0
index 53ec6c8b8c40..84e26ebef70b 100644
@@ -59,6 +59,10 @@ static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
 	pr_info("<%s> p->addr = 0x%p, era = 0x%lx, estat = 0x%lx\n",
 		p->symbol_name, p->addr, regs->csr_era, regs->csr_estat);
 #endif
+#ifdef CONFIG_OPENRISC
+	pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, status = 0x%lx\n",
+		p->symbol_name, p->addr, regs->pc, regs->sr);
+#endif
 
 	/* A dump_stack() here will give a stack backtrace */
 	return 0;
@@ -100,6 +104,10 @@ static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
 	pr_info("<%s> p->addr = 0x%p, estat = 0x%lx\n",
 		p->symbol_name, p->addr, regs->csr_estat);
 #endif
+#ifdef CONFIG_OPENRISC
+	pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n",
+		p->symbol_name, p->addr, regs->sr);
+#endif
 }
 
 static int __init kprobe_init(void)
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [RFC 1/2] openrisc: Add utilities and clean up simulation of instructions

In reply to Sahil Siddiq

Hello Sahil,

Thanks for your patches. I have a few questions. so this is not really a technical review at the moment just some main items.

Quoted 5 lines · Sahil Siddiq

Introduce new instruction-related utilities and macros for OpenRISC. This is in preparation for patches that add tracing support such as KProbes.

Simulate l.adrp. Fix bugs in simulation of l.jal and l.jalr. Earlier,

Why emulate l.adrp? We don't really use this yet in any OpenRISC code. This instruction is meant to be used to help -fpic code, see:

https://openrisc.io/proposals/ladrp

Quoted 29 lines

PC was being updated and then saved in the link register r9, resulting in a corrupted page table (bad page map in process). Instead, update PC after storing it in r9.

Move instruction simulation to its own file to enable reuse. Clean it up and replace hardcoded values with computed expressions.

---
 arch/openrisc/include/asm/insn-def.h | 61 +++++++++++++++++++++--
 arch/openrisc/include/asm/spr_defs.h |  1 +
 arch/openrisc/kernel/Makefile        |  2 +-
 arch/openrisc/kernel/insn.c          | 74 ++++++++++++++++++++++++++++
 arch/openrisc/kernel/jump_label.c    |  2 +-
 arch/openrisc/kernel/traps.c         | 41 +--------------
 6 files changed, 136 insertions(+), 45 deletions(-)
 create mode 100644 arch/openrisc/kernel/insn.c
arch/openrisc/include/asm/insn-def.h +2 -0
index 1e0c028a5b95..c98f9770c52e 100644
@@ -3,13 +3,66 @@
  * Copyright (C) 2025 Chen Miao
  */
 
+#include <asm/spr.h>
+#include <asm/spr_defs.h>

arch/openrisc/include/asm/insn-def.h line 13

Quoted 10 lines
arch/openrisc/include/asm/insn-def.h +2 -4
+
 #ifndef __ASM_OPENRISC_INSN_DEF_H
 #define __ASM_OPENRISC_INSN_DEF_H
 
-/* or1k instructions are always 32 bits. */
-#define	OPENRISC_INSN_SIZE		4
-
 /* or1k nop instruction code */
-#define OPENRISC_INSN_NOP     0x15000000U
+#define INSN_NOP	0x15000000U

Why remove the OPENRISC_ namespace from thse definitions?

Other architectures like arm64, riscv have namespaces on these similar definitions.

arch/openrisc/include/asm/insn-def.h line 25

Quoted 12 lines
arch/openrisc/include/asm/insn-def.h +12 -0
+
+#define INSN_CSYNC	0x23000000U
+#define INSN_MSYNC	0x22000000U
+#define INSN_PSYNC	0x22800000U
+
+#define OPCODE_TRAP	0x21000000U
+#define OPCODE_SYS	0x20000000U
+#define OPCODE_MACRC	0x18010000U
+
+struct pt_regs;
+
+enum six_bit_opcodes {

I don't see why we need to name this six_bit as such. OpenRISC instructions have either fir most significant 6-bits or 11-bits as opcodes but we still use the first 6-bits to drive the logic.

Quoted 28 lines
arch/openrisc/include/asm/insn-def.h +28 -0
+	l_rfe = 0x09,
+	l_lwa = 0x1b,
+	l_mfspr = 0x2d,
+	l_mtspr = 0x30,
+	l_swa = 0x33,
+	l_j = 0x00,
+	l_jal = 0x01,
+	l_adrp = 0x02,
+	l_bnf = 0x03,
+	l_bf = 0x04,
+	l_jr = 0x11,
+	l_jalr = 0x12,
+};
+
+struct insn {
+	unsigned int opcode: 6;
+	unsigned int operands: 26;
+};
+
+union openrisc_instruction {
+	unsigned int word;
+	struct insn opcodes_6bit;
+};
+
+#define OPENRISC_INSN_SIZE  (sizeof(union openrisc_instruction))
+
+/* Helpers for working with l.trap */
+static inline unsigned long __emit_trap(unsigned int code)

arch/openrisc/include/asm/insn-def.h line 63

Quoted 10 lines
arch/openrisc/include/asm/insn-def.h +10 -0
+{
+	return (code & 0xffff) | OPCODE_TRAP;
+}
+
+static inline bool has_delay_slot(void)
+{
+	unsigned int cpucfgr = mfspr(SPR_CPUCFGR);
+
+	return !(cpucfgr & SPR_CPUCFGR_ND);
+}

This is for handling CPU's that do not have delay slots. We didn't do this before, why are you doing it now? Should we mention this in the git commit message?

Also, since this is a static configuration for the CPU should we use static keys for this?

Quoted 58 lines
arch/openrisc/include/asm/insn-def.h +3 -0
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp);
+void simulate_branch(struct pt_regs *regs, unsigned int jmp, bool has_delay_slot);
 
 #endif /* __ASM_OPENRISC_INSN_DEF_H */
arch/openrisc/include/asm/spr_defs.h +1 -0
index f0b6b492e9f4..5d13a9b96263 100644
@@ -179,6 +179,7 @@
 #define SPR_CPUCFGR_OF32S  0x00000080  /* ORFPX32 supported */
 #define SPR_CPUCFGR_OF64S  0x00000100  /* ORFPX64 supported */
 #define SPR_CPUCFGR_OV64S  0x00000200  /* ORVDX64 supported */
+#define SPR_CPUCFGR_ND     0x00000400  /* No delay slot */
 #define SPR_CPUCFGR_RES	   0xfffffc00  /* Reserved */
 
 /*
arch/openrisc/kernel/Makefile +1 -1
index 19e0eb94f2eb..150779fbf010 100644
@@ -5,7 +5,7 @@
 
 always-$(KBUILD_BUILTIN)	:= vmlinux.lds
 
-obj-y	:= head.o setup.o or32_ksyms.o process.o dma.o \
+obj-y	:= head.o insn.o setup.o or32_ksyms.o process.o dma.o \
 	   traps.o time.o irq.o entry.o ptrace.o signal.o \
 	   sys_call_table.o unwinder.o cacheinfo.o
 
arch/openrisc/kernel/insn.c new file +22 -0
new file mode 100644
index 000000000000..2c97eceee6d7
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * OpenRISC instruction utils
+ *
+ * Linux architectural port borrowing liberally from similar works of
+ * others.  All original copyrights apply as per the original source
+ * declaration.
+ *
+ * OpenRISC implementation:
+ * Copyright (C) 2026 Sahil Siddiq <[email protected]>
+ */
+
+#include <linux/ptrace.h>
+#include <asm/insn-def.h>
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp)
+{
+	int displacement;
+	unsigned int rd, op;
+
+	displacement = sign_extend32(((jmp) & 0x7ffff) << 13, 31);
+	rd = (jmp & 0x3ffffff) >> 21;

arch/openrisc/kernel/insn.c line 32

Quoted 10 lines
arch/openrisc/kernel/insn.c new file +10 -0
+	op = jmp >> 26;
+
+	switch (op) {
+	case l_adrp:
+		regs->gpr[rd] = displacement + (regs->pc & (-8192));
+		return;
+	default:
+		break;
+	}
+}

This function is not used, what will it be used for? Could you mention it in the commit message? Also could you document it?

arch/openrisc/kernel/insn.c line 34

Quoted 2 lines
arch/openrisc/kernel/insn.c new file +2 -0
+
+void simulate_branch(struct pt_regs *regs, unsigned int jmp_insn, bool has_delay_slot)

Since you are using this for more than just instruction simulation now, maybe its good to document what it does. For example when we use it below we surround it with the in_delay_slot check.

arch/openrisc/kernel/insn.c line 41

Quoted 7 lines
arch/openrisc/kernel/insn.c new file +7 -0
+{
+	int displacement;
+	unsigned int rb, op, jmp;
+
+	displacement = sign_extend32(((jmp_insn) & 0x3ffffff) << 2, 27);
+	rb = (jmp_insn & 0x0000ffff) >> 11;
+	op = jmp_insn >> 26;

Naming this variable jmp is a bit misleading. Maybe something like link_displacement, link_offset or fallthrough?

Quoted 110 lines
arch/openrisc/kernel/insn.c new file +33 -0
+	jmp = has_delay_slot ? 2 * OPENRISC_INSN_SIZE : OPENRISC_INSN_SIZE;
+
+	switch (op) {
+	case l_j: /* l.j */
+		regs->pc += displacement;
+		return;
+	case l_jal: /* l.jal */
+		regs->gpr[9] = regs->pc + jmp;
+		regs->pc += displacement;
+		return;
+	case l_bnf: /* l.bnf */
+		if (regs->sr & SPR_SR_F)
+			regs->pc += jmp;
+		else
+			regs->pc += displacement;
+		return;
+	case l_bf: /* l.bf */
+		if (regs->sr & SPR_SR_F)
+			regs->pc += displacement;
+		else
+			regs->pc += jmp;
+		return;
+	case l_jr: /* l.jr */
+		regs->pc = regs->gpr[rb];
+		return;
+	case l_jalr: /* l.jalr */
+		regs->gpr[9] = regs->pc + jmp;
+		regs->pc = regs->gpr[rb];
+		return;
+	default:
+		break;
+	}
+}
arch/openrisc/kernel/jump_label.c +1 -1
index ab7137c23b46..fe082eb847a4 100644
@@ -34,7 +34,7 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
 
 		insn = offset;
 	} else {
-		insn = OPENRISC_INSN_NOP;
+		insn = INSN_NOP;
 	}
 
 	if (early_boot_irqs_disabled)
arch/openrisc/kernel/traps.c +2 -39
index c195be9cc9fc..ee87a3af34fc 100644
@@ -32,6 +32,7 @@
 
 #include <asm/bug.h>
 #include <asm/fpu.h>
+#include <asm/insn-def.h>
 #include <asm/io.h>
 #include <asm/processor.h>
 #include <asm/unwinder.h>
@@ -269,47 +270,9 @@ static inline int in_delay_slot(struct pt_regs *regs)
 
 static inline void adjust_pc(struct pt_regs *regs, unsigned long address)
 {
-	int displacement;
-	unsigned int rb, op, jmp;
-
 	if (unlikely(in_delay_slot(regs))) {
 		/* In delay slot, instruction at pc is a branch, simulate it */
-		jmp = *((unsigned int *)regs->pc);
-
-		displacement = sign_extend32(((jmp) & 0x3ffffff) << 2, 27);
-		rb = (jmp & 0x0000ffff) >> 11;
-		op = jmp >> 26;
-
-		switch (op) {
-		case 0x00: /* l.j */
-			regs->pc += displacement;
-			return;
-		case 0x01: /* l.jal */
-			regs->pc += displacement;
-			regs->gpr[9] = regs->pc + 8;
-			return;
-		case 0x03: /* l.bnf */
-			if (regs->sr & SPR_SR_F)
-				regs->pc += 8;
-			else
-				regs->pc += displacement;
-			return;
-		case 0x04: /* l.bf */
-			if (regs->sr & SPR_SR_F)
-				regs->pc += displacement;
-			else
-				regs->pc += 8;
-			return;
-		case 0x11: /* l.jr */
-			regs->pc = regs->gpr[rb];
-			return;
-		case 0x12: /* l.jalr */
-			regs->pc = regs->gpr[rb];
-			regs->gpr[9] = regs->pc + 8;
-			return;
-		default:
-			break;
-		}
+		simulate_branch(regs, *((unsigned int *)regs->pc), has_delay_slot());
 	} else {
 		regs->pc += 4;
 	}
Signature · 1 line
2.53.0
author
#3 lore raw

Re: [RFC 1/2] openrisc: Add utilities and clean up simulation of instructions

Hi Stafford,

Thank you for your review.

Quoted 10 lines · Stafford Horne

Hello Sahil,

Thanks for your patches. I have a few questions. so this is not really a technical review at the moment just some main items.

Quoted 4 lines · Sahil Siddiq

Introduce new instruction-related utilities and macros for OpenRISC. This is in preparation for patches that add tracing support such as KProbes.

Quoted 6 lines
Quoted 1 line

Simulate l.adrp. Fix bugs in simulation of l.jal and l.jalr. Earlier,

Why emulate l.adrp? We don't really use this yet in any OpenRISC code. This instruction is meant to be used to help -fpic code, see:

https://openrisc.io/proposals/ladrp

Right, I couldn't find l.adrp in the kernel's image after compilation. But I was thinking, in case l.adrp is used somewhere in the future (for e.g. in a custom kernel module) and a probe is placed at that instruction, we would need to emulate that instruction. This is because the target address is calculated using the instruction's address (InsnAddr & -8192). If l.adrp is moved to an out-of-line slot to single-step for KProbes, the slot's address will be used to generate the target address instead of the original address.

Since l.adrp is not really used anywhere and it's highly unlikely that it will be probed, an alternative could be to add l.adrp to the blacklist so that probes at this instruction are never inserted.

Quoted 42 lines
Quoted 29 lines

PC was being updated and then saved in the link register r9, resulting in a corrupted page table (bad page map in process). Instead, update PC after storing it in r9.

Move instruction simulation to its own file to enable reuse. Clean it up and replace hardcoded values with computed expressions.

---
  arch/openrisc/include/asm/insn-def.h | 61 +++++++++++++++++++++--
  arch/openrisc/include/asm/spr_defs.h |  1 +
  arch/openrisc/kernel/Makefile        |  2 +-
  arch/openrisc/kernel/insn.c          | 74 ++++++++++++++++++++++++++++
  arch/openrisc/kernel/jump_label.c    |  2 +-
  arch/openrisc/kernel/traps.c         | 41 +--------------
  6 files changed, 136 insertions(+), 45 deletions(-)

create mode 100644 arch/openrisc/kernel/insn.c

arch/openrisc/include/asm/insn-def.h +2 -0
index 1e0c028a5b95..c98f9770c52e 100644
@@ -3,13 +3,66 @@
   * Copyright (C) 2025 Chen Miao
   */
 
+#include <asm/spr.h>
+#include <asm/spr_defs.h>

arch/openrisc/include/asm/insn-def.h line 13

Quoted 10 lines
arch/openrisc/include/asm/insn-def.h +2 -4
+
  #ifndef __ASM_OPENRISC_INSN_DEF_H
  #define __ASM_OPENRISC_INSN_DEF_H
 
-/* or1k instructions are always 32 bits. */
-#define	OPENRISC_INSN_SIZE		4
-
  /* or1k nop instruction code */
-#define OPENRISC_INSN_NOP     0x15000000U
+#define INSN_NOP	0x15000000U

Why remove the OPENRISC_ namespace from thse definitions?

Quoted 2 lines

Other architectures like arm64, riscv have namespaces on these similar definitions.

Oh, got it. I hadn't checked those architectures at that point. I was following the convention in LoongArch and the namespace wasn't there. I'll revert this change.

Quoted 8 lines

arch/openrisc/include/asm/insn-def.h line 21

Quoted 8 lines
arch/openrisc/include/asm/insn-def.h +8 -0
+
+#define INSN_CSYNC	0x23000000U
+#define INSN_MSYNC	0x22000000U
+#define INSN_PSYNC	0x22800000U
+
+#define OPCODE_TRAP	0x21000000U
+#define OPCODE_SYS	0x20000000U
+#define OPCODE_MACRC	0x18010000U

I'll add the namespace here.

Quoted 7 lines

arch/openrisc/include/asm/insn-def.h line 25

Quoted 3 lines
arch/openrisc/include/asm/insn-def.h +3 -0
+struct pt_regs;
+
+enum six_bit_opcodes {

I don't see why we need to name this six_bit as such. OpenRISC instructions have either fir most significant 6-bits or 11-bits as opcodes but we still use the first 6-bits to drive the logic.

Maybe it should be renamed to something that indicates this is related to probes (I wasn't able to come up with a better name).

The enums are used in a few places in KProbe's implementation to check if an instruction requires emulation, has been blacklisted, or can be single-stepped in an out-of-line slot. An alternative would be to use macros (similar to OPCODE_SYS) but that won't have type checking. Either way, I think it's better than using raw values in multiple places.

Shall I simply rename the enum type, or shall I switch to macros? What are your thoughts?

Quoted 34 lines
Quoted 34 lines
arch/openrisc/include/asm/insn-def.h +34 -0
+	l_rfe = 0x09,
+	l_lwa = 0x1b,
+	l_mfspr = 0x2d,
+	l_mtspr = 0x30,
+	l_swa = 0x33,
+	l_j = 0x00,
+	l_jal = 0x01,
+	l_adrp = 0x02,
+	l_bnf = 0x03,
+	l_bf = 0x04,
+	l_jr = 0x11,
+	l_jalr = 0x12,
+};
+
+struct insn {
+	unsigned int opcode: 6;
+	unsigned int operands: 26;
+};
+
+union openrisc_instruction {
+	unsigned int word;
+	struct insn opcodes_6bit;
+};
+
+#define OPENRISC_INSN_SIZE  (sizeof(union openrisc_instruction))
+
+/* Helpers for working with l.trap */
+static inline unsigned long __emit_trap(unsigned int code)
+{
+	return (code & 0xffff) | OPCODE_TRAP;
+}
+
+static inline bool has_delay_slot(void)
+{
Quoted 8 lines

arch/openrisc/include/asm/insn-def.h line 63

Quoted 4 lines
arch/openrisc/include/asm/insn-def.h +4 -0
+	unsigned int cpucfgr = mfspr(SPR_CPUCFGR);
+
+	return !(cpucfgr & SPR_CPUCFGR_ND);
+}

This is for handling CPU's that do not have delay slots. We didn't do this before, why are you doing it now? Should we mention this in the git commit message?

I am not entirely sure why CPUs with no delay slot were not taken into account initially. The "in_delay_slot" function in traps.c [1] always assumes (for the case where SPR_SR_DSX is not implemented) that the CPU has a delay slot if PC is a branch instruction.

Given that the specification allows CPUs not to have a delay slot, I thought I would include that as well. With no SPR_SR_DSX, if it can be guaranteed that PC will be a branch instruction only when delay slots are enabled, then there should be no change in behaviour in "simulate_branch" even with the addition of the "has_delay_slot" check.

However, if PC can be a branch instruction even when there are no delay slots, then "simulate_branch" now adds 4 to regs->pc instead of adding 8 unconditionally. Please correct me if I am wrong.

"has_delay_slot" is particularly important for KProbes, since a probe at a branch instruction with a delay slot means the branch will have to be emulated followed by single-stepping of the delay slot instruction.

But if there are no delay slots, then only branch emulation is required.

Quoted 2 lines

Also, since this is a static configuration for the CPU should we use static keys for this?

Yes, I think static keys can be used here. If the more likely case is that a delay slot exists, we can make that the more likely branch.

Quoted 63 lines
Quoted 63 lines
arch/openrisc/include/asm/insn-def.h +3 -0
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp);
+void simulate_branch(struct pt_regs *regs, unsigned int jmp, bool has_delay_slot);
 
  #endif /* __ASM_OPENRISC_INSN_DEF_H */
arch/openrisc/include/asm/spr_defs.h +1 -0
index f0b6b492e9f4..5d13a9b96263 100644
@@ -179,6 +179,7 @@
  #define SPR_CPUCFGR_OF32S  0x00000080  /* ORFPX32 supported */
  #define SPR_CPUCFGR_OF64S  0x00000100  /* ORFPX64 supported */
  #define SPR_CPUCFGR_OV64S  0x00000200  /* ORVDX64 supported */
+#define SPR_CPUCFGR_ND     0x00000400  /* No delay slot */
  #define SPR_CPUCFGR_RES	   0xfffffc00  /* Reserved */
 
  /*
arch/openrisc/kernel/Makefile +1 -1
index 19e0eb94f2eb..150779fbf010 100644
@@ -5,7 +5,7 @@
 
  always-$(KBUILD_BUILTIN)	:= vmlinux.lds
 
-obj-y	:= head.o setup.o or32_ksyms.o process.o dma.o \
+obj-y	:= head.o insn.o setup.o or32_ksyms.o process.o dma.o \
  	   traps.o time.o irq.o entry.o ptrace.o signal.o \
  	   sys_call_table.o unwinder.o cacheinfo.o
 
arch/openrisc/kernel/insn.c new file +27 -0
new file mode 100644
index 000000000000..2c97eceee6d7
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * OpenRISC instruction utils
+ *
+ * Linux architectural port borrowing liberally from similar works of
+ * others.  All original copyrights apply as per the original source
+ * declaration.
+ *
+ * OpenRISC implementation:
+ * Copyright (C) 2026 Sahil Siddiq <[email protected]>
+ */
+
+#include <linux/ptrace.h>
+#include <asm/insn-def.h>
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp)
+{
+	int displacement;
+	unsigned int rd, op;
+
+	displacement = sign_extend32(((jmp) & 0x7ffff) << 13, 31);
+	rd = (jmp & 0x3ffffff) >> 21;
+	op = jmp >> 26;
+
+	switch (op) {
+	case l_adrp:
+		regs->gpr[rd] = displacement + (regs->pc & (-8192));
Quoted 8 lines

arch/openrisc/kernel/insn.c line 32

Quoted 5 lines
arch/openrisc/kernel/insn.c new file +5 -0
+		return;
+	default:
+		break;
+	}
+}

This function is not used, what will it be used for? Could you mention it in the commit message? Also could you document it?

"simulate_pc" is used by KProbes for single-stepping l.adrp. At the moment, it's not being used anywhere else. Maybe I should move this function to the second commit which has the KProbe changes.

Also, this function can be removed altogether if we choose to blacklist l.adrp.

Quoted 6 lines

arch/openrisc/kernel/insn.c line 34

Quoted 2 lines
arch/openrisc/kernel/insn.c new file +2 -0
+
+void simulate_branch(struct pt_regs *regs, unsigned int jmp_insn, bool has_delay_slot)

Since you are using this for more than just instruction simulation now, maybe its good to document what it does. For example when we use it below we surround it with the in_delay_slot check.

I am a little confused here. The core of the function was already wrapped in the "in_delay_slot" check [2]. I moved it to a new file so instruction emulation could be used in other places as well(e.g., in KProbes). Functionality wise, the function is still the same with the added feature that it also supports CPUs with no delay slot.

Quoted 10 lines

arch/openrisc/kernel/insn.c line 41

Quoted 7 lines
arch/openrisc/kernel/insn.c new file +7 -0
+{
+	int displacement;
+	unsigned int rb, op, jmp;
+
+	displacement = sign_extend32(((jmp_insn) & 0x3ffffff) << 2, 27);
+	rb = (jmp_insn & 0x0000ffff) >> 11;
+	op = jmp_insn >> 26;

Naming this variable jmp is a bit misleading. Maybe something like link_displacement, link_offset or fallthrough?

Sure, I'll rename this.

Quoted 102 lines
Quoted 102 lines
arch/openrisc/kernel/insn.c new file +33 -0
+	jmp = has_delay_slot ? 2 * OPENRISC_INSN_SIZE : OPENRISC_INSN_SIZE;
+
+	switch (op) {
+	case l_j: /* l.j */
+		regs->pc += displacement;
+		return;
+	case l_jal: /* l.jal */
+		regs->gpr[9] = regs->pc + jmp;
+		regs->pc += displacement;
+		return;
+	case l_bnf: /* l.bnf */
+		if (regs->sr & SPR_SR_F)
+			regs->pc += jmp;
+		else
+			regs->pc += displacement;
+		return;
+	case l_bf: /* l.bf */
+		if (regs->sr & SPR_SR_F)
+			regs->pc += displacement;
+		else
+			regs->pc += jmp;
+		return;
+	case l_jr: /* l.jr */
+		regs->pc = regs->gpr[rb];
+		return;
+	case l_jalr: /* l.jalr */
+		regs->gpr[9] = regs->pc + jmp;
+		regs->pc = regs->gpr[rb];
+		return;
+	default:
+		break;
+	}
+}
arch/openrisc/kernel/jump_label.c +1 -1
index ab7137c23b46..fe082eb847a4 100644
@@ -34,7 +34,7 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
 
  		insn = offset;
  	} else {
-		insn = OPENRISC_INSN_NOP;
+		insn = INSN_NOP;
  	}
 
  	if (early_boot_irqs_disabled)
arch/openrisc/kernel/traps.c +1 -38
index c195be9cc9fc..ee87a3af34fc 100644
@@ -32,6 +32,7 @@
 
  #include <asm/bug.h>
  #include <asm/fpu.h>
+#include <asm/insn-def.h>
  #include <asm/io.h>
  #include <asm/processor.h>
  #include <asm/unwinder.h>
@@ -269,47 +270,9 @@ static inline int in_delay_slot(struct pt_regs *regs)
 
  static inline void adjust_pc(struct pt_regs *regs, unsigned long address)
  {
-	int displacement;
-	unsigned int rb, op, jmp;
-
  	if (unlikely(in_delay_slot(regs))) {
  		/* In delay slot, instruction at pc is a branch, simulate it */
-		jmp = *((unsigned int *)regs->pc);
-
-		displacement = sign_extend32(((jmp) & 0x3ffffff) << 2, 27);
-		rb = (jmp & 0x0000ffff) >> 11;
-		op = jmp >> 26;
-
-		switch (op) {
-		case 0x00: /* l.j */
-			regs->pc += displacement;
-			return;
-		case 0x01: /* l.jal */
-			regs->pc += displacement;
-			regs->gpr[9] = regs->pc + 8;
-			return;
-		case 0x03: /* l.bnf */
-			if (regs->sr & SPR_SR_F)
-				regs->pc += 8;
-			else
-				regs->pc += displacement;
-			return;
-		case 0x04: /* l.bf */
-			if (regs->sr & SPR_SR_F)
-				regs->pc += displacement;
-			else
-				regs->pc += 8;
-			return;
-		case 0x11: /* l.jr */
-			regs->pc = regs->gpr[rb];
-			return;
-		case 0x12: /* l.jalr */
-			regs->pc = regs->gpr[rb];
-			regs->gpr[9] = regs->pc + 8;
-			return;
-		default:
-			break;
Quoted 8 lines
Quoted 8 lines
arch/openrisc/kernel/traps.c +1 -1
-		}
+		simulate_branch(regs, *((unsigned int *)regs->pc), has_delay_slot());
  	} else {
  		regs->pc += 4;
  	}
Signature · 1 line
2.53.0

Thanks,
Sahil

[1] https://github.com/openrisc/linux/blob/for-next/arch/openrisc/kernel/traps.c#L248
[2] https://github.com/openrisc/linux/blob/for-next/arch/openrisc/kernel/traps.c#L275-L312

#4 lore raw

Re: [RFC 1/2] openrisc: Add utilities and clean up simulation of instructions

In reply to Stafford Horne
Quoted 10 lines · On Tue, 14 Apr 2026 18:11:37 +0100 Stafford Horne <[email protected]> wrote:

Hello Sahil,

Thanks for your patches. I have a few questions. so this is not really a technical review at the moment just some main items.

Quoted 4 lines · Sahil Siddiq

Introduce new instruction-related utilities and macros for OpenRISC. This is in preparation for patches that add tracing support such as KProbes.

Quoted 6 lines
Quoted 1 line

Simulate l.adrp. Fix bugs in simulation of l.jal and l.jalr. Earlier,

Why emulate l.adrp? We don't really use this yet in any OpenRISC code. This instruction is meant to be used to help -fpic code, see:

https://openrisc.io/proposals/ladrp

Maybe because l.adrp depends on InsnAddr (instruction address), if kprobe runs copied instruction on its trampoline buffer, it generates a wrong result. If it may not be used in the kernel, arch_prepare_kprobe() can detect it and return an error.

[...]

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+#define OPENRISC_INSN_SIZE  (sizeof(union openrisc_instruction))

This should be defined within an architecture header file.

Quoted 18 lines

arch/openrisc/include/asm/insn-def.h line 63

Quoted 13 lines
arch/openrisc/include/asm/insn-def.h +13 -0
+
+/* Helpers for working with l.trap */
+static inline unsigned long __emit_trap(unsigned int code)
+{
+	return (code & 0xffff) | OPCODE_TRAP;
+}
+
+static inline bool has_delay_slot(void)
+{
+	unsigned int cpucfgr = mfspr(SPR_CPUCFGR);
+
+	return !(cpucfgr & SPR_CPUCFGR_ND);
+}

This is for handling CPU's that do not have delay slots. We didn't do this before, why are you doing it now? Should we mention this in the git commit message?

Quoted 6 lines

Also, since this is a static configuration for the CPU should we use static keys for this?

arch/openrisc/include/asm/insn-def.h line 66

Quoted 3 lines
arch/openrisc/include/asm/insn-def.h +3 -0
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp);
+void simulate_branch(struct pt_regs *regs, unsigned int jmp, bool has_delay_slot);

Also, do not use the same name for a function name and a local variable. Please rename has_delay_slot() to machine_has_delay_slot() or rename the has_delay_slot parameter to 'delay_slot'.

[...]

Quoted 10 lines

arch/openrisc/kernel/insn.c line 22

Quoted 10 lines
arch/openrisc/kernel/insn.c new file +10 -0
+#include <linux/ptrace.h>
+#include <asm/insn-def.h>
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp)
+{
+	int displacement;
+	unsigned int rd, op;
+
+	displacement = sign_extend32(((jmp) & 0x7ffff) << 13, 31);
+	rd = (jmp & 0x3ffffff) >> 21;

Please use GENMASK() macro instead of hex value.
(Moreover, define meaningful named macro instead of the magic number.)

Quoted 4 lines

arch/openrisc/kernel/insn.c line 26

Quoted 4 lines
arch/openrisc/kernel/insn.c new file +4 -0
+	op = jmp >> 26;
+
+	switch (op) {
+	case l_adrp:

l_adrp seems have 2 modes. Is this OK only supporting 32-bit?

32-bit Implementation:
rD[31:0] ← exts(Immediate[18:0] << 13) + (InstAddr & -8192) 64-bit Implementation:
rD[63:0] ← exts(Immediate[20:0] << 13) + (InstAddr & -8192)

Quoted 1 line

arch/openrisc/kernel/insn.c line 27

Quoted 1 line
arch/openrisc/kernel/insn.c new file +1 -0
+		regs->gpr[rd] = displacement + (regs->pc & (-8192));

Please use nicely named macro instead of -8192.

Thanks,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#19 lore raw
In reply to Sahil Siddiq

Hi Sahil,

Quoted 7 lines · On Wed, 8 Apr 2026 00:26:48 +0530 Sahil Siddiq <[email protected]> wrote:

Hi,

This series adds basic support for KProbes on OpenRISC. There are a few changes that I would still like to add and test before this can be considered for merging. I was hoping to get some feedback on the changes made so far. The implementation in this series is based on KProbes for LoongArch, MIPS and RISC-V.

Thanks for porting!
Sashiko reviewed this series, can you check the comments? Most comments (not all) look reasonable to me.

https://sashiko.dev/#/patchset/20260407185650.79816-1-sahilcdq0%40gmail.com

Generally, please make better use of macros rather than magic values in your code to make it easier to understand.
Also, use GENMASK() and BIT() macro to define bitmasks and bit.

Thanks,

Quoted 100 lines

The current state of the series allows traps to be inserted dynamically in the kernel. A KProbe can be inserted via a kernel module whose init/exit functions are used to register/unregister a KProbe. A pre- handler and post-handler can also be provisioned in the module, which are associated with the KProbe and triggered when the probe is hit. See the documentation on KProbes for a detailed explanation [1].

The following are yet to be implemented for OpenRISC:
1. kretprobes
2. kprobe-based event tracing
3. ftrace, and kprobe features that depend on ftrace (particularly, dynamic tracing)

I hope to submit a patch for kretprobes soon (possibly in a revision of this series).

I wrote a couple of kernel modules to test these changes. They can be found here [2]. I also ran test_kprobes located at ./lib/tests/ against these changes [3]. The results are as shown below:

/home # insmod test_kprobes.ko
KTAP version 1
1..1
KTAP version 1
# Subtest: kprobes_test
# module: test_kprobes
1..3
ok 1 test_kprobe
ok 2 test_kprobes
ok 3 test_kprobe_missed
# kprobes_test: pass:3 fail:0 skip:0 total:3
# Totals: pass:3 fail:0 skip:0 total:3
ok 1 kprobes_test
/home #

When compiling the kernel, the following options should be enabled:
1. CONFIG_HAVE_KPROBES=y
2. CONFIG_KPROBES=y

Also ensure that CONFIG_KPROBE_EVENTS is disabled.

To compile /lib/tests/test_kprobes.c, add the following to .config:
1. CONFIG_KUNIT=y
2. CONFIG_DEBUG_KERNEL=y
3. CONFIG_KPROBES_SANITY_TEST=m

The first commit cleans up and reorganizes existing functions, fixes a few issues with instruction simulation, and introduces new structures and macros that will be used by KProbes and other tracing facilities in the future.

The second commit adds support for KProbes. Currently, I have implemented this in such a way that KProbes can't be used to probe a few "blacklisted" instructions. Probes can't be inserted in a delay slot either (similar to MIPS). I have also added a few asm functions to the blacklist that I think should not be probed. For e.g., "memset" and "_trap_handler" have been blacklisted because probing them causes the kernel to hang. However, I am not sure if other functions in "entry.S" need to be added as well to the blacklist.

Thanks,
Sahil

[1] https://www.kernel.org/doc/html/latest/trace/kprobes.html
[2] https://github.com/valdaarhun/or-dev/tree/main/home
[3] https://github.com/openrisc/linux/blob/for-next/lib/tests/test_kprobes.c

Sahil Siddiq (2):
openrisc: Add utilities and clean up simulation of instructions
openrisc: Add KProbes

 arch/openrisc/Kconfig                   |   1 +
 arch/openrisc/configs/or1ksim_defconfig |   2 +
 arch/openrisc/configs/virt_defconfig    |   2 +
 arch/openrisc/include/asm/asm.h         |  22 ++
 arch/openrisc/include/asm/break.h       |  19 ++
 arch/openrisc/include/asm/insn-def.h    |  61 +++-
 arch/openrisc/include/asm/kprobes.h     |  76 +++++
 arch/openrisc/include/asm/spr_defs.h    |   1 +
 arch/openrisc/kernel/Makefile           |   3 +-
 arch/openrisc/kernel/entry.S            |  16 +
 arch/openrisc/kernel/insn.c             |  74 +++++
 arch/openrisc/kernel/jump_label.c       |   2 +-
 arch/openrisc/kernel/kprobes.c          | 381 ++++++++++++++++++++++++
 arch/openrisc/kernel/traps.c            |  67 ++---
 arch/openrisc/lib/memcpy.c              |   2 +
 arch/openrisc/lib/memset.S              |   4 +
 arch/openrisc/mm/fault.c                |   5 +
 samples/kprobes/kprobe_example.c        |   8 +
 18 files changed, 701 insertions(+), 45 deletions(-)
 create mode 100644 arch/openrisc/include/asm/asm.h
 create mode 100644 arch/openrisc/include/asm/break.h
 create mode 100644 arch/openrisc/include/asm/kprobes.h
 create mode 100644 arch/openrisc/kernel/insn.c
 create mode 100644 arch/openrisc/kernel/kprobes.c
Signature · 1 line
2.53.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author
#5 lore raw

Re: [RFC 1/2] openrisc: Add utilities and clean up simulation of instructions

In reply to Masami Hiramatsu (Google)

Hi Masami,

Thank you for your review.

Quoted 25 lines · Masami Hiramatsu (Google)
Quoted 10 lines · On Tue, 14 Apr 2026 18:11:37 +0100 Stafford Horne <[email protected]> wrote:

Hello Sahil,

Thanks for your patches. I have a few questions. so this is not really a technical review at the moment just some main items.

Quoted 4 lines · Sahil Siddiq

Introduce new instruction-related utilities and macros for OpenRISC. This is in preparation for patches that add tracing support such as KProbes.

Quoted 6 lines
Quoted 1 line

Simulate l.adrp. Fix bugs in simulation of l.jal and l.jalr. Earlier,

Why emulate l.adrp? We don't really use this yet in any OpenRISC code. This instruction is meant to be used to help -fpic code, see:

https://openrisc.io/proposals/ladrp

Maybe because l.adrp depends on InsnAddr (instruction address), if kprobe runs copied instruction on its trampoline buffer, it generates a wrong result. If it may not be used in the kernel, arch_prepare_kprobe() can detect it and return an error.

Quoted 5 lines

[...]

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+#define OPENRISC_INSN_SIZE  (sizeof(union openrisc_instruction))

This should be defined within an architecture header file.

Sorry, I am a little confused here. OPENRISC_INSN_SIZE is in OpenRISC's instruction definition file (arch/openrisc/include/asm/insn-def.h).

The instruction size for Arm has also been set in it's own insn-def.h file [1].

Quoted 29 lines
Quoted 18 lines

arch/openrisc/include/asm/insn-def.h line 63

Quoted 13 lines
arch/openrisc/include/asm/insn-def.h +13 -0
+
+/* Helpers for working with l.trap */
+static inline unsigned long __emit_trap(unsigned int code)
+{
+	return (code & 0xffff) | OPCODE_TRAP;
+}
+
+static inline bool has_delay_slot(void)
+{
+	unsigned int cpucfgr = mfspr(SPR_CPUCFGR);
+
+	return !(cpucfgr & SPR_CPUCFGR_ND);
+}

This is for handling CPU's that do not have delay slots. We didn't do this before, why are you doing it now? Should we mention this in the git commit message?

Quoted 6 lines

Also, since this is a static configuration for the CPU should we use static keys for this?

arch/openrisc/include/asm/insn-def.h line 66

Quoted 3 lines
arch/openrisc/include/asm/insn-def.h +3 -0
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp);
+void simulate_branch(struct pt_regs *regs, unsigned int jmp, bool has_delay_slot);

Also, do not use the same name for a function name and a local variable. Please rename has_delay_slot() to machine_has_delay_slot() or rename the has_delay_slot parameter to 'delay_slot'.

Quoted 1 line

[...]

Oh, good catch. I missed this. I'll change this and retest.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
arch/openrisc/kernel/insn.c new file +5 -0
+#include <linux/ptrace.h>
+#include <asm/insn-def.h>
+
+void simulate_pc(struct pt_regs *regs, unsigned int jmp)
+{
Quoted 8 lines
Quoted 5 lines

arch/openrisc/kernel/insn.c line 22

Quoted 5 lines
arch/openrisc/kernel/insn.c new file +5 -0
+	int displacement;
+	unsigned int rd, op;
+
+	displacement = sign_extend32(((jmp) & 0x7ffff) << 13, 31);
+	rd = (jmp & 0x3ffffff) >> 21;

Please use GENMASK() macro instead of hex value.
(Moreover, define meaningful named macro instead of the magic number.)

Understood, I'll fix this.

Quoted 11 lines
Quoted 4 lines

arch/openrisc/kernel/insn.c line 26

Quoted 4 lines
arch/openrisc/kernel/insn.c new file +4 -0
+	op = jmp >> 26;
+
+	switch (op) {
+	case l_adrp:

l_adrp seems have 2 modes. Is this OK only supporting 32-bit?

32-bit Implementation:
rD[31:0] ← exts(Immediate[18:0] << 13) + (InstAddr & -8192) 64-bit Implementation:
rD[63:0] ← exts(Immediate[20:0] << 13) + (InstAddr & -8192)

I think supporting the 32-bit version should be good enough for the time being, since OpenRISC's implemention in the kernel is 32-bit. Please see the following comment from the patch series to support jump labels (published last year) [2]:

Quoted 3 lines

for us it's more correct to use u32. I always thing unsigned long is OK in openrisc as we have no 64-bit implementations. But might as well have it correct in case we ever add the 64-bit implementation.

I haven't found any OpenRISC patches in the past year that add the 64-bit implementation.

Quoted 3 lines
Quoted 1 line

arch/openrisc/kernel/insn.c line 27

Quoted 1 line
arch/openrisc/kernel/insn.c new file +1 -0
+		regs->gpr[rd] = displacement + (regs->pc & (-8192));

Please use nicely named macro instead of -8192.

Got it. I'll make this change too.

Quoted 1 line

Thanks,

Thanks,
Sahil

[1] https://github.com/torvalds/linux/blob/master/arch/arm64/include/asm/insn-def.h#L9
[2] https://lore.kernel.org/openrisc/[email protected]/T/#mb4a939437351b705c30ec7f9e9195d519f9b586f

author
#20 lore raw
In reply to Masami Hiramatsu (Google)

Hi Masami,

Quoted 13 lines · Masami Hiramatsu (Google)

Hi Sahil,

Quoted 7 lines · On Wed, 8 Apr 2026 00:26:48 +0530 Sahil Siddiq <[email protected]> wrote:

Hi,

This series adds basic support for KProbes on OpenRISC. There are a few changes that I would still like to add and test before this can be considered for merging. I was hoping to get some feedback on the changes made so far. The implementation in this series is based on KProbes for LoongArch, MIPS and RISC-V.

Quoted 5 lines

Thanks for porting!
Sashiko reviewed this series, can you check the comments? Most comments (not all) look reasonable to me.

https://sashiko.dev/#/patchset/20260407185650.79816-1-sahilcdq0%40gmail.com

Thank you for the link. I'll address the review comments.

Quoted 3 lines

Generally, please make better use of macros rather than magic values in your code to make it easier to understand.
Also, use GENMASK() and BIT() macro to define bitmasks and bit.

Understood. I'll keep that in mind in future patches.

Quoted 2 lines

Thanks,

Thanks,
Sahil

#6 lore raw

v2 cover openrisc: Add support for KProbes

In reply to Sahil Siddiq

Hi,

Apologies for the delay. This is v2 of the patch series to add KProbe support for OpenRISC.

The series also adds support for Kretprobes (which depends on the rethook framework [1]). Kretprobes are fired on function return.

In order to build the kernel with Kprobes and Kretprobes enabled, the following config options are required:
1. CONFIG_HAVE_KPROBES=y
2. CONFIG_KPROBES=y
3. CONFIG_RETHOOK=y
4. CONFIG_KRETPROBES=y

I tested the generic functionality of Kprobes and Kretprobes using the tests located at "lib/tests" [2], samples located at "samples/kprobes" [3], as well as a few test modules that I have published here [4]. The following options are required to build the samples and tests:
1. CONFIG_SAMPLES=y
2. CONFIG_SAMPLE_KPROBES=m
3. CONFIG_SAMPLE_KRETPROBES=m
4. CONFIG_KPROBES_SANITY_TEST=m

I have also added unit tests at "arch/openrisc/kernel/tests" to verify that Kprobes work properly when inserted at branch instructions, and that delay slots are handled correctly. The tests are based on the Kprobe testsuite added for RISC-V. The following options need to be enabled to build the tests:
1. CONFIG_KUNIT=y
2. CONFIG_RUNTIME_KERNEL_TESTING_MENU=y
3. CONFIG_OR_KPROBES_KUNIT=m

Below are the results of the tests and sample modules when run in my environment.

- kretprobe_example:

/home # insmod kretprobe_example.ko
Planted return probe at kernel_clone: (ptrval) /home # ls >/dev/null
kernel_clone returned 35 and took 1302450 ns to execute /home # rmmod kretprobe_example.ko kernel_clone returned 36 and took 1121200 ns to execute kretprobe at (ptrval) unregistered
Missed probing 0 instances of kernel_clone

- test_kprobes:

/home # insmod test_kprobes.ko
KTAP version 1
1..1
KTAP version 1
# Subtest: kprobes_test
# module: test_kprobes
1..5
ok 1 test_kprobe
ok 2 test_kprobes
ok 3 test_kprobe_missed
ok 4 test_kretprobe
ok 5 test_kretprobes
# kprobes_test: pass:5 fail:0 skip:0 total:5
# Totals: pass:5 fail:0 skip:0 total:5
ok 1 kprobes_test

- openrisc_test_kprobes:

/home # insmod openrisc_test_kprobes.ko
KTAP version 1
1..1
KTAP version 1
# Subtest: kprobes_openrisc
# module: openrisc_test_kprobes
1..1
ok 1 test_kprobe_openrisc
ok 1 kprobes_openrisc

I have also made changes based on the review comments I received for v1 [5] including several of Sachiko's comments [6]. I chose not to make changes in a few places. I'll send emails as a reply to this thread explaining my reasoning inline.

Thanks,
Sahil

[1] https://lkml.rescloud.iu.edu/2203.0/05974.html
[2] https://github.com/torvalds/linux/tree/master/lib/tests
[3] https://github.com/torvalds/linux/tree/master/samples/kprobes
[4] https://github.com/valdaarhun/or-dev
[5] https://lwn.net/ml/all/[email protected]/
[6] https://sashiko.dev/#/patchset/20260407185650.79816-1-sahilcdq0%40gmail.com

Sahil Siddiq (4):
openrisc: Add utilities and clean up simulation of instructions
openrisc: Add KProbes
openrisc: Add unit tests for KProbes on branch instructions
openrisc: Add Kretprobes

 arch/openrisc/Kconfig                         |   3 +
 arch/openrisc/Kconfig.debug                   |  30 +-
 arch/openrisc/configs/or1ksim_defconfig       |   2 +
 arch/openrisc/configs/virt_defconfig          |   2 +
 arch/openrisc/include/asm/asm.h               |  22 +
 arch/openrisc/include/asm/break.h             |  19 +
 arch/openrisc/include/asm/cpuinfo.h           |   3 +
 arch/openrisc/include/asm/insn-def.h          |  64 ++-
 arch/openrisc/include/asm/kprobes.h           |  77 ++++
 arch/openrisc/include/asm/spr_defs.h          |   1 +
 arch/openrisc/kernel/Makefile                 |   5 +-
 arch/openrisc/kernel/entry.S                  |  16 +
 arch/openrisc/kernel/insn.c                   |  84 ++++
 arch/openrisc/kernel/kprobes.c                | 400 ++++++++++++++++++
 arch/openrisc/kernel/rethook.c                |  32 ++
 arch/openrisc/kernel/rethook.h                |   8 +
 arch/openrisc/kernel/rethook_trampoline.S     |  97 +++++
 arch/openrisc/kernel/setup.c                  |   4 +
 arch/openrisc/kernel/tests/Makefile           |   3 +
 arch/openrisc/kernel/tests/test_kprobes.c     |  59 +++
 arch/openrisc/kernel/tests/test_kprobes.h     |  24 ++
 arch/openrisc/kernel/tests/test_kprobes_asm.S | 147 +++++++
 arch/openrisc/kernel/traps.c                  |  82 ++--
 arch/openrisc/lib/memcpy.c                    |   2 +
 arch/openrisc/lib/memset.S                    |   4 +
 arch/openrisc/mm/fault.c                      |   5 +
 samples/kprobes/kprobe_example.c              |   8 +
 27 files changed, 1152 insertions(+), 51 deletions(-)
 create mode 100644 arch/openrisc/include/asm/asm.h
 create mode 100644 arch/openrisc/include/asm/break.h
 create mode 100644 arch/openrisc/include/asm/kprobes.h
 create mode 100644 arch/openrisc/kernel/insn.c
 create mode 100644 arch/openrisc/kernel/kprobes.c
 create mode 100644 arch/openrisc/kernel/rethook.c
 create mode 100644 arch/openrisc/kernel/rethook.h
 create mode 100644 arch/openrisc/kernel/rethook_trampoline.S
 create mode 100644 arch/openrisc/kernel/tests/Makefile
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes.c
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes.h
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes_asm.S
Signature · 1 line
2.55.0
#10 lore raw

PATCH v2 1/4 openrisc: Add utilities and clean up simulation of instructions

In reply to Sahil Siddiq

Introduce new instruction-related utilities and macros for OpenRISC. This is in preparation for patches that add tracing support such as KProbes and Kretprobes.

Currently, PC is first updated and then saved in the link register (r9) when simulating l.jal and l.jalr. This can result in a corrupted page table (bad page map in process). Fix this by saving PC in r9 before updating it.

Move branch simulation to its own function, simulate_branch(), so it can be reused. It's already being used by do_illegal_instruction(). It'll also be used by KProbes in the following commit.

Introduce simulate_pc() to simulate l.adrp. KProbes will require this instruction to be simulated as well. simulate_branch() and simulate_pc() need to be handled differently since branch instructions change the value of PC while l.adrp depends on PC but doesn't change its value.

Use a static key to check if delay slots are disabled. Delay slots are a part of the CPU's configuration and won't change at runtime. Since they are often enabled in an OpenRISC CPU, using a static key will reduce the overhead of checking this flag in the default case while processing KProbes inserted at branch instructions and simulating them.

---
 arch/openrisc/include/asm/cpuinfo.h  |  3 +
 arch/openrisc/include/asm/insn-def.h | 64 +++++++++++++++++++--
 arch/openrisc/include/asm/spr_defs.h |  1 +
 arch/openrisc/kernel/Makefile        |  2 +-
 arch/openrisc/kernel/insn.c          | 84 ++++++++++++++++++++++++++++
 arch/openrisc/kernel/setup.c         |  4 ++
 arch/openrisc/kernel/traps.c         | 79 +++++++++++---------------
 7 files changed, 187 insertions(+), 50 deletions(-)
 create mode 100644 arch/openrisc/kernel/insn.c
arch/openrisc/include/asm/cpuinfo.h +3 -0
index 3cfc4cf0b019..d6bb8744eba0 100644
@@ -15,6 +15,7 @@
 #ifndef __ASM_OPENRISC_CPUINFO_H
 #define __ASM_OPENRISC_CPUINFO_H
 
+#include <linux/jump_label.h>
 #include <asm/spr.h>
 #include <asm/spr_defs.h>
 
@@ -34,6 +35,8 @@ struct cpuinfo_or1k {
 	u16 coreid;
 };
 
+DECLARE_STATIC_KEY_TRUE(delay_slot);
+
 extern struct cpuinfo_or1k cpuinfo_or1k[NR_CPUS];
 extern void setup_cpuinfo(void);
 
arch/openrisc/include/asm/insn-def.h +60 -4

73 lines not on this page · raw message

arch/openrisc/include/asm/spr_defs.h +1 -0
index f0b6b492e9f4..5d13a9b96263 100644
@@ -179,6 +179,7 @@
 #define SPR_CPUCFGR_OF32S  0x00000080  /* ORFPX32 supported */
 #define SPR_CPUCFGR_OF64S  0x00000100  /* ORFPX64 supported */
 #define SPR_CPUCFGR_OV64S  0x00000200  /* ORVDX64 supported */
+#define SPR_CPUCFGR_ND     0x00000400  /* No delay slot */
 #define SPR_CPUCFGR_RES	   0xfffffc00  /* Reserved */
 
 /*
arch/openrisc/kernel/Makefile +1 -1
index 19e0eb94f2eb..150779fbf010 100644
@@ -5,7 +5,7 @@
 
 always-$(KBUILD_BUILTIN)	:= vmlinux.lds
 
-obj-y	:= head.o setup.o or32_ksyms.o process.o dma.o \
+obj-y	:= head.o insn.o setup.o or32_ksyms.o process.o dma.o \
 	   traps.o time.o irq.o entry.o ptrace.o signal.o \
 	   sys_call_table.o unwinder.o cacheinfo.o
 
arch/openrisc/kernel/insn.c new file +84 -0

84 lines not on this page · raw message

arch/openrisc/kernel/setup.c +4 -0
index 000a9cc10e6f..0f69c36472d5 100644
@@ -255,6 +255,10 @@ void __init setup_arch(char **cmdline_p)
 	/* paging_init() sets up the MMU and marks all pages as reserved */
 	paging_init();
 
+	/* determine if the processor supports delay slots */
+	if (mfspr(SPR_CPUCFGR) & SPR_CPUCFGR_ND)
+		static_branch_disable(&delay_slot);
+
 	*cmdline_p = boot_command_line;
 
 	printk(KERN_INFO "OpenRISC Linux -- http://openrisc.io\n");
arch/openrisc/kernel/traps.c +34 -45

105 lines not on this page · raw message

Signature · 1 line
2.55.0
#12 lore raw

PATCH v2 2/4 openrisc: Add KProbes

In reply to Sahil Siddiq

Add KProbes support for OpenRISC. This work is primarily based on similar work done for LoongArch, MIPS and RISC-V.

KProbes make it possible to trap at almost any address in the kernel to collect performance/debugging info.

---
 arch/openrisc/Kconfig                   |   1 +
 arch/openrisc/configs/or1ksim_defconfig |   2 +
 arch/openrisc/configs/virt_defconfig    |   2 +
 arch/openrisc/include/asm/asm.h         |  22 ++
 arch/openrisc/include/asm/break.h       |  19 ++
 arch/openrisc/include/asm/kprobes.h     |  77 +++++
 arch/openrisc/kernel/Makefile           |   1 +
 arch/openrisc/kernel/entry.S            |  16 +
 arch/openrisc/kernel/kprobes.c          | 400 ++++++++++++++++++++++++
 arch/openrisc/kernel/traps.c            |   3 +
 arch/openrisc/lib/memcpy.c              |   2 +
 arch/openrisc/lib/memset.S              |   4 +
 arch/openrisc/mm/fault.c                |   5 +
 samples/kprobes/kprobe_example.c        |   8 +
 14 files changed, 562 insertions(+)
 create mode 100644 arch/openrisc/include/asm/asm.h
 create mode 100644 arch/openrisc/include/asm/break.h
 create mode 100644 arch/openrisc/include/asm/kprobes.h
 create mode 100644 arch/openrisc/kernel/kprobes.c
arch/openrisc/Kconfig +1 -0
index 9156635dd264..d240533b424b 100644
@@ -27,6 +27,7 @@ config OPENRISC
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_PCI
+	select HAVE_KPROBES
 	select HAVE_UID16
 	select HAVE_PAGE_SIZE_8KB
 	select HAVE_REGS_AND_STACK_ACCESS_API
arch/openrisc/configs/or1ksim_defconfig +2 -0
index 769705ac24d5..24d2915e7609 100644
@@ -10,7 +10,9 @@ CONFIG_EXPERT=y
 # CONFIG_KALLSYMS is not set
 CONFIG_BUILTIN_DTB_NAME="or1ksim"
 CONFIG_HZ_100=y
+CONFIG_OPENRISC=y
 CONFIG_JUMP_LABEL=y
+CONFIG_KPROBES=y
 CONFIG_MODULES=y
 # CONFIG_BLOCK is not set
 CONFIG_SLUB_TINY=y
arch/openrisc/configs/virt_defconfig +2 -0
index 0b9979b35ca8..2eccb506032f 100644
@@ -11,8 +11,10 @@ CONFIG_OPENRISC_HAVE_INST_SEXT=y
 CONFIG_NR_CPUS=8
 CONFIG_SMP=y
 CONFIG_HZ_100=y
+CONFIG_OPENRISC=y
 # CONFIG_OPENRISC_NO_SPR_SR_DSX is not set
 CONFIG_JUMP_LABEL=y
+CONFIG_KPROBES=y
 # CONFIG_COMPAT_BRK is not set
 CONFIG_NET=y
 CONFIG_PACKET=y
arch/openrisc/include/asm/asm.h new file +22 -0
new file mode 100644
index 000000000000..aafe08029b4b
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Macros for OpenRISC asm
+ *
+ * Linux architectural port borrowing nearly verbatim from
+ * LoongArch and Arm. All original copyrights apply as per
+ * the original source declaration.
+ */
+
+#ifndef __ASM_ASM_H
+#define __ASM_ASM_H
+
+#ifdef CONFIG_KPROBES
+#define _ASM_NOKPROBE(symbol)			\
+	.pushsection "_kprobe_blacklist", "aw";	\
+	.long symbol;				\
+	.popsection
+#else
+#define _ASM_NOKPROBE(symbol)
+#endif
+
+#endif /* __ASM_ASM_H */
arch/openrisc/include/asm/break.h new file +19 -0
new file mode 100644
index 000000000000..04a74d863eef
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * OpenRISC trap codes used internally by the kernel
+ *
+ * Linux architectural port borrowing liberally from similar works of
+ * others.  All original copyrights apply as per the original source
+ * declaration.
+ *
+ * Modifications for the OpenRISC architecture:
+ * Copyright (C) 2026 Sahil Siddiq <[email protected]>
+ */
+
+#ifndef __ASM_BREAK_H
+#define __ASM_BREAK_H
+
+#define BRK_KPROBE_BP       512     /* Kprobe break */
+#define BRK_KPROBE_SSTEPBP  1024    /* Kprobe single-step software implementation */
+
+#endif /* __ASM_BREAK_H */
arch/openrisc/include/asm/kprobes.h new file +77 -0

77 lines not on this page · raw message

arch/openrisc/kernel/Makefile +1 -0
index 150779fbf010..2ac824867963 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_SMP)		+= smp.o sync-timer.o
 obj-$(CONFIG_STACKTRACE)	+= stacktrace.o
 obj-$(CONFIG_MODULES)		+= module.o
 obj-$(CONFIG_OF)		+= prom.o
+obj-$(CONFIG_KPROBES)		+= kprobes.o
 obj-y	+= patching.o
 
 clean:
arch/openrisc/kernel/entry.S +16 -0
index c7e90b09645e..cd28bf1f7a3b 100644
@@ -15,6 +15,7 @@
 #include <linux/linkage.h>
 #include <linux/pgtable.h>
 
+#include <asm/asm.h>
 #include <asm/processor.h>
 #include <asm/unistd.h>
 #include <asm/thread_info.h>
@@ -640,6 +641,7 @@ ENTRY(_sys_call_handler)
 	/* r30 is the only register we clobber in the fast path */
 	/* r30 already saved */
 /*	l.sw    PT_GPR30(r1),r30 */
+_ASM_NOKPROBE(_sys_call_handler)
 
 _syscall_check_trace_enter:
 	/* syscalls run with interrupts enabled */
@@ -652,12 +654,14 @@ _syscall_check_trace_enter:
 	l.sfne	r30,r0
 	l.bf	_syscall_trace_enter
 	 l.nop
+_ASM_NOKPROBE(_syscall_check_trace_enter)
 
 _syscall_check:
 	/* Ensure that the syscall number is reasonable */
 	l.sfgeui r11,__NR_syscalls
 	l.bf	_syscall_badsys
 	 l.nop
+_ASM_NOKPROBE(_syscall_check)
 
 _syscall_call:
 	l.movhi r29,hi(sys_call_table)
@@ -668,12 +672,14 @@ _syscall_call:
 
 	l.jalr  r29
 	 l.nop
+_ASM_NOKPROBE(_syscall_call)
 
 _syscall_return:
 	/* All syscalls return here... just pay attention to ret_from_fork
 	 * which does it in a round-about way.
 	 */
 	l.sw    PT_GPR11(r1),r11           // save return value
+_ASM_NOKPROBE(_syscall_return)
 
 #if 0
 _syscall_debug:
@@ -708,6 +714,7 @@ _syscall_check_trace_leave:
 	l.sfne	r30,r0
 	l.bf	_syscall_trace_leave
 	 l.nop
+_ASM_NOKPROBE(_syscall_check_trace_leave)
 
 /* This is where the exception-return code begins... interrupts need to be
  * disabled the rest of the way here because we can't afford to miss any
@@ -744,6 +751,7 @@ _syscall_check_work:
 	/* _work_pending needs to be called with interrupts disabled */
 	l.j	_work_pending
 	 l.nop
+_ASM_NOKPROBE(_syscall_check_work)
 
 _syscall_resume_userspace:
 //	ENABLE_INTERRUPTS(r29)
@@ -800,6 +808,7 @@ _syscall_resume_userspace:
 	l.mtspr r0,r13,SPR_EPCR_BASE
 	l.mtspr r0,r15,SPR_ESR_BASE
 	l.rfe
+_ASM_NOKPROBE(_syscall_resume_userspace)
 
 /* End of hot path!
  * Keep the below tracing and error handling out of the hot path...
@@ -829,6 +838,7 @@ _syscall_trace_enter:
 
 	l.j	_syscall_check
 	 l.lwz	r8,PT_GPR8(r1)
+_ASM_NOKPROBE(_syscall_trace_enter)
 
 _syscall_trace_leave:
 	l.jal	do_syscall_trace_leave
@@ -836,6 +846,7 @@ _syscall_trace_leave:
 
 	l.j	_syscall_check_work
 	 l.nop
+_ASM_NOKPROBE(_syscall_trace_leave)
 
 _syscall_badsys:
 	/* Here we effectively pretend to have executed an imaginary
@@ -845,6 +856,7 @@ _syscall_badsys:
 	 */
 	l.j	_syscall_return
 	 l.addi	r11,r0,-ENOSYS
+_ASM_NOKPROBE(_syscall_badsys)
 
 /******* END SYSCALL HANDLING *******/
 
@@ -870,6 +882,7 @@ EXCEPTION_ENTRY(_trap_handler)
 
 	l.j     _ret_from_exception
 	 l.nop
+_ASM_NOKPROBE(_trap_handler)
 
 /* ---[ 0xf00: Reserved exception ]-------------------------------------- */
 
@@ -1004,6 +1017,8 @@ ENTRY(_ret_from_exception)
 	 l.nop
 	l.j	_resume_userspace
 	 l.nop
+_ASM_NOKPROBE(_ret_from_exception)
+_ASM_NOKPROBE(_ret_from_intr)
 
 ENTRY(ret_from_fork)
 	l.jal	schedule_tail
@@ -1038,6 +1053,7 @@ ENTRY(ret_from_fork)
 
 	l.j	_syscall_return
 	 l.nop
+_ASM_NOKPROBE(ret_from_fork)
 
 /* ========================================================[ switch ] === */
 
arch/openrisc/kernel/kprobes.c new file +400 -0

400 lines not on this page · raw message

arch/openrisc/kernel/traps.c +3 -0
index c9ab630ec895..2d6b29ea8c4f 100644
@@ -30,10 +30,12 @@
 #include <linux/kallsyms.h>
 #include <linux/uaccess.h>
 
+#include <asm/break.h>
 #include <asm/bug.h>
 #include <asm/fpu.h>
 #include <asm/insn-def.h>
 #include <asm/io.h>
+#include <asm/kprobes.h>
 #include <asm/processor.h>
 #include <asm/unwinder.h>
 #include <asm/sections.h>
@@ -246,6 +248,7 @@ asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
 		die("Die:", regs, SIGILL);
 	}
 }
+NOKPROBE_SYMBOL(do_trap)
 
 asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address)
 {
arch/openrisc/lib/memcpy.c +2 -0
index e2af9b510804..701262a40134 100644
@@ -16,6 +16,7 @@
 
 #include <linux/export.h>
 
+#include <linux/kprobes.h>
 #include <linux/string.h>
 
 #ifdef CONFIG_OR1K_1200
@@ -122,4 +123,5 @@ void *memcpy(void *dest, __const void *src, __kernel_size_t n)
 }
 #endif
 
+NOKPROBE_SYMBOL(memcpy);
 EXPORT_SYMBOL(memcpy);
arch/openrisc/lib/memset.S +4 -0
index c3ac2a8b68d3..14e63af12d73 100644
@@ -9,6 +9,8 @@
  * Copyright (C) 2015 Olof Kindgren <[email protected]>
  */
 
+#include <asm/asm.h>
+
 	.global memset
 	.type	memset, @function
 memset:
@@ -92,3 +94,5 @@ memset:
 
 4:	l.jr		r9
 	 l.ori		r11, r3, 0
+
+_ASM_NOKPROBE(memset)
arch/openrisc/mm/fault.c +5 -0
index 29e232d78d82..5263a832562f 100644
@@ -14,6 +14,7 @@
 #include <linux/mm.h>
 #include <linux/interrupt.h>
 #include <linux/extable.h>
+#include <linux/kprobes.h>
 #include <linux/sched/signal.h>
 #include <linux/perf_event.h>
 
@@ -55,6 +56,9 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
 
 	tsk = current;
 
+	if (kprobe_page_fault(regs, vector))
+		return;
+
 	/*
 	 * We fault-in kernel-space virtual memory on-demand. The
 	 * 'reference' page table is init_mm.pgd.
@@ -351,3 +355,4 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address,
 		return;
 	}
 }
+NOKPROBE_SYMBOL(do_page_fault)
samples/kprobes/kprobe_example.c +8 -0
index 53ec6c8b8c40..84e26ebef70b 100644
@@ -59,6 +59,10 @@ static int __kprobes handler_pre(struct kprobe *p, struct pt_regs *regs)
 	pr_info("<%s> p->addr = 0x%p, era = 0x%lx, estat = 0x%lx\n",
 		p->symbol_name, p->addr, regs->csr_era, regs->csr_estat);
 #endif
+#ifdef CONFIG_OPENRISC
+	pr_info("<%s> p->addr = 0x%p, pc = 0x%lx, status = 0x%lx\n",
+		p->symbol_name, p->addr, regs->pc, regs->sr);
+#endif
 
 	/* A dump_stack() here will give a stack backtrace */
 	return 0;
@@ -100,6 +104,10 @@ static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,
 	pr_info("<%s> p->addr = 0x%p, estat = 0x%lx\n",
 		p->symbol_name, p->addr, regs->csr_estat);
 #endif
+#ifdef CONFIG_OPENRISC
+	pr_info("<%s> p->addr = 0x%p, status = 0x%lx\n",
+		p->symbol_name, p->addr, regs->sr);
+#endif
 }
 
 static int __init kprobe_init(void)
Signature · 1 line
2.55.0
#14 lore raw

PATCH v2 3/4 openrisc: Add unit tests for KProbes on branch instructions

In reply to Sahil Siddiq

Tests already exist lib/tests to verify the generic functionality of KProbes. Add unit tests to verify that KProbes work on instructions that require simulation in the OpenRISC ISA, and that the corresponding delay slots are handled correctly.

---
 arch/openrisc/Kconfig.debug                   |  30 +++-
 arch/openrisc/kernel/Makefile                 |   1 +
 arch/openrisc/kernel/tests/Makefile           |   3 +
 arch/openrisc/kernel/tests/test_kprobes.c     |  59 +++++++
 arch/openrisc/kernel/tests/test_kprobes.h     |  24 +++
 arch/openrisc/kernel/tests/test_kprobes_asm.S | 147 ++++++++++++++++++
 6 files changed, 263 insertions(+), 1 deletion(-)
 create mode 100644 arch/openrisc/kernel/tests/Makefile
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes.c
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes.h
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes_asm.S
arch/openrisc/Kconfig.debug +29 -1
index 295942fe3fd5..54110f3a111f 100644
@@ -1,2 +1,30 @@
 # SPDX-License-Identifier: GPL-2.0-only
-# dummy file, do not delete
+menu "openrisc Testing"
+
+menuconfig RUNTIME_KERNEL_TESTING_MENU
+    bool "openrisc kernel runtime testing"
+    default y
+    help
+      Enable openrisc kernel runtime testing. With this option, one can
+      build and run tests to verify the correctness of relevant components
+      in the kernel at runtime.
+
+if RUNTIME_KERNEL_TESTING_MENU
+
+config OR_KPROBES_KUNIT
+    tristate "KUnit test for kprobes on openrisc"
+    depends on KUNIT
+    depends on KPROBES
+    default KUNIT_ALL_TESTS
+    help
+      Enable testing of KProbes in OpenRISC for instructions that
+      require simulation. When further changes related to probes or
+      tracing are added to OpenRISC's port, these tests can help
+      verify that instruction simulation and processing of delay slots
+      continue to work as expected.
+
+      If unsure, say N
+
+endif # RUNTIME_KERNEL_TESTING_MENU
+
+endmenu # "openrisc Testing"
arch/openrisc/kernel/Makefile +1 -0
index 2ac824867963..0c1cade09e0c 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_STACKTRACE)	+= stacktrace.o
 obj-$(CONFIG_MODULES)		+= module.o
 obj-$(CONFIG_OF)		+= prom.o
 obj-$(CONFIG_KPROBES)		+= kprobes.o
+obj-$(CONFIG_OR_KPROBES_KUNIT)	+= tests/
 obj-y	+= patching.o
 
 clean:
arch/openrisc/kernel/tests/Makefile new file +3 -0
new file mode 100644
index 000000000000..9e9693fd3550
@@ -0,0 +1,3 @@
+obj-$(CONFIG_OR_KPROBES_KUNIT)	+= openrisc_test_kprobes.o
+
+openrisc_test_kprobes-objs := test_kprobes.o test_kprobes_asm.o
arch/openrisc/kernel/tests/test_kprobes.c new file +59 -0

59 lines not on this page · raw message

arch/openrisc/kernel/tests/test_kprobes.h new file +24 -0
new file mode 100644
index 000000000000..26c26920d430
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Unit tests borrowed liberally from similar work of RISC-V. All
+ * original copyrights apply as per the original source declaration.
+ */
+
+#ifndef TEST_KPROBES_H
+#define TEST_KPROBES_H
+
+#define KPROBE_TEST_MAGIC          0xDEADC0DE
+#define KPROBE_TEST_MAGIC_LOWER    0x0000C0DE
+#define KPROBE_TEST_MAGIC_UPPER    0x0000DEAD
+
+#ifndef __ASSEMBLER__
+
+/* array of addresses to install kprobes */
+extern void *test_kprobes_addresses[];
+
+/* array of functions that return KPROBE_TEST_MAGIC */
+extern unsigned long (*test_kprobes_functions[])(void);
+
+#endif /* __ASSEMBLER__ */
+
+#endif /* TEST_KPROBES_H */
arch/openrisc/kernel/tests/test_kprobes_asm.S new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
2.55.0
#16 lore raw

PATCH v2 4/4 openrisc: Add Kretprobes

In reply to Sahil Siddiq

Add Kretprobes support for OpenRISC. Kretprobes allow a probe to be fired when a function returns. They are built on top of the generic rethook framework and also require the KProbe mechanism introduced in the previous commit. This work is primarily based on similar work done for RISC-V.

Kretprobes work by inserting a KProbe at a function's entry point. When the function begins to execute, the KProbe is fired and its pre-handler replaces the function's return address with that of the rethook trampoline. The trampoline in turn calls the kretprobe handler. Control is returned to the original caller following this.

---
 arch/openrisc/Kconfig                     |  2 +
 arch/openrisc/kernel/Makefile             |  1 +
 arch/openrisc/kernel/rethook.c            | 32 ++++++++
 arch/openrisc/kernel/rethook.h            |  8 ++
 arch/openrisc/kernel/rethook_trampoline.S | 97 +++++++++++++++++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 arch/openrisc/kernel/rethook.c
 create mode 100644 arch/openrisc/kernel/rethook.h
 create mode 100644 arch/openrisc/kernel/rethook_trampoline.S
arch/openrisc/Kconfig +2 -0
index d240533b424b..82365eb8f574 100644
@@ -28,6 +28,8 @@ config OPENRISC
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_PCI
 	select HAVE_KPROBES
+	select HAVE_KRETPROBES
+	select HAVE_RETHOOK
 	select HAVE_UID16
 	select HAVE_PAGE_SIZE_8KB
 	select HAVE_REGS_AND_STACK_ACCESS_API
arch/openrisc/kernel/Makefile +1 -0
index 0c1cade09e0c..e36537cea319 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_STACKTRACE)	+= stacktrace.o
 obj-$(CONFIG_MODULES)		+= module.o
 obj-$(CONFIG_OF)		+= prom.o
 obj-$(CONFIG_KPROBES)		+= kprobes.o
+obj-$(CONFIG_RETHOOK)		+= rethook.o rethook_trampoline.o
 obj-$(CONFIG_OR_KPROBES_KUNIT)	+= tests/
 obj-y	+= patching.o
 
arch/openrisc/kernel/rethook.c new file +32 -0
new file mode 100644
index 000000000000..50ef6715d044
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Generic return hook for OpenRISC. Depends on KProbes.
+ *
+ * Linux architectural port borrowing liberally from similar works of
+ * others.  All original copyrights apply as per the original source
+ * declaration.
+ *
+ * OpenRISC implementation:
+ * Copyright (C) 2026 Sahil Siddiq <[email protected]>
+ */
+
+#include <linux/kprobes.h>
+#include <linux/rethook.h>
+#include "rethook.h"
+
+unsigned long arch_rethook_trampoline_callback(struct pt_regs *regs)
+{
+	return rethook_trampoline_handler(regs, regs->gpr2);
+}
+NOKPROBE_SYMBOL(arch_rethook_trampoline_callback)
+
+void arch_rethook_prepare(struct rethook_node *rhn, struct pt_regs *regs, bool mcount)
+{
+	/* Store original return address and frame pointer */
+	rhn->ret_addr = regs->gpr9;
+	rhn->frame = regs->gpr2;
+
+	/* Set return address to trampoline */
+	regs->gpr9 = (unsigned long)arch_rethook_trampoline;
+}
+NOKPROBE_SYMBOL(arch_rethook_prepare)
arch/openrisc/kernel/rethook.h new file +8 -0
new file mode 100644
index 000000000000..332306a5c380
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __OPENRISC_RETHOOK_H
+#define __OPENRISC_RETHOOK_H
+
+unsigned long arch_rethook_trampoline_callback(struct pt_regs *regs);
+void arch_rethook_prepare(struct rethook_node *rhn, struct pt_regs *regs, bool mcount);
+
+#endif
arch/openrisc/kernel/rethook_trampoline.S new file +97 -0

97 lines not on this page · raw message

Signature · 1 line
2.55.0
#17 lore raw

Re: [PATCH v2 4/4] openrisc: Add Kretprobes

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] openrisc: kretprobe trampoline leaves regs->sp and regs->sr uninitialized --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] openrisc: kretprobe trampoline leaves regs->sp and regs->sr uninitialized --

commit a5e8b19fd4705325db24ad7195d2d69f741fc493
Author: Sahil Siddiq <[email protected]>

openrisc: Add Kretprobes

This commit adds Kretprobes support for OpenRISC by building on top of the generic rethook framework. It implements the necessary trampoline routines to intercept function returns and route them through the kretprobe handler.

Quoted 5 lines
arch/openrisc/kernel/rethook_trampoline.S new file +0 -0
new file mode 100644
index 0000000000000..ac4ca0ecc6694

[ ... ]

arch/openrisc/kernel/rethook_trampoline.S line 16

Quoted 5 lines
arch/openrisc/kernel/rethook_trampoline.S new file +5 -0
+	.text
+
+	.macro save_all_base_regs
+	l.sw	PT_GPR2(r1),r2
+	l.sw	PT_GPR3(r1),r3

[Severity: High]
Does this macro need to save the stack pointer (r1) to the PT_SP offset and the status register to the PT_SR offset? It appears they are omitted from the base register save list.

[ ... ]

arch/openrisc/kernel/rethook_trampoline.S line 86

Quoted 7 lines
arch/openrisc/kernel/rethook_trampoline.S new file +7 -0
+SYM_CODE_START(arch_rethook_trampoline)
+	/* Allocate enough space on stack for pt_regs */
+	l.addi	r1,r1,-PT_SIZE
+	save_all_base_regs
+
+	l.addi	r3,r1,0	/* pt_regs */
+	l.jal	arch_rethook_trampoline_callback

[Severity: High]
Since save_all_base_regs skips saving r1 (sp) and sr, will the pt_regs passed into arch_rethook_trampoline_callback contain uninitialized stack garbage for regs->sp and regs->sr?

Tracing handlers like BPF or ftrace rely on functions like kernel_stack_pointer() and user_mode() which read these fields. Could these uninitialized fields lead to broken stack unwinding or incorrect privilege evaluation when the kretprobe handlers run?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#15 lore raw

Re: [PATCH v2 3/4] openrisc: Add unit tests for KProbes on branch instructions

In reply to Sahil Siddiq
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] openrisc: uninitialized return value causes kprobe test to unconditionally fail - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] openrisc: uninitialized return value causes kprobe test to unconditionally fail
- [Medium] openrisc: flaky test in test_kprobes_adrp due to missing alignment --

commit d52f067996dd8c21d999dffa9e89c92071ac2e93
Author: Sahil Siddiq <[email protected]>

openrisc: Add unit tests for KProbes on branch instructions

Tests already exist lib/tests to verify the generic functionality of KProbes. Add unit tests to verify that KProbes work on instructions that require simulation in the OpenRISC ISA, and that the corresponding delay slots are handled correctly.

Quoted 5 lines
arch/openrisc/kernel/tests/test_kprobes_asm.S new file +0 -0
new file mode 100644
index 0000000000000..a9fcf99b55ae1

[ ... ]

arch/openrisc/kernel/tests/test_kprobes_asm.S line 112

Quoted 10 lines
arch/openrisc/kernel/tests/test_kprobes_asm.S new file +10 -0
+SYM_FUNC_START(test_kprobes_adrp)
+	l.addi	r13,r0,0
+	l.addi	r15,r9,0
+	l.addi	r17,r0,-8192
+	l.jal	1f
+	l.nop
+1:	l.and	r19,r9,r17
+test_kprobes_adrp_addr:
+	l.adrp	r21,0
+	l.sfeq	r21,r19

[Severity: Medium]
Will this test be flaky due to a lack of alignment directives?

The test verifies the l.adrp instruction by evaluating if it computes the same page base as the 1: label. It derives the page address of 1: by masking its address with -8192, and compares it to the page base computed by l.adrp on the next instruction.

Since there is no alignment directive, the 1: label and the l.adrp instruction might straddle an 8KB page boundary during linkage (e.g., if 1:
is at 0x1FFC and l.adrp is at 0x2000). If they cross the boundary, the two page bases won't match, causing the test to incorrectly branch away to the failure path.

arch/openrisc/kernel/tests/test_kprobes_asm.S line 118

Quoted 6 lines
arch/openrisc/kernel/tests/test_kprobes_asm.S new file +6 -0
+	l.bnf	2f
+	l.nop
+	l.movhi	r13,KPROBE_TEST_MAGIC_UPPER
+	l.ori	r13,r13,KPROBE_TEST_MAGIC_LOWER
+2:	l.addi	r9,r15,0
+	l.jr	r9

[Severity: Medium]
Does this function omit setting the return register, causing the kprobe test to unconditionally fail?

The inline assembly successfully loads the magic value into r13 but omits the l.addi r11, r13, 0 instruction required by the OpenRISC calling convention to move it into the return register (r11) before returning.

Because of this, test_kprobes_adrp() will return whatever garbage data was in r11.

Quoted 2 lines
arch/openrisc/kernel/tests/test_kprobes_asm.S new file +2 -0
+	l.nop
+SYM_FUNC_END(test_kprobes_adrp)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#13 lore raw

Re: [PATCH v2 2/4] openrisc: Add KProbes

In reply to Sahil Siddiq
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] kprobes: unsafe direct memory access in arch_prepare_kprobe() - [High] kprobes: unsafe direct…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] kprobes: unsafe direct memory access in arch_prepare_kprobe()
- [High] kprobes: unsafe direct memory access in kprobe_breakpoint_handler()
- [High] kprobes: SSOL executes with interrupts enabled due to missing regs->sr update
- [High] kprobes: preemption count and lockdep state leak on KPROBE_REENTER
- [Medium] kprobes: missing NOKPROBE_SYMBOL() for simulate_branch() and simulate_pc()
- [Medium] kprobes: l.jalr state corruption on delay slot page fault
--

commit a255007d852431d07365105e6070af0de294b166
Author: Sahil Siddiq <[email protected]>

openrisc: Add KProbes

Add KProbes support for OpenRISC, based on similar work done for LoongArch, MIPS and RISC-V. KProbes make it possible to trap at almost any address in the kernel to collect performance/debugging info.

Quoted 5 lines
arch/openrisc/kernel/kprobes.c new file +0 -0
new file mode 100644
index 0000000000000..3268ffb0d7077

[ ... ]

Quoted 2 lines
arch/openrisc/kernel/kprobes.c new file +2 -0
+int arch_prepare_kprobe(struct kprobe *p)
+{

[ ... ]

arch/openrisc/kernel/kprobes.c line 149

Quoted 13 lines
arch/openrisc/kernel/kprobes.c new file +13 -0
+	if (insns_need_simulation(insn, &exec_delay_slot) && !exec_delay_slot) {
+		p->ainsn.insn = NULL;
+	} else {
+		/*
+		 * Single step probed instruction or, in case of branch instructions, single
+		 * step instruction in delay slot.
+		 */
+		p->ainsn.insn = get_insn_slot();
+		if (!p->ainsn.insn)
+			return -ENOMEM;
+
+		if (exec_delay_slot)
+			patch_insn_write(p->ainsn.insn, *(p->addr + 1));

[Severity: High]
Does this directly dereference p->addr + 1, bypassing the safety provided by copy_from_kernel_nofault() used earlier in the function? If the delay slot instruction falls into an unmapped page, couldn't this cause a kernel panic during probe registration?

Quoted 7 lines
arch/openrisc/kernel/kprobes.c new file +7 -0
+		else
+			patch_insn_write(p->ainsn.insn, p->opcode);
+		patch_insn_write(&p->ainsn.insn[1], KPROBE_SSTEPBP_INSN);
+	}
+
+	return 0;
+}

[ ... ]

arch/openrisc/kernel/kprobes.c line 211

Quoted 8 lines
arch/openrisc/kernel/kprobes.c new file +8 -0
+static void post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb,
+				struct pt_regs *regs)
+{
+	instruction_pointer_set(regs, kcb->restore_pc);
+
+	if (kcb->kprobe_status == KPROBE_REENTER) {
+		restore_previous_kprobe(kcb);
+		return;

[Severity: High]
Does this early return skip calling preempt_enable_no_resched()? If a reentrant kprobe finishes single-stepping, this seems to permanently leak the preemption count disabled in kprobe_breakpoint_handler(), which could lead to a system lockup.

Quoted 10 lines
arch/openrisc/kernel/kprobes.c new file +10 -0
+	}
+
+	kcb->kprobe_status = KPROBE_HIT_SSDONE;
+
+	if (cur->post_handler)
+		cur->post_handler(cur, regs, 0);
+
+	reset_current_kprobe();
+	preempt_enable_no_resched();
+}

[ ... ]

Quoted 7 lines
arch/openrisc/kernel/kprobes.c new file +7 -0
+static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
+			     struct kprobe_ctlblk *kcb, int reenter)
+{
+	kprobe_opcode_t insn;
+
+	if (reenter) {
+		save_previous_kprobe(kcb);

arch/openrisc/kernel/kprobes.c line 240

Quoted 10 lines
arch/openrisc/kernel/kprobes.c new file +10 -0
+		set_current_kprobe(p);
+		kcb->kprobe_status = KPROBE_REENTER;
+	} else {
+		kcb->kprobe_status = KPROBE_HIT_SS;
+	}
+
+	/* Emulate instruction if required. */
+	insn = p->opcode;
+	if (is_branch_insn(insn)) {
+		simulate_branch(regs, insn);

[Severity: Medium]
Since simulate_branch() and simulate_pc() are executed during Kprobe handling, should they be marked with NOKPROBE_SYMBOL() in arch/openrisc/kernel/insn.c? Without it, placing a kprobe on these functions and hitting another branch-related kprobe could cause a reentrant probe loop and trigger the "Unrecoverable KProbe detected" panic.

Quoted 4 lines
arch/openrisc/kernel/kprobes.c new file +4 -0
+		/* Save target addr before updating PC to SSOL slot */
+		kcb->restore_pc = regs->pc;
+	} else if (is_pc_insn(insn)) {
+		simulate_pc(regs, insn);

arch/openrisc/kernel/kprobes.c line 254

Quoted 10 lines
arch/openrisc/kernel/kprobes.c new file +10 -0
+		kcb->restore_pc = regs->pc;
+		/* No SSOL is required here, so call post-process immediately. */
+		post_kprobe_handler(p, kcb, regs);
+		return;
+	} else
+		kcb->restore_pc = regs->pc + OPENRISC_INSN_SIZE;
+
+	if (p->ainsn.insn) {
+		/* Disable IRQs before single stepping */
+		local_irq_save(kcb->irq_flags);

[Severity: High]
Does this only disable interrupts for the exception handler itself, without modifying regs->sr? Since OpenRISC's exception handler returns using regs->sr, it appears the single-step out-of-line (SSOL) instruction would execute with interrupts enabled, violating atomicity and allowing reentrant interrupts.

Quoted 9 lines
arch/openrisc/kernel/kprobes.c new file +9 -0
+		instruction_pointer_set(regs, (unsigned long)p->ainsn.insn);
+	} else {
+		/*
+		 * The instruction is a branch but delay slots are disabled.
+		 * Simply call the post-handler.
+		 */
+		post_kprobe_handler(p, kcb, regs);
+	}
+}

[ ... ]

Quoted 2 lines
arch/openrisc/kernel/kprobes.c new file +2 -0
+bool kprobe_breakpoint_handler(struct pt_regs *regs)
+{

[ ... ]

arch/openrisc/kernel/kprobes.c line 338

Quoted 8 lines
arch/openrisc/kernel/kprobes.c new file +8 -0
+	/*
+	 * The breakpoint instruction was removed right after we hit it
+	 * possibly by another cpu. If the original instruction was also
+	 * a trap instruction then return to the trap handler for further
+	 * processing, else no further processing is required.
+	 */
+	if (get_kernel_nofault(insn, addr) < 0
+		|| (*addr & OPENRISC_MASK_TRAP) != OPENRISC_INSN_TRAP) {

[Severity: High]
Is this directly dereferencing *addr instead of using the safely read 'insn' variable? If the probe is concurrently unregistered and the memory is unmapped, dereferencing *addr directly could cause a kernel panic, defeating the safety of get_kernel_nofault().

Quoted 7 lines
arch/openrisc/kernel/kprobes.c new file +7 -0
+		preempt_enable_no_resched();
+		return true;
+	}
+
+	preempt_enable_no_resched();
+	return false;
+}

[ ... ]

Quoted 4 lines
arch/openrisc/kernel/kprobes.c new file +4 -0
+int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
+{
+	struct kprobe *cur = kprobe_running();
+	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();

arch/openrisc/kernel/kprobes.c line 380

Quoted 10 lines
arch/openrisc/kernel/kprobes.c new file +10 -0
+
+	switch (kcb->kprobe_status) {
+	case KPROBE_HIT_SS:
+	case KPROBE_REENTER:
+		/*
+		 * The instruction being single-stepped caused a page fault.
+		 * Reset the current KProbe and set PC to the probe's address.
+		 * Then allow the page fault handler to continue as usual.
+		 */
+		instruction_pointer_set(regs, (unsigned long)cur->addr);

[Severity: Medium]
If a page fault occurs during the delay slot of an "l.jalr r9" instruction, doesn't rewinding the PC to the branch instruction cause state corruption? simulate_branch() permanently modifies regs->gpr[9], so re-executing the branch after the fault handler returns would jump to the return address instead of the original target.

arch/openrisc/kernel/kprobes.c line 210

Quoted 3 lines
arch/openrisc/kernel/kprobes.c new file +3 -0
+
+		if (kcb->kprobe_status == KPROBE_REENTER) {
+			restore_previous_kprobe(kcb);

[Severity: High]
Does this KPROBE_REENTER path skip local_irq_restore() and preempt_enable_no_resched()? This appears to leak the preemption count and corrupt the lockdep state when a page fault occurs during a reentrant kprobe.

Quoted 11 lines
arch/openrisc/kernel/kprobes.c new file +11 -0
+		} else {
+			regs->sr |= kcb->irq_flags;
+			local_irq_restore(kcb->irq_flags);
+			reset_current_kprobe();
+			preempt_enable_no_resched();
+		}
+		break;
+	}
+
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#11 lore raw

Re: [PATCH v2 1/4] openrisc: Add utilities and clean up simulation of instructions

In reply to Sahil Siddiq
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] openrisc: instruction simulator corrupts sr via gpr[0] aliasing - [High] openrisc: silent text…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] openrisc: instruction simulator corrupts sr via gpr[0] aliasing
- [High] openrisc: silent text patching failure on RO memory
- [Low] build: undefined Kprobe constants and handlers in traps.c
--

commit 55276d42aa86ee1b0ca046c1e956df26c99385de
Author: Sahil Siddiq <[email protected]>

openrisc: Add utilities and clean up simulation of instructions

Introduce new instruction-related utilities and macros for OpenRISC. This is in preparation for patches that add tracing support such as KProbes and Kretprobes.

Quoted 5 lines
arch/openrisc/kernel/insn.c new file +0 -0
new file mode 100644
index 0000000000000..285658512b433

[ ... ]

arch/openrisc/kernel/insn.c line 31

Quoted 13 lines
arch/openrisc/kernel/insn.c new file +13 -0
+void simulate_pc(struct pt_regs *regs, unsigned long pc_insn)
+{
+	int page_offset;
+	unsigned int rd, op;
+
+	page_offset = sign_extend32(
+		(pc_insn & OPENRISC_ADRP_IMM_MASK) << OPENRISC_ADRP_IMM_SHIFT, 31);
+	rd = (pc_insn & OPENRISC_ADRP_REG_MASK) >> OPENRISC_ADRP_REG_SHIFT;
+	op = get_6_bit_opcode(pc_insn);
+
+	switch (op) {
+	case OPENRISC_OPCODE_ADRP:
+		regs->gpr[rd] = page_offset + (regs->pc & OPENRISC_ADRP_OFFSET_MASK);

[Severity: High]
Does this code corrupt the Status Register (sr) if rd is 0?

In OpenRISC's pt_regs, gpr[0] aliases to the Status Register (sr). If a probe is placed on an instruction like l.adrp r0, ..., writing to regs->gpr[rd] without checking if rd == 0 could inadvertently overwrite the kernel's sr.

Quoted 17 lines
arch/openrisc/kernel/insn.c new file +17 -0
+		regs->pc += OPENRISC_INSN_SIZE;
+		return;
+	default:
+		break;
+	}
+}
+
+void simulate_branch(struct pt_regs *regs, unsigned long jmp_insn)
+{
+	int displacement;
+	unsigned int rb, op, link_offset, temp;
+
+	displacement = sign_extend32(
+		((jmp_insn) & OPENRISC_JUMP_IMM_MASK) << OPENRISC_JUMP_IMM_SHIFT, 27);
+	rb = (jmp_insn & OPENRISC_JUMP_REG_MASK) >> OPENRISC_JUMP_REG_SHIFT;
+	op = get_6_bit_opcode(jmp_insn);
+	link_offset = OPENRISC_INSN_SIZE;

[ ... ]

Quoted 4 lines
arch/openrisc/kernel/insn.c new file +4 -0
+	switch (op) {
+	case OPENRISC_OPCODE_J: /* l.j */
+		regs->pc += displacement;
+		return;

[ ... ]

arch/openrisc/kernel/insn.c

Quoted 2 lines
arch/openrisc/kernel/insn.c new file +2 -0
+	case OPENRISC_OPCODE_JR: /* l.jr */
+		regs->pc = regs->gpr[rb];

[Severity: High]
Similarly, does this code read the Status Register instead of 0 if rb is 0?

Since gpr[0] aliases to sr in pt_regs, an instruction like l.jr r0 would end up reading the sr rather than 0 in simulate_branch().

Quoted 16 lines
arch/openrisc/kernel/insn.c new file +10 -0
+		return;
+	case OPENRISC_OPCODE_JALR: /* l.jalr */
+		temp = regs->pc + link_offset;
+		regs->pc = regs->gpr[rb];
+		regs->gpr[9] = temp;
+		return;
+	default:
+		break;
+	}
+}
arch/openrisc/kernel/setup.c +0 -0
index 000a9cc10e6f6..0f69c36472d55 100644

arch/openrisc/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 261

Quoted 7 lines
arch/openrisc/kernel/setup.c +4 -0
@@ -255,6 +255,10 @@ void __init setup_arch(char **cmdline_p)
 	/* paging_init() sets up the MMU and marks all pages as reserved */
 	paging_init();
 
+	/* determine if the processor supports delay slots */
+	if (mfspr(SPR_CPUCFGR) & SPR_CPUCFGR_ND)
+		static_branch_disable(&delay_slot);
+

[Severity: High]
Does this static branch update silently fail to patch read-only memory?

Since this is called immediately after paging_init() marks the kernel text as read-only, and interrupts are disabled, jump label text patching will fall back to copy_to_kernel_nofault(). This might silently fail to write to the read-only memory, leaving the delay_slot key incorrectly enabled on CPUs without delay slots and leading to corrupted return addresses.

Quoted 8 lines
arch/openrisc/kernel/setup.c +0 -0
 	*cmdline_p = boot_command_line;
 
 	printk(KERN_INFO "OpenRISC Linux -- http://openrisc.io\n");
arch/openrisc/kernel/traps.c +0 -0
index c195be9cc9fcc..c9ab630ec8950 100644

[ ... ]

Quoted 10 lines
arch/openrisc/kernel/traps.c +6 -0
@@ -215,6 +216,32 @@ asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
 	if (user_mode(regs)) {
 		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->pc);
 	} else {
+		unsigned long trap, bcode;
+
+		if (get_kernel_nofault(trap, (unsigned long *)regs->pc) < 0)
+			return;
+
+		/*

arch/openrisc/kernel/traps.c asmlinkage void do_trap(struct pt_regs *regs, unsigned long address) · line 234

Quoted 10 lines
arch/openrisc/kernel/traps.c +10 -0
+		 * Trap instruction was probably removed and no further processing
+		 * is required.
+		 */
+		if ((trap & OPENRISC_MASK_TRAP) != OPENRISC_INSN_TRAP)
+			return;
+
+		bcode = (trap & OPENRISC_MASK_BCODE);
+		switch (bcode) {
+		case BRK_KPROBE_BP:
+			if (kprobe_breakpoint_handler(regs))

[Severity: Low]
Will this unconditionally break the build during a git bisect?

BRK_KPROBE_BP and kprobe_breakpoint_handler() do not appear to be defined until a follow-up commit ("openrisc: Add KProbes"). Introducing their usage here in do_trap() will result in undefined constants and undeclared functions when compiling this commit.

Quoted 13 lines
arch/openrisc/kernel/traps.c +10 -0
+				return;
+			break;
+		case BRK_KPROBE_SSTEPBP:
+			if (kprobe_singlestep_handler(regs))
+				return;
+			break;
+		default:
+			break;
+		}
+
 		pr_emerg("KERNEL: Illegal trap exception 0x%.8lx\n", regs->pc);
 		die("Die:", regs, SIGILL);
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#7 lore raw
In reply to Sahil Siddiq
Quoted 17 lines · Sahil Siddiq

Hi,

Apologies for the delay. This is v2 of the patch series to add KProbe support for OpenRISC.

The series also adds support for Kretprobes (which depends on the rethook framework [1]). Kretprobes are fired on function return.

[...]

I have also made changes based on the review comments I received for v1 [5] including several of Sachiko's comments [6]. I chose not to make changes in a few places. I'll send emails as a reply to this thread explaining my reasoning inline.

[...]

Quoted 7 lines

Sahil Siddiq (4):
openrisc: Add utilities and clean up simulation of instructions
openrisc: Add KProbes
openrisc: Add unit tests for KProbes on branch instructions
openrisc: Add Kretprobes

[...]>

Sorry, I forgot to post a summary of the differences between v2 and v1:

Commit #1:
- arch/openrisc/include/asm/cpuinfo.h: Introduce "delay_slot" static key
- arch/openrisc/include/asm/insn-def.h:
- Use macros instead of union
- Use better names for macros
- Use genmask() for masks
- arch/openrisc/kernel/insn.c:
- Use macros instead of magic values
- (simulate_pc): Update PC
- (simulate_branch): Handle rb=r9 in l.jalr simulation
- arch/openrisc/kernel/setup.c: Use "delay slot" static key
- arch/openrisc/kernel/traps.c: Use macros instead of magic values

Commit #2:
- arch/openrisc/include/asm/kprobes.h:
- (struct prev_kprobe): Add restore_pc
- (struct kprobe_ctlblk): Move restore_pc from global "struct arch_specific_insn"
- arch/openrisc/kernel/kprobes.c:
- Use macros instead of magic values
- Use "get_kernel_nofault" instead of directly dereferencing address
- (insns_need_simulation): Use "daley slot" static key
- (arch_prepare_kprobe): Prevent KProbe insertion if instruction in delay slot also requires simulation
- (save_previous_kprobe): Use "restore_pc"
- (restore_previous_kprobe): Likewise
- (post_kprobe_handler): Don't re-enable preemption if KProbe is nested
- (setup_singlestep): Use "restore_pc"
- (reenter_kprobe): Return false in case of unrecoverable KProbe
- (kprobe_singlestep_handler): Enable interrupt in pt_regs->sr
- (kprobe_fault_handler): Likewise

Commit #3/#4: Introduced in v2 of the series

Thanks,
Sahil

#8 lore raw
In reply to Sahil Siddiq

Hi Sahil,

Thanks for updating! Could you check Sashiko's comments on the series?

https://sashiko.dev/#/patchset/20260930203129.51048-1-sahilcdq0%40gmail.com

I need OpenRISC maintainer's review for this series too.

Thank you,

Quoted 142 lines · Sahil Siddiq

Hi,

Apologies for the delay. This is v2 of the patch series to add KProbe support for OpenRISC.

The series also adds support for Kretprobes (which depends on the rethook framework [1]). Kretprobes are fired on function return.

In order to build the kernel with Kprobes and Kretprobes enabled, the following config options are required:
1. CONFIG_HAVE_KPROBES=y
2. CONFIG_KPROBES=y
3. CONFIG_RETHOOK=y
4. CONFIG_KRETPROBES=y

I tested the generic functionality of Kprobes and Kretprobes using the tests located at "lib/tests" [2], samples located at "samples/kprobes" [3], as well as a few test modules that I have published here [4]. The following options are required to build the samples and tests:
1. CONFIG_SAMPLES=y
2. CONFIG_SAMPLE_KPROBES=m
3. CONFIG_SAMPLE_KRETPROBES=m
4. CONFIG_KPROBES_SANITY_TEST=m

I have also added unit tests at "arch/openrisc/kernel/tests" to verify that Kprobes work properly when inserted at branch instructions, and that delay slots are handled correctly. The tests are based on the Kprobe testsuite added for RISC-V. The following options need to be enabled to build the tests:
1. CONFIG_KUNIT=y
2. CONFIG_RUNTIME_KERNEL_TESTING_MENU=y
3. CONFIG_OR_KPROBES_KUNIT=m

Below are the results of the tests and sample modules when run in my environment.

- kretprobe_example:

/home # insmod kretprobe_example.ko
Planted return probe at kernel_clone: (ptrval) /home # ls >/dev/null
kernel_clone returned 35 and took 1302450 ns to execute /home # rmmod kretprobe_example.ko kernel_clone returned 36 and took 1121200 ns to execute kretprobe at (ptrval) unregistered
Missed probing 0 instances of kernel_clone

- test_kprobes:

/home # insmod test_kprobes.ko
KTAP version 1
1..1
KTAP version 1
# Subtest: kprobes_test
# module: test_kprobes
1..5
ok 1 test_kprobe
ok 2 test_kprobes
ok 3 test_kprobe_missed
ok 4 test_kretprobe
ok 5 test_kretprobes
# kprobes_test: pass:5 fail:0 skip:0 total:5
# Totals: pass:5 fail:0 skip:0 total:5
ok 1 kprobes_test

- openrisc_test_kprobes:

/home # insmod openrisc_test_kprobes.ko
KTAP version 1
1..1
KTAP version 1
# Subtest: kprobes_openrisc
# module: openrisc_test_kprobes
1..1
ok 1 test_kprobe_openrisc
ok 1 kprobes_openrisc

I have also made changes based on the review comments I received for v1 [5] including several of Sachiko's comments [6]. I chose not to make changes in a few places. I'll send emails as a reply to this thread explaining my reasoning inline.

Thanks,
Sahil

[1] https://lkml.rescloud.iu.edu/2203.0/05974.html
[2] https://github.com/torvalds/linux/tree/master/lib/tests
[3] https://github.com/torvalds/linux/tree/master/samples/kprobes
[4] https://github.com/valdaarhun/or-dev
[5] https://lwn.net/ml/all/[email protected]/
[6] https://sashiko.dev/#/patchset/20260407185650.79816-1-sahilcdq0%40gmail.com

Sahil Siddiq (4):
openrisc: Add utilities and clean up simulation of instructions
openrisc: Add KProbes
openrisc: Add unit tests for KProbes on branch instructions
openrisc: Add Kretprobes

 arch/openrisc/Kconfig                         |   3 +
 arch/openrisc/Kconfig.debug                   |  30 +-
 arch/openrisc/configs/or1ksim_defconfig       |   2 +
 arch/openrisc/configs/virt_defconfig          |   2 +
 arch/openrisc/include/asm/asm.h               |  22 +
 arch/openrisc/include/asm/break.h             |  19 +
 arch/openrisc/include/asm/cpuinfo.h           |   3 +
 arch/openrisc/include/asm/insn-def.h          |  64 ++-
 arch/openrisc/include/asm/kprobes.h           |  77 ++++
 arch/openrisc/include/asm/spr_defs.h          |   1 +
 arch/openrisc/kernel/Makefile                 |   5 +-
 arch/openrisc/kernel/entry.S                  |  16 +
 arch/openrisc/kernel/insn.c                   |  84 ++++
 arch/openrisc/kernel/kprobes.c                | 400 ++++++++++++++++++
 arch/openrisc/kernel/rethook.c                |  32 ++
 arch/openrisc/kernel/rethook.h                |   8 +
 arch/openrisc/kernel/rethook_trampoline.S     |  97 +++++
 arch/openrisc/kernel/setup.c                  |   4 +
 arch/openrisc/kernel/tests/Makefile           |   3 +
 arch/openrisc/kernel/tests/test_kprobes.c     |  59 +++
 arch/openrisc/kernel/tests/test_kprobes.h     |  24 ++
 arch/openrisc/kernel/tests/test_kprobes_asm.S | 147 +++++++
 arch/openrisc/kernel/traps.c                  |  82 ++--
 arch/openrisc/lib/memcpy.c                    |   2 +
 arch/openrisc/lib/memset.S                    |   4 +
 arch/openrisc/mm/fault.c                      |   5 +
 samples/kprobes/kprobe_example.c              |   8 +
 27 files changed, 1152 insertions(+), 51 deletions(-)
 create mode 100644 arch/openrisc/include/asm/asm.h
 create mode 100644 arch/openrisc/include/asm/break.h
 create mode 100644 arch/openrisc/include/asm/kprobes.h
 create mode 100644 arch/openrisc/kernel/insn.c
 create mode 100644 arch/openrisc/kernel/kprobes.c
 create mode 100644 arch/openrisc/kernel/rethook.c
 create mode 100644 arch/openrisc/kernel/rethook.h
 create mode 100644 arch/openrisc/kernel/rethook_trampoline.S
 create mode 100644 arch/openrisc/kernel/tests/Makefile
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes.c
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes.h
 create mode 100644 arch/openrisc/kernel/tests/test_kprobes_asm.S
Signature · 1 line
2.55.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author
#9 lore raw

Hi Masami,

Quoted 5 lines · Masami Hiramatsu (Google)

Hi Sahil,

Thanks for updating! Could you check Sashiko's comments on the series?

https://sashiko.dev/#/patchset/20260930203129.51048-1-sahilcdq0%40gmail.com

Thank you for your reply, and apologies for being slow in sending an update.

I have seen Sachiko's review comments. I am still working on getting v3 ready.

Quoted 1 line

I need OpenRISC maintainer's review for this series too.

Got it, that makes sense.

Quoted 4 lines

Thank you,

[...]

Regards,
Sahil