target/riscv: Write zero into mtinst/htinst for Zicfiss instructions
cover target/riscv: Write zero into mtinst/htinst for Zicfiss instructions
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
Signature · 1 line
2.43.0
PATCH 1/2 target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults
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
--- target/riscv/instmap.h | 2 ++ target/riscv/tcg/cpu_helper.c | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-)
target/riscv/instmap.h +2 -0
@@ -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)))
target/riscv/tcg/cpu_helper.c +6 -3
@@ -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 :
Signature · 1 line
2.43.0
Re: [PATCH v1 1/2] target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults
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.
Signature · 1 line
Sashiko AI review · https://risc6-threadripper.alistair23.me/#/patchset/[email protected]?part=1
Re: [PATCH v1 1/2] target/riscv: Write zero into mtinst/htinst for SSAMOSWAP faults
In reply to Alvin Chang via qemu developmentQuoted 11 lines · 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.
Quoted 4 lines
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4721
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")Signed-off-by Alvin Chang <[email protected]>---
Quoted 37 lines
target/riscv/instmap.h | 2 ++ target/riscv/tcg/cpu_helper.c | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-)target/riscv/instmap.h +2
-0@@ -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)))target/riscv/tcg/cpu_helper.c +6
-3@@ -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 :
PATCH 2/2 target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults
In reply to Alvin Chang via qemu developmentThe 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.
--- target/riscv/instmap.h | 15 ++++++++++++++- target/riscv/tcg/cpu_helper.c | 14 +++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-)
target/riscv/instmap.h +14 -1
@@ -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)))
target/riscv/tcg/cpu_helper.c +13 -1
@@ -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 :
Signature · 1 line
2.43.0
Re: [PATCH v1 2/2] target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults
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.
Signature · 1 line
Sashiko AI review · https://risc6-threadripper.alistair23.me/#/patchset/[email protected]?part=2
Re: [PATCH v1 2/2] target/riscv: Write zero into mtinst/htinst for SSPUSH/SSPOPCHK faults
In reply to Alvin Chang via qemu developmentQuoted 12 lines · 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.
Quoted 3 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")Signed-off-by Alvin Chang <[email protected]>---
Quoted 66 lines
target/riscv/instmap.h | 15 ++++++++++++++- target/riscv/tcg/cpu_helper.c | 14 +++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-)target/riscv/instmap.h +14
-1@@ -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)))target/riscv/tcg/cpu_helper.c +13
-1@@ -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 :