* [PATCH v1 0/2] target/riscv: Write zero into mtinst/htinst for Zicfiss instructions
@ 2026-11-17 13:26 ` Alvin Chang via qemu development
0 siblings, 0 replies; 8+ messages in thread
From: Alvin Chang via @ 2026-11-17 13:26 UTC (permalink / raw)
To: qemu-riscv, qemu-devel
Cc: alistair.francis, liwei1518, daniel.barboza, zhiwei_liu, chao.liu,
Alvin Chang
On any trap into M-mode or HS-mode, a zero, or a transformed instruction may be
written automatically into the appropriate trap instruction CSR, mtinst or
htinst. The privileged spec section "Transformed Instruction or
Pseudoinstruction for mtinst or htinst" lists the instructions that may
automatically write a value into mtinst/htinst CSR, and the section also says:
"If a synchronous trap occurs for a standard instruction for which no
transformation has been defined, the trap instruction register shall be written
with zero".
The instructions (SSAMOSWAP, SSPUSH and SSPOPCHK) defined in the Zicfiss
extension are not listed in that section of the privileged spec. Thus, the
mtinst/htinst shall be written with zero for these three instructions.
Alvin Chang (2):
target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults
target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults
target/riscv/instmap.h | 17 ++++++++++++++++-
target/riscv/tcg/cpu_helper.c | 23 +++++++++++++++++++----
2 files changed, 35 insertions(+), 5 deletions(-)
base-commit: e9fa1f66f0d47c74b6766e1b8b87ad7c2a9394d5
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v1 0/2] target/riscv: Write zero into mtinst/htinst for Zicfiss instructions
@ 2026-11-17 13:26 ` Alvin Chang via qemu development
0 siblings, 0 replies; 8+ messages in thread
From: Alvin Chang via qemu development @ 2026-11-17 13:26 UTC (permalink / raw)
To: qemu-riscv, qemu-devel
Cc: alistair.francis, liwei1518, daniel.barboza, zhiwei_liu, chao.liu,
Alvin Chang
On any trap into M-mode or HS-mode, a zero, or a transformed instruction may be
written automatically into the appropriate trap instruction CSR, mtinst or
htinst. The privileged spec section "Transformed Instruction or
Pseudoinstruction for mtinst or htinst" lists the instructions that may
automatically write a value into mtinst/htinst CSR, and the section also says:
"If a synchronous trap occurs for a standard instruction for which no
transformation has been defined, the trap instruction register shall be written
with zero".
The instructions (SSAMOSWAP, SSPUSH and SSPOPCHK) defined in the Zicfiss
extension are not listed in that section of the privileged spec. Thus, the
mtinst/htinst shall be written with zero for these three instructions.
Alvin Chang (2):
target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults
target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults
target/riscv/instmap.h | 17 ++++++++++++++++-
target/riscv/tcg/cpu_helper.c | 23 +++++++++++++++++++----
2 files changed, 35 insertions(+), 5 deletions(-)
base-commit: e9fa1f66f0d47c74b6766e1b8b87ad7c2a9394d5
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v1 1/2] target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults
2026-11-17 13:26 ` Alvin Chang via qemu development
@ 2026-11-17 13:26 ` Alvin Chang via qemu development
-1 siblings, 0 replies; 8+ messages in thread
From: Alvin Chang via @ 2026-11-17 13:26 UTC (permalink / raw)
To: qemu-riscv, qemu-devel
Cc: alistair.francis, liwei1518, daniel.barboza, zhiwei_liu, chao.liu,
Alvin Chang
The privileged spec defines transformed instructions for standard AMO
instructions which do not include SSAMOSWAP. The spec also says: "If a
synchronous trap occurs for a standard instruction for which no
transformation has been defined, the trap instruction register shall
be written with zero".
So mtinst and htinst should be zero for SSAMOSWAP faults. The previous
implementation only compares opcode which is not enough to distinguish
SSAMOSWAP from standard AMO instructions. This commit further compares
it with funct5.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4721
Fixes: f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
Signed-off-by: Alvin Chang <alvinga@andestech.com>
---
target/riscv/instmap.h | 2 ++
target/riscv/tcg/cpu_helper.c | 9 ++++++---
2 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/target/riscv/instmap.h b/target/riscv/instmap.h
index f877530576..bddcf313df 100644
--- a/target/riscv/instmap.h
+++ b/target/riscv/instmap.h
@@ -165,6 +165,8 @@ enum {
OPC_RISC_AMOMAX = OPC_RISC_ATOMIC | (0x14 << 27),
OPC_RISC_AMOMINU = OPC_RISC_ATOMIC | (0x18 << 27),
OPC_RISC_AMOMAXU = OPC_RISC_ATOMIC | (0x1C << 27),
+
+ OPC_RISC_SSAMOSWAP = OPC_RISC_ATOMIC | (0x09 << 27),
};
#define MASK_OP_SYSTEM(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12)))
diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c
index 58e98e975c..94e385261b 100644
--- a/target/riscv/tcg/cpu_helper.c
+++ b/target/riscv/tcg/cpu_helper.c
@@ -2078,9 +2078,12 @@ static target_ulong riscv_transformed_insn(CPURISCVState *env,
/* Transform 32bit (or wider) instructions */
switch (MASK_OP_MAJOR(insn)) {
case OPC_RISC_ATOMIC:
- xinsn = insn;
- access_rs1 = GET_RS1(insn);
- access_size = 1 << GET_FUNCT3(insn);
+ /* Exclude SSAMOSWAP.W and SSAMOSWAP.D */
+ if (MASK_OP_ATOMIC_NO_AQ_RL_SZ(insn) != OPC_RISC_SSAMOSWAP) {
+ xinsn = insn;
+ access_rs1 = GET_RS1(insn);
+ access_size = 1 << GET_FUNCT3(insn);
+ }
break;
case OPC_RISC_LOAD:
case OPC_RISC_FP_LOAD:
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v1 1/2] target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults
@ 2026-11-17 13:26 ` Alvin Chang via qemu development
0 siblings, 0 replies; 8+ messages in thread
From: Alvin Chang via qemu development @ 2026-11-17 13:26 UTC (permalink / raw)
To: qemu-riscv, qemu-devel
Cc: alistair.francis, liwei1518, daniel.barboza, zhiwei_liu, chao.liu,
Alvin Chang
The privileged spec defines transformed instructions for standard AMO
instructions which do not include SSAMOSWAP. The spec also says: "If a
synchronous trap occurs for a standard instruction for which no
transformation has been defined, the trap instruction register shall
be written with zero".
So mtinst and htinst should be zero for SSAMOSWAP faults. The previous
implementation only compares opcode which is not enough to distinguish
SSAMOSWAP from standard AMO instructions. This commit further compares
it with funct5.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4721
Fixes: f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
Signed-off-by: Alvin Chang <alvinga@andestech.com>
---
target/riscv/instmap.h | 2 ++
target/riscv/tcg/cpu_helper.c | 9 ++++++---
2 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/target/riscv/instmap.h b/target/riscv/instmap.h
index f877530576..bddcf313df 100644
--- a/target/riscv/instmap.h
+++ b/target/riscv/instmap.h
@@ -165,6 +165,8 @@ enum {
OPC_RISC_AMOMAX = OPC_RISC_ATOMIC | (0x14 << 27),
OPC_RISC_AMOMINU = OPC_RISC_ATOMIC | (0x18 << 27),
OPC_RISC_AMOMAXU = OPC_RISC_ATOMIC | (0x1C << 27),
+
+ OPC_RISC_SSAMOSWAP = OPC_RISC_ATOMIC | (0x09 << 27),
};
#define MASK_OP_SYSTEM(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12)))
diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c
index 58e98e975c..94e385261b 100644
--- a/target/riscv/tcg/cpu_helper.c
+++ b/target/riscv/tcg/cpu_helper.c
@@ -2078,9 +2078,12 @@ static target_ulong riscv_transformed_insn(CPURISCVState *env,
/* Transform 32bit (or wider) instructions */
switch (MASK_OP_MAJOR(insn)) {
case OPC_RISC_ATOMIC:
- xinsn = insn;
- access_rs1 = GET_RS1(insn);
- access_size = 1 << GET_FUNCT3(insn);
+ /* Exclude SSAMOSWAP.W and SSAMOSWAP.D */
+ if (MASK_OP_ATOMIC_NO_AQ_RL_SZ(insn) != OPC_RISC_SSAMOSWAP) {
+ xinsn = insn;
+ access_rs1 = GET_RS1(insn);
+ access_size = 1 << GET_FUNCT3(insn);
+ }
break;
case OPC_RISC_LOAD:
case OPC_RISC_FP_LOAD:
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v1 2/2] target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults
2026-11-17 13:26 ` Alvin Chang via qemu development
@ 2026-11-17 13:26 ` Alvin Chang via qemu development
-1 siblings, 0 replies; 8+ messages in thread
From: Alvin Chang via @ 2026-11-17 13:26 UTC (permalink / raw)
To: qemu-riscv, qemu-devel
Cc: alistair.francis, liwei1518, daniel.barboza, zhiwei_liu, chao.liu,
Alvin Chang
The privileged spec defines transformed instructions for standard
virtual-machine load/store instructions which do not include SSPUSH and
SSPOPCHK. The spec also says: "If a synchronous trap occurs for a
standard instruction for which no transformation has been defined, the
trap instruction register shall be written with zero".
So mtinst and htinst should be zero for SSPUSH and SSPOPCHK
faults. The previous implementation only compares opcode and funct3,
which is not enough to distinguish SSPUSH and SSPOPCHK from
virtual-machine load/store instructions. Now we compare opcode, funct3
and funct7 together.
Fixes: f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
Signed-off-by: Alvin Chang <alvinga@andestech.com>
---
target/riscv/instmap.h | 15 ++++++++++++++-
target/riscv/tcg/cpu_helper.c | 14 +++++++++++++-
2 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/target/riscv/instmap.h b/target/riscv/instmap.h
index bddcf313df..107e7c5a83 100644
--- a/target/riscv/instmap.h
+++ b/target/riscv/instmap.h
@@ -170,6 +170,8 @@ enum {
};
#define MASK_OP_SYSTEM(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12)))
+#define MASK_OP_SYSTEM_FUNCT3_FUNCT7(op) \
+ (MASK_OP_MAJOR(op) | (op & (0x7 << 12)) | (op & (0x7F << 25)))
enum {
OPC_RISC_ECALL = OPC_RISC_SYSTEM | (0x0 << 12),
OPC_RISC_EBREAK = OPC_RISC_SYSTEM | (0x0 << 12),
@@ -187,7 +189,18 @@ enum {
OPC_RISC_CSRRSI = OPC_RISC_SYSTEM | (0x6 << 12),
OPC_RISC_CSRRCI = OPC_RISC_SYSTEM | (0x7 << 12),
- OPC_RISC_HLVHSV = OPC_RISC_SYSTEM | (0x4 << 12),
+ /* HLV.B and HLV.BU */
+ OPC_RISC_HLVB = OPC_RISC_SYSTEM | (0x4 << 12) | (0x30 << 25),
+ /* HLV.H, HLV.HU and HLVX.HU */
+ OPC_RISC_HLVH = OPC_RISC_SYSTEM | (0x4 << 12) | (0x32 << 25),
+ /* HLV.W, HLV.WU and HLVX.WU */
+ OPC_RISC_HLVW = OPC_RISC_SYSTEM | (0x4 << 12) | (0x34 << 25),
+ OPC_RISC_HLVD = OPC_RISC_SYSTEM | (0x4 << 12) | (0x36 << 25),
+
+ OPC_RISC_HSVB = OPC_RISC_SYSTEM | (0x4 << 12) | (0x31 << 25),
+ OPC_RISC_HSVH = OPC_RISC_SYSTEM | (0x4 << 12) | (0x33 << 25),
+ OPC_RISC_HSVW = OPC_RISC_SYSTEM | (0x4 << 12) | (0x35 << 25),
+ OPC_RISC_HSVD = OPC_RISC_SYSTEM | (0x4 << 12) | (0x37 << 25),
};
#define MASK_OP_FP_LOAD(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12)))
diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c
index 94e385261b..9a1ce61a2f 100644
--- a/target/riscv/tcg/cpu_helper.c
+++ b/target/riscv/tcg/cpu_helper.c
@@ -2100,11 +2100,23 @@ static target_ulong riscv_transformed_insn(CPURISCVState *env,
access_size = 1 << GET_FUNCT3(insn);
break;
case OPC_RISC_SYSTEM:
- if (MASK_OP_SYSTEM(insn) == OPC_RISC_HLVHSV) {
+ switch (MASK_OP_SYSTEM_FUNCT3_FUNCT7(insn)) {
+ case OPC_RISC_HLVB:
+ case OPC_RISC_HLVH:
+ case OPC_RISC_HLVW:
+ case OPC_RISC_HLVD:
+ case OPC_RISC_HSVB:
+ case OPC_RISC_HSVH:
+ case OPC_RISC_HSVW:
+ case OPC_RISC_HSVD:
xinsn = insn;
access_rs1 = GET_RS1(insn);
access_size = 1 << ((GET_FUNCT7(insn) >> 1) & 0x3);
access_size = 1 << access_size;
+ break;
+ default:
+ /* Not a transformed instruction, e.g., SSPUSH and SSPOPCHK. */
+ break;
}
break;
default:
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v1 2/2] target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults
@ 2026-11-17 13:26 ` Alvin Chang via qemu development
0 siblings, 0 replies; 8+ messages in thread
From: Alvin Chang via qemu development @ 2026-11-17 13:26 UTC (permalink / raw)
To: qemu-riscv, qemu-devel
Cc: alistair.francis, liwei1518, daniel.barboza, zhiwei_liu, chao.liu,
Alvin Chang
The privileged spec defines transformed instructions for standard
virtual-machine load/store instructions which do not include SSPUSH and
SSPOPCHK. The spec also says: "If a synchronous trap occurs for a
standard instruction for which no transformation has been defined, the
trap instruction register shall be written with zero".
So mtinst and htinst should be zero for SSPUSH and SSPOPCHK
faults. The previous implementation only compares opcode and funct3,
which is not enough to distinguish SSPUSH and SSPOPCHK from
virtual-machine load/store instructions. Now we compare opcode, funct3
and funct7 together.
Fixes: f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
Signed-off-by: Alvin Chang <alvinga@andestech.com>
---
target/riscv/instmap.h | 15 ++++++++++++++-
target/riscv/tcg/cpu_helper.c | 14 +++++++++++++-
2 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/target/riscv/instmap.h b/target/riscv/instmap.h
index bddcf313df..107e7c5a83 100644
--- a/target/riscv/instmap.h
+++ b/target/riscv/instmap.h
@@ -170,6 +170,8 @@ enum {
};
#define MASK_OP_SYSTEM(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12)))
+#define MASK_OP_SYSTEM_FUNCT3_FUNCT7(op) \
+ (MASK_OP_MAJOR(op) | (op & (0x7 << 12)) | (op & (0x7F << 25)))
enum {
OPC_RISC_ECALL = OPC_RISC_SYSTEM | (0x0 << 12),
OPC_RISC_EBREAK = OPC_RISC_SYSTEM | (0x0 << 12),
@@ -187,7 +189,18 @@ enum {
OPC_RISC_CSRRSI = OPC_RISC_SYSTEM | (0x6 << 12),
OPC_RISC_CSRRCI = OPC_RISC_SYSTEM | (0x7 << 12),
- OPC_RISC_HLVHSV = OPC_RISC_SYSTEM | (0x4 << 12),
+ /* HLV.B and HLV.BU */
+ OPC_RISC_HLVB = OPC_RISC_SYSTEM | (0x4 << 12) | (0x30 << 25),
+ /* HLV.H, HLV.HU and HLVX.HU */
+ OPC_RISC_HLVH = OPC_RISC_SYSTEM | (0x4 << 12) | (0x32 << 25),
+ /* HLV.W, HLV.WU and HLVX.WU */
+ OPC_RISC_HLVW = OPC_RISC_SYSTEM | (0x4 << 12) | (0x34 << 25),
+ OPC_RISC_HLVD = OPC_RISC_SYSTEM | (0x4 << 12) | (0x36 << 25),
+
+ OPC_RISC_HSVB = OPC_RISC_SYSTEM | (0x4 << 12) | (0x31 << 25),
+ OPC_RISC_HSVH = OPC_RISC_SYSTEM | (0x4 << 12) | (0x33 << 25),
+ OPC_RISC_HSVW = OPC_RISC_SYSTEM | (0x4 << 12) | (0x35 << 25),
+ OPC_RISC_HSVD = OPC_RISC_SYSTEM | (0x4 << 12) | (0x37 << 25),
};
#define MASK_OP_FP_LOAD(op) (MASK_OP_MAJOR(op) | (op & (0x7 << 12)))
diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c
index 94e385261b..9a1ce61a2f 100644
--- a/target/riscv/tcg/cpu_helper.c
+++ b/target/riscv/tcg/cpu_helper.c
@@ -2100,11 +2100,23 @@ static target_ulong riscv_transformed_insn(CPURISCVState *env,
access_size = 1 << GET_FUNCT3(insn);
break;
case OPC_RISC_SYSTEM:
- if (MASK_OP_SYSTEM(insn) == OPC_RISC_HLVHSV) {
+ switch (MASK_OP_SYSTEM_FUNCT3_FUNCT7(insn)) {
+ case OPC_RISC_HLVB:
+ case OPC_RISC_HLVH:
+ case OPC_RISC_HLVW:
+ case OPC_RISC_HLVD:
+ case OPC_RISC_HSVB:
+ case OPC_RISC_HSVH:
+ case OPC_RISC_HSVW:
+ case OPC_RISC_HSVD:
xinsn = insn;
access_rs1 = GET_RS1(insn);
access_size = 1 << ((GET_FUNCT7(insn) >> 1) & 0x3);
access_size = 1 << access_size;
+ break;
+ default:
+ /* Not a transformed instruction, e.g., SSPUSH and SSPOPCHK. */
+ break;
}
break;
default:
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v1 1/2] target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults
2026-11-17 13:26 ` Alvin Chang via qemu development
(?)
@ 2026-11-19 23:53 ` alistair23
-1 siblings, 0 replies; 8+ messages in thread
From: alistair23 @ 2026-11-19 23:53 UTC (permalink / raw)
To: Alvin Chang via
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] target/riscv: Missing Fixes: trailer for bug fix
--
- [LOW] target/riscv: Missing Fixes: trailer for bug fix. The commit
fixes a bug introduced in f06bfe3dc38c ("target/riscv: implement
zicfiss instructions"). Add a Fixes: trailer for commit hygiene.
--
Sashiko AI review · https://risc6-threadripper.alistair23.me/#/patchset/20261117132638.1452474-1-alvinga@andestech.com?part=1
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v1 2/2] target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults
2026-11-17 13:26 ` Alvin Chang via qemu development
(?)
@ 2026-11-20 0:02 ` alistair23
-1 siblings, 0 replies; 8+ messages in thread
From: alistair23 @ 2026-11-20 0:02 UTC (permalink / raw)
To: Alvin Chang via
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] target/riscv: Double-shift error in access_size calculation
- [High] target/riscv: incorrect access size calculation for transformed
instructions
--
- [HIGH] In target/riscv/tcg/cpu_helper.c (riscv_transformed_insn), the
access_size calculation contains a double-shift error. The first shift
calculates the byte width (e.g., 4 for a word), but the subsequent
access_size = 1 << access_size; turns that width into an incorrect
power of two (e.g., 16 for a word). This causes incorrect memory
access sizes for HLV/HSV instructions, leading to guest memory
corruption or incorrect state. Fix by removing the second shift.
--
Sashiko AI review · https://risc6-threadripper.alistair23.me/#/patchset/20261117132638.1452474-1-alvinga@andestech.com?part=2
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-20 0:02 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 13:26 [PATCH v1 0/2] target/riscv: Write zero into mtinst/htinst for Zicfiss instructions Alvin Chang via
2026-11-17 13:26 ` Alvin Chang via qemu development
2026-11-17 13:26 ` [PATCH v1 1/2] target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults Alvin Chang via
2026-11-17 13:26 ` Alvin Chang via qemu development
2026-11-19 23:53 ` alistair23
2026-11-17 13:26 ` [PATCH v1 2/2] target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults Alvin Chang via
2026-11-17 13:26 ` Alvin Chang via qemu development
2026-11-20 0:02 ` alistair23
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.