Re: [PATCH v3 1/4] openrisc: Add utilities and clean up simulation of instructions
From: Sahil
Date: Wed Nov 18 2026 - 06:32:50 EST
On 11/15/26 6:43 AM, Stafford Horne wrote:
On Fri, Nov 13, 2026 at 10:41:56PM +0530, Sahil wrote:Got it. Thank you for the clarification. Based on our conversation in commit #3
Hi Stafford,
On 11/7/26 12:15 PM, Stafford Horne wrote:
Hi Sahil,
Thanks for your patches, sorry its taken me time to get around to them, my year
since summer has been quite hectic.
No worries. I completely understand :)
I will do the review in chunks so give me some time.
Thank you for the review.
On Tue, Oct 27, 2026 at 01:42:37AM +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 and Kretprobes.
[...]
+
+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;
+
+ if (static_branch_likely(&delay_slot))
+ link_offset <<= 1;
Why do shift left here instead of += OPENRISC_INSN_SIZE? Since logically we are
adding one more insn for the delay slot, not doubling.
Since link_offset is initialized to OPENRISC_INSN_SIZE, shifting it to the left by 1 would
update link_offset to 2 * OPENRISC_INSN_SIZE. This is a little more obscure than explicitly
setting it to 2 * OPENRISC_INSN_SIZE. Initially, I thought the left shift operator might be
less expensive than multiplying.
I was going through mor1kx's design and noticed there's also a pipelined implementation for
the multiplier [1]. But it looks like the implementation for shifting is simpler and can be done
in one cycle. Please let me know if my understanding is incorrect here.
That's correct, but my suggestion was to use += OPENRISC_INSN_SIZE instead of
2 *.
However, It may be more efficient to avoid using the alu and write as:
if (static_branch_likely(&delay_slot))
link_offset = 2 * OPENRISC_INSN_SIZE;
else
link_offset = OPENRISC_INSN_SIZE;
what do you think?
I am not sure if I have understood the alu part. WIll it be more efficient because 2 * OPENRISC_INSN_SIZE
will be optimized by the compiler to simply be a single number? So no alu operation will be required,
just one store operation?
Yup, since 2 * OPENRISC_INSN_SIZE is a constant expression the compiler
will replace it with 8. Then the above code would be just one store operation,
something like:
if (static_branch_likely(&delay_slot))
link_offset = 8;
else
link_offset = 4;
regarding delay slots, it should now be fine to remove the static key and handle
delay slots unconditionally.
+ switch (op) {
+ case OPENRISC_OPCODE_J: /* l.j */
I think these comments (/* l.j */) are no longer needed sinc you have
the named macros.
Got it, I'll remove the comments.
[snip]
Thanks,
-Stafford
Thanks,
Sahil