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

Stafford Horne
2026-04-14T17:11:41Z

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.

On Wed, Apr 08, 2026 at 12:26:49AM +0530, Sahil Siddiq wrote:
> 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

> 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.
>
> Link: https://raw.githubusercontent.com/openrisc/doc/master/openrisc-arch-1.4-rev0.pdf
> Signed-off-by: Sahil Siddiq <sahilcdq0@gmail.com>
> ---
> 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
>
> diff --git a/arch/openrisc/include/asm/insn-def.h b/arch/openrisc/include/asm/insn-def.h
> index 1e0c028a5b95..c98f9770c52e 100644
> --- a/arch/openrisc/include/asm/insn-def.h
> +++ b/arch/openrisc/include/asm/insn-def.h
> @@ -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

Why remove the OPENRISC_ namespace from thse definitions?

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

> +
> +#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.

> + 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);
> +}

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?

> +
> +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 */
> diff --git a/arch/openrisc/include/asm/spr_defs.h b/arch/openrisc/include/asm/spr_defs.h
> index f0b6b492e9f4..5d13a9b96263 100644
> --- a/arch/openrisc/include/asm/spr_defs.h
> +++ b/arch/openrisc/include/asm/spr_defs.h
> @@ -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 */
>
> /*
> diff --git a/arch/openrisc/kernel/Makefile b/arch/openrisc/kernel/Makefile
> index 19e0eb94f2eb..150779fbf010 100644
> --- a/arch/openrisc/kernel/Makefile
> +++ b/arch/openrisc/kernel/Makefile
> @@ -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
>
> diff --git a/arch/openrisc/kernel/insn.c b/arch/openrisc/kernel/insn.c
> new file mode 100644
> index 000000000000..2c97eceee6d7
> --- /dev/null
> +++ b/arch/openrisc/kernel/insn.c
> @@ -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 <sahilcdq0@gmail.com>
> + */
> +
> +#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));
> + 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?

> +
> +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.

> +{
> + 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?

> + 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;
> + }
> +}
> diff --git a/arch/openrisc/kernel/jump_label.c b/arch/openrisc/kernel/jump_label.c
> index ab7137c23b46..fe082eb847a4 100644
> --- a/arch/openrisc/kernel/jump_label.c
> +++ b/arch/openrisc/kernel/jump_label.c
> @@ -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)
> diff --git a/arch/openrisc/kernel/traps.c b/arch/openrisc/kernel/traps.c
> index c195be9cc9fc..ee87a3af34fc 100644
> --- a/arch/openrisc/kernel/traps.c
> +++ b/arch/openrisc/kernel/traps.c
> @@ -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;
> }
> --
> 2.53.0
>