Skip to contentSkip to conversation

riscv, kprobes: Stricter c.jr/c.jalr decoding

PATCH Applied Björn Töpel 5 participants 7 messages Jan 2, 2023 – Jan 5, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv, kprobes: Stricter c.jr/c.jalr decoding

From: Björn Töpel <[email protected]>

In the compressed instruction extension, c.jr, c.jalr, c.mv, and c.add is encoded the following way (each instruction is 16b):

---+-+-----------+-----------+--
100 0 rs1[4:0]!=0 00000 10 : c.jr
100 1 rs1[4:0]!=0 00000 10 : c.jalr
100 0 rd[4:0]!=0 rs2[4:0]!=0 10 : c.mv
100 1 rd[4:0]!=0 rs2[4:0]!=0 10 : c.add

The following logic is used to decode c.jr and c.jalr:

insn & 0xf007 == 0x8002 => instruction is an c.jr insn & 0xf007 == 0x9002 => instruction is an c.jalr

When 0xf007 is used to mask the instruction, c.mv can be incorrectly decoded as c.jr, and c.add as c.jalr.

Correct the decoding by changing the mask from 0xf007 to 0xf07f.

Fixes c22b0bcb1dd0 ("riscv: Add kprobes supported")
---
 arch/riscv/kernel/probes/simulate-insn.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/probes/simulate-insn.h +2 -2
index cb6ff7dccb92..de8474146a9b 100644
@@ -31,9 +31,9 @@ __RISCV_INSN_FUNCS(fence,	0x7f, 0x0f);
 	} while (0)
 
 __RISCV_INSN_FUNCS(c_j,		0xe003, 0xa001);
-__RISCV_INSN_FUNCS(c_jr,	0xf007, 0x8002);
+__RISCV_INSN_FUNCS(c_jr,	0xf07f, 0x8002);
 __RISCV_INSN_FUNCS(c_jal,	0xe003, 0x2001);
-__RISCV_INSN_FUNCS(c_jalr,	0xf007, 0x9002);
+__RISCV_INSN_FUNCS(c_jalr,	0xf07f, 0x9002);
 __RISCV_INSN_FUNCS(c_beqz,	0xe003, 0xc001);
 __RISCV_INSN_FUNCS(c_bnez,	0xe003, 0xe001);
 __RISCV_INSN_FUNCS(c_ebreak,	0xffff, 0x9002);

base-commit: 1b929c02afd37871d5afb9d498426f83432e71c2

Signature · 1 line
2.37.2
Reviewed-by on v1 1/1
#1 lore raw

Hey Bjorn,

Quoted 32 lines · Björn Töpel

From: Björn Töpel <[email protected]>

In the compressed instruction extension, c.jr, c.jalr, c.mv, and c.add is encoded the following way (each instruction is 16b):

---+-+-----------+-----------+--
100 0 rs1[4:0]!=0 00000 10 : c.jr
100 1 rs1[4:0]!=0 00000 10 : c.jalr
100 0 rd[4:0]!=0 rs2[4:0]!=0 10 : c.mv
100 1 rd[4:0]!=0 rs2[4:0]!=0 10 : c.add

The following logic is used to decode c.jr and c.jalr:

insn & 0xf007 == 0x8002 => instruction is an c.jr insn & 0xf007 == 0x9002 => instruction is an c.jalr

When 0xf007 is used to mask the instruction, c.mv can be incorrectly decoded as c.jr, and c.add as c.jalr.

Correct the decoding by changing the mask from 0xf007 to 0xf07f.

Fixes c22b0bcb1dd0 ("riscv: Add kprobes supported")
---
 arch/riscv/kernel/probes/simulate-insn.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/probes/simulate-insn.h +0 -0
index cb6ff7dccb92..de8474146a9b 100644

arch/riscv/kernel/probes/simulate-insn.h __RISCV_INSN_FUNCS(fence, 0x7f, 0x0f); · line 34

Quoted 4 lines
arch/riscv/kernel/probes/simulate-insn.h +0 -1
@@ -31,9 +31,9 @@ __RISCV_INSN_FUNCS(fence,	0x7f, 0x0f);
 	} while (0)
 
 __RISCV_INSN_FUNCS(c_j,		0xe003, 0xa001);
-__RISCV_INSN_FUNCS(c_jr,	0xf007, 0x8002);

Hmm, I wonder where the mask originally came from!
I had a look at the compressed spec, of which the version google gave to me was v1.9 [1], and Table 1.6 in that (Instruction listing for RVC, Quadrant 2) seems to list them all together.
Tedious it may be, but future instruction decoding bits probably need more scrutiny as Drew found another clearly wrong bit a few weeks ago [2].

Anyways, I checked against the doc and the new versions look good to me. How'd you spot this, and did you check the other masks?

[1] - https://riscv.org/wp-content/uploads/2015/11/riscv-compressed-spec-v1.9.pdf
[2] - https://lore.kernel.org/linux-riscv/[email protected]/

arch/riscv/kernel/probes/simulate-insn.h __RISCV_INSN_FUNCS(fence, 0x7f, 0x0f); · line 39

Quoted 7 lines
arch/riscv/kernel/probes/simulate-insn.h +2 -1
+__RISCV_INSN_FUNCS(c_jr,	0xf07f, 0x8002);
 __RISCV_INSN_FUNCS(c_jal,	0xe003, 0x2001);
-__RISCV_INSN_FUNCS(c_jalr,	0xf007, 0x9002);
+__RISCV_INSN_FUNCS(c_jalr,	0xf07f, 0x9002);
 __RISCV_INSN_FUNCS(c_beqz,	0xe003, 0xc001);
 __RISCV_INSN_FUNCS(c_bnez,	0xe003, 0xe001);
 __RISCV_INSN_FUNCS(c_ebreak,	0xffff, 0x9002);

Worth noting that this code is gone in riscv/for-next thanks to Heiko's de-duplication:
https://lore.kernel.org/linux-riscv/[email protected]/

Quoted 5 lines

base-commit: 1b929c02afd37871d5afb9d498426f83432e71c2

Signature · 1 line
2.37.2
#2 lore raw
Quoted 37 lines · Conor Dooley

Hey Bjorn,

Quoted 34 lines · Björn Töpel

From: Björn Töpel <[email protected]>

In the compressed instruction extension, c.jr, c.jalr, c.mv, and c.add is encoded the following way (each instruction is 16b):

---+-+-----------+-----------+--
100 0 rs1[4:0]!=0 00000 10 : c.jr
100 1 rs1[4:0]!=0 00000 10 : c.jalr
100 0 rd[4:0]!=0 rs2[4:0]!=0 10 : c.mv
100 1 rd[4:0]!=0 rs2[4:0]!=0 10 : c.add

The following logic is used to decode c.jr and c.jalr:

insn & 0xf007 == 0x8002 => instruction is an c.jr insn & 0xf007 == 0x9002 => instruction is an c.jalr

When 0xf007 is used to mask the instruction, c.mv can be incorrectly decoded as c.jr, and c.add as c.jalr.

Correct the decoding by changing the mask from 0xf007 to 0xf07f.

Fixes c22b0bcb1dd0 ("riscv: Add kprobes supported")
---
 arch/riscv/kernel/probes/simulate-insn.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/probes/simulate-insn.h +0 -0
index cb6ff7dccb92..de8474146a9b 100644
@@ -31,9 +31,9 @@ __RISCV_INSN_FUNCS(fence,	0x7f, 0x0f);
 	} while (0)
 
Quoted 4 lines

arch/riscv/kernel/probes/simulate-insn.h __RISCV_INSN_FUNCS(fence, 0x7f, 0x0f); · line 34

Quoted 2 lines
arch/riscv/kernel/probes/simulate-insn.h +0 -1
 __RISCV_INSN_FUNCS(c_j,		0xe003, 0xa001);
-__RISCV_INSN_FUNCS(c_jr,	0xf007, 0x8002);

Hmm, I wonder where the mask originally came from!

I think it's just a simple bug -- missing that "rs2" must be zero.

Quoted 9 lines

I had a look at the compressed spec, of which the version google gave to me was v1.9 [1], and Table 1.6 in that (Instruction listing for RVC, Quadrant 2) seems to list them all together.
Tedious it may be, but future instruction decoding bits probably need more scrutiny as Drew found another clearly wrong bit a few weeks ago [2].

Anyways, I checked against the doc and the new versions look good to me. How'd you spot this, and did you check the other masks?

I got hit by it when testing the optprobe series (c.mv was rejected as c.jr).

Skimmed the other masks quickly, but will take another look.

Quoted 4 lines

[1] -
https://riscv.org/wp-content/uploads/2015/11/riscv-compressed-spec-v1.9.pdf

C-ext is part of the unpriv spec:
https://github.com/riscv/riscv-isa-manual/releases

Quoted 13 lines

[2] - https://lore.kernel.org/linux-riscv/[email protected]/

arch/riscv/kernel/probes/simulate-insn.h __RISCV_INSN_FUNCS(fence, 0x7f, 0x0f); · line 39

Quoted 7 lines
arch/riscv/kernel/probes/simulate-insn.h +2 -1
+__RISCV_INSN_FUNCS(c_jr,	0xf07f, 0x8002);
 __RISCV_INSN_FUNCS(c_jal,	0xe003, 0x2001);
-__RISCV_INSN_FUNCS(c_jalr,	0xf007, 0x9002);
+__RISCV_INSN_FUNCS(c_jalr,	0xf07f, 0x9002);
 __RISCV_INSN_FUNCS(c_beqz,	0xe003, 0xc001);
 __RISCV_INSN_FUNCS(c_bnez,	0xe003, 0xe001);
 __RISCV_INSN_FUNCS(c_ebreak,	0xffff, 0x9002);

Worth noting that this code is gone in riscv/for-next thanks to Heiko's de-duplication:
https://lore.kernel.org/linux-riscv/[email protected]/

Yay!

Björn

#3 lore raw
Quoted 21 lines · Björn Töpel
Quoted 9 lines · Conor Dooley
Quoted 8 lines · Björn Töpel

From: Björn Töpel <[email protected]>

arch/riscv/kernel/probes/simulate-insn.h +0 -0
index cb6ff7dccb92..de8474146a9b 100644
@@ -31,9 +31,9 @@ __RISCV_INSN_FUNCS(fence,	0x7f, 0x0f);
 	} while (0)
 
Quoted 4 lines

arch/riscv/kernel/probes/simulate-insn.h __RISCV_INSN_FUNCS(fence, 0x7f, 0x0f); · line 34

Quoted 2 lines
arch/riscv/kernel/probes/simulate-insn.h +0 -1
 __RISCV_INSN_FUNCS(c_j,		0xe003, 0xa001);
-__RISCV_INSN_FUNCS(c_jr,	0xf007, 0x8002);

Hmm, I wonder where the mask originally came from!

I think it's just a simple bug -- missing that "rs2" must be zero.

Quoted 3 lines

I had a look at the compressed spec, of which the version google gave to me was v1.9 [1], and Table 1.6 in that (Instruction listing for RVC, Quadrant 2) seems to list them all together.

Quoted 5 lines
Quoted 2 lines

[1] -
https://riscv.org/wp-content/uploads/2015/11/riscv-compressed-spec-v1.9.pdf

C-ext is part of the unpriv spec:
https://github.com/riscv/riscv-isa-manual/releases

Yah, I was trying to see if there was some period piece which was misleading that would have explained the mask. I looked again & the v1.7 spec doesn't have that table, but also has no reason to suggest the current mask either. Guess it was just a mistake :)

Reviewed-by on v1 1/1
#4 lore raw
In reply to Björn Töpel

Thx for catching it.

Quoted 11 lines · Björn Töpel

From: Björn Töpel <[email protected]>

In the compressed instruction extension, c.jr, c.jalr, c.mv, and c.add is encoded the following way (each instruction is 16b):

---+-+-----------+-----------+--
100 0 rs1[4:0]!=0 00000 10 : c.jr
100 1 rs1[4:0]!=0 00000 10 : c.jalr
100 0 rd[4:0]!=0 rs2[4:0]!=0 10 : c.mv
100 1 rd[4:0]!=0 rs2[4:0]!=0 10 : c.add

Yes, I forgot the c.mv & c.add effect.

Quoted 38 lines

The following logic is used to decode c.jr and c.jalr:

insn & 0xf007 == 0x8002 => instruction is an c.jr insn & 0xf007 == 0x9002 => instruction is an c.jalr

When 0xf007 is used to mask the instruction, c.mv can be incorrectly decoded as c.jr, and c.add as c.jalr.

Correct the decoding by changing the mask from 0xf007 to 0xf07f.

Fixes c22b0bcb1dd0 ("riscv: Add kprobes supported")
---
 arch/riscv/kernel/probes/simulate-insn.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/probes/simulate-insn.h +2 -2
index cb6ff7dccb92..de8474146a9b 100644
@@ -31,9 +31,9 @@ __RISCV_INSN_FUNCS(fence,     0x7f, 0x0f);
        } while (0)
 
 __RISCV_INSN_FUNCS(c_j,                0xe003, 0xa001);
-__RISCV_INSN_FUNCS(c_jr,       0xf007, 0x8002);
+__RISCV_INSN_FUNCS(c_jr,       0xf07f, 0x8002);
 __RISCV_INSN_FUNCS(c_jal,      0xe003, 0x2001);
-__RISCV_INSN_FUNCS(c_jalr,     0xf007, 0x9002);
+__RISCV_INSN_FUNCS(c_jalr,     0xf07f, 0x9002);
 __RISCV_INSN_FUNCS(c_beqz,     0xe003, 0xc001);
 __RISCV_INSN_FUNCS(c_bnez,     0xe003, 0xe001);
 __RISCV_INSN_FUNCS(c_ebreak,   0xffff, 0x9002);

base-commit: 1b929c02afd37871d5afb9d498426f83432e71c2 --
2.37.2

Signature · 2 lines
Best Regards
 Guo Ren
#5 lore raw
In reply to Björn Töpel
This patch was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <[email protected]>:

Quoted 5 lines · On Mon, 2 Jan 2023 17:07:48 +0100 you wrote:

From: Björn Töpel <[email protected]>

In the compressed instruction extension, c.jr, c.jalr, c.mv, and c.add is encoded the following way (each instruction is 16b):

Quoted 7 lines

---+-+-----------+-----------+--
100 0 rs1[4:0]!=0 00000 10 : c.jr
100 1 rs1[4:0]!=0 00000 10 : c.jalr
100 0 rd[4:0]!=0 rs2[4:0]!=0 10 : c.mv
100 1 rd[4:0]!=0 rs2[4:0]!=0 10 : c.add

[...]

Here is the summary with links:
- riscv, kprobes: Stricter c.jr/c.jalr decoding

    https://git.kernel.org/riscv/c/b2d473a6019e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#6 lore raw
In reply to Björn Töpel
Quoted 5 lines · Björn Töpel

From: Björn Töpel <[email protected]>

In the compressed instruction extension, c.jr, c.jalr, c.mv, and c.add is encoded the following way (each instruction is 16b):

Quoted 7 lines

---+-+-----------+-----------+--
100 0 rs1[4:0]!=0 00000 10 : c.jr
100 1 rs1[4:0]!=0 00000 10 : c.jalr
100 0 rd[4:0]!=0 rs2[4:0]!=0 10 : c.mv
100 1 rd[4:0]!=0 rs2[4:0]!=0 10 : c.add

[...]

Applied, thanks!

[1/1] riscv, kprobes: Stricter c.jr/c.jalr decoding

      https://git.kernel.org/palmer/c/b2d473a6019e

Best regards,

Signature · 6 lines
Palmer Dabbelt <[email protected]>

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv