Skip to content

Commit 79bc3f8

Browse files
covanampalmer-dabbelt
authored andcommitted
riscv: correct riscv_insn_is_c_jr() and riscv_insn_is_c_jalr()
The instructions c.jr and c.jalr must have rs1 != 0, but riscv_insn_is_c_jr() and riscv_insn_is_c_jalr() do not check for this. So, riscv_insn_is_c_jr() can match a reserved encoding, while riscv_insn_is_c_jalr() can match the c.ebreak instruction. Rewrite them with check for rs1 != 0. Signed-off-by: Nam Cao <[email protected]> Reviewed-by: Charlie Jenkins <[email protected]> Fixes: ec5f908 ("RISC-V: Move riscv_insn_is_* macros into a common header") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 52449c1 commit 79bc3f8

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

arch/riscv/include/asm/insn.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
#define RVC_INSN_FUNCT4_OPOFF 12
111111
#define RVC_INSN_FUNCT3_MASK GENMASK(15, 13)
112112
#define RVC_INSN_FUNCT3_OPOFF 13
113+
#define RVC_INSN_J_RS1_MASK GENMASK(11, 7)
113114
#define RVC_INSN_J_RS2_MASK GENMASK(6, 2)
114115
#define RVC_INSN_OPCODE_MASK GENMASK(1, 0)
115116
#define RVC_ENCODE_FUNCT3(f_) (RVC_FUNCT3_##f_ << RVC_INSN_FUNCT3_OPOFF)
@@ -245,8 +246,6 @@ __RISCV_INSN_FUNCS(c_jal, RVC_MASK_C_JAL, RVC_MATCH_C_JAL)
245246
__RISCV_INSN_FUNCS(auipc, RVG_MASK_AUIPC, RVG_MATCH_AUIPC)
246247
__RISCV_INSN_FUNCS(jalr, RVG_MASK_JALR, RVG_MATCH_JALR)
247248
__RISCV_INSN_FUNCS(jal, RVG_MASK_JAL, RVG_MATCH_JAL)
248-
__RISCV_INSN_FUNCS(c_jr, RVC_MASK_C_JR, RVC_MATCH_C_JR)
249-
__RISCV_INSN_FUNCS(c_jalr, RVC_MASK_C_JALR, RVC_MATCH_C_JALR)
250249
__RISCV_INSN_FUNCS(c_j, RVC_MASK_C_J, RVC_MATCH_C_J)
251250
__RISCV_INSN_FUNCS(beq, RVG_MASK_BEQ, RVG_MATCH_BEQ)
252251
__RISCV_INSN_FUNCS(bne, RVG_MASK_BNE, RVG_MATCH_BNE)
@@ -273,6 +272,18 @@ static __always_inline bool riscv_insn_is_branch(u32 code)
273272
return (code & RV_INSN_OPCODE_MASK) == RVG_OPCODE_BRANCH;
274273
}
275274

275+
static __always_inline bool riscv_insn_is_c_jr(u32 code)
276+
{
277+
return (code & RVC_MASK_C_JR) == RVC_MATCH_C_JR &&
278+
(code & RVC_INSN_J_RS1_MASK) != 0;
279+
}
280+
281+
static __always_inline bool riscv_insn_is_c_jalr(u32 code)
282+
{
283+
return (code & RVC_MASK_C_JALR) == RVC_MATCH_C_JALR &&
284+
(code & RVC_INSN_J_RS1_MASK) != 0;
285+
}
286+
276287
#define RV_IMM_SIGN(x) (-(((x) >> 31) & 1))
277288
#define RVC_IMM_SIGN(x) (-(((x) >> 12) & 1))
278289
#define RV_X(X, s, mask) (((X) >> (s)) & (mask))

0 commit comments

Comments
 (0)