Skip to contentSkip to conversation

disas/riscv.c: fix inst_length()

PATCH Applied Daniel Henrique Barboza 2 participants 4 messages May 27 – Jun 1 RISC-V

lore
Share & tools
lore
#0 lore raw

v2 cover disas/riscv.c: fix inst_length()

Hello,

In this new version we're fixing the disas function by reducing the encodings we support, which is super fine. Thanks to Alistair for pointing this out in the v1 review.

Changes from v1:
- support only 16 and 32 bit inst length as specified in the RISC-V
unpriv ISA
- v1 link: https://lore.kernel.org/qemu-devel/[email protected]/

Daniel Henrique Barboza (1):
disas/riscv.c: fix inst_length()

 disas/riscv.c | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/1 disas/riscv.c: fix inst_length()

inst_length() can return 0 if 'inst' happens to not match any known encoding (like [1]). Returning 0 is not desirable, even for unknown encodings, given that it will cause a loop in target_disas() later on.

The most recent version of the RISC-V unpriv spec ditched the sophisticated instruction-length encoding. We're now supporting only 16-bit and 32-bit length instructions, where:

"All the 32-bit instructions in the base ISA have their lowest two bits set to 11. The optional compressed 16-bit instruction-set extensions have their lowest two bits equal to 00, 01, or 10."

So the code is now simpler, never returning 0, and in fact it's the same thing we're already doing in insn_len() from target/riscv/internals.h. Due to include shenarigans we can't use that function in disas/riscv.c, but I believe we can cut ourselves some slack this time and not lose sleep over a 1 line of duplicated logic. We're documenting it though!

[1] https://gitlab.com/qemu-project/qemu/-/work_items/3479

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3479

---
 disas/riscv.c | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
disas/riscv.c +2 -18
index d416a4d6b3..fbdb24fcea 100644
@@ -5057,26 +5057,10 @@ static bool check_constraints(rv_decode *dec, const rvc_constraint *c)
     return true;
 }
 
-/* instruction length */
-
+/* Same as insn_len() from target/riscv/internals.h */
 static size_t inst_length(rv_inst inst)
 {
-    /* NOTE: supports maximum instruction size of 64-bits */
-
-    /*
-     * instruction length coding
-     *
-     *      aa - 16 bit aa != 11
-     *   bbb11 - 32 bit bbb != 111
-     *  011111 - 48 bit
-     * 0111111 - 64 bit
-     */
-
-    return (inst &      0b11) != 0b11      ? 2
-         : (inst &   0b11100) != 0b11100   ? 4
-         : (inst &  0b111111) == 0b011111  ? 6
-         : (inst & 0b1111111) == 0b0111111 ? 8
-         : 0;
+    return (inst & 3) == 3 ? 4 : 2;
 }
 
 /* format instruction */
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#2 lore raw
Quoted 19 lines · Daniel Henrique Barboza

inst_length() can return 0 if 'inst' happens to not match any known encoding (like [1]). Returning 0 is not desirable, even for unknown encodings, given that it will cause a loop in target_disas() later on.

The most recent version of the RISC-V unpriv spec ditched the sophisticated instruction-length encoding. We're now supporting only 16-bit and 32-bit length instructions, where:

"All the 32-bit instructions in the base ISA have their lowest two bits set to 11. The optional compressed 16-bit instruction-set extensions have their lowest two bits equal to 00, 01, or 10."

So the code is now simpler, never returning 0, and in fact it's the same thing we're already doing in insn_len() from target/riscv/internals.h. Due to include shenarigans we can't use that function in disas/riscv.c, but I believe we can cut ourselves some slack this time and not lose sleep over a 1 line of duplicated logic. We're documenting it though!

Quoted 4 lines

[1] https://gitlab.com/qemu-project/qemu/-/work_items/3479

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3479

Alistair

Quoted 41 lines
---
 disas/riscv.c | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
disas/riscv.c +2 -19
index d416a4d6b3..fbdb24fcea 100644
@@ -5057,26 +5057,10 @@ static bool check_constraints(rv_decode *dec, const rvc_constraint *c)
     return true;
 }
 
-/* instruction length */
-
+/* Same as insn_len() from target/riscv/internals.h */
 static size_t inst_length(rv_inst inst)
 {
-    /* NOTE: supports maximum instruction size of 64-bits */
-
-    /*
-     * instruction length coding
-     *
-     *      aa - 16 bit aa != 11
-     *   bbb11 - 32 bit bbb != 111
-     *  011111 - 48 bit
-     * 0111111 - 64 bit
-     */
-
-    return (inst &      0b11) != 0b11      ? 2
-         : (inst &   0b11100) != 0b11100   ? 4
-         : (inst &  0b111111) == 0b011111  ? 6
-         : (inst & 0b1111111) == 0b0111111 ? 8
-         : 0;
+    return (inst & 3) == 3 ? 4 : 2;
 }
 
 /* format instruction */
--

2.43.0

#3 lore raw
In reply to Daniel Henrique Barboza
Quoted 9 lines · Daniel Henrique Barboza

Hello,

In this new version we're fixing the disas function by reducing the encodings we support, which is super fine. Thanks to Alistair for pointing this out in the v1 review.

Quoted 4 lines

Changes from v1:
- support only 16 and 32 bit inst length as specified in the RISC-V
unpriv ISA
- v1 link: https://lore.kernel.org/qemu-devel/[email protected]/

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 12 lines

Daniel Henrique Barboza (1):
disas/riscv.c: fix inst_length()

 disas/riscv.c | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)

--
2.43.0