Skip to contentSkip to conversation

target/riscv: fix access permission checks for CSR_SSP

PATCH Superseded Deepak Gupta 2 participants 10 messages Feb 18, 2025 – Mar 6, 2025 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 2/2 target/riscv: fixes a bug against `ssamoswap` behavior in M-mode

In reply to Deepak Gupta

Commit f06bfe3dc38c ("target/riscv: implement zicfiss instructions") adds ssamoswap instruction. ssamoswap takes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), then ssamoswap must result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.

Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However, ssamoswap can still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior for ssamoswap.

Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
---
 target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
target/riscv/insn_trans/trans_rvzicfiss.c.inc +11 -2
index e3ebc4977c..ec016cd70f 100644
@@ -15,6 +15,13 @@
  * You should have received a copy of the GNU General Public License along with
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+
+ #define REQUIRE_ZICFISS(ctx) do {       \
+    if (!ctx->cfg_ptr->ext_zicfiss) {    \
+        return false;                    \
+    }                                    \
+} while (0)
+
 static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a)
 {
     if (!ctx->bcfi_enabled) {
@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a)
 static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_64BIT(ctx);
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
Signature · 1 line
2.34.1
#0 lore raw

PATCH 1/2 target/riscv: fix access permission checks for CSR_SSP

Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced CSR_SSP but it mis-interpreted the spec on access to CSR_SSP in M-mode. Gated to CSR_SSP is not gated via xSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer" of zicfiss specification. Thanks to Adam Zabrocki for bringing this to attention.

Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controls

for zicfiss"

---
 target/riscv/csr.c | 5 +++++
 1 file changed, 5 insertions(+)
target/riscv/csr.c +5 -0
index afb7544f07..75c661d2a1 100644
@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /* If ext implemented, M-mode always have access to SSP CSR */
+    if (env->priv == PRV_M) {
+        return RISCV_EXCP_NONE;
+    }
+
     /* if bcfi not active for current env, access to csr is illegal */
     if (!cpu_get_bcfien(env)) {
 #if !defined(CONFIG_USER_ONLY)
Signature · 1 line
2.34.1
Reviewed-by on v1 1/2
#6 lore raw
Quoted 5 lines · Deepak Gupta

Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced CSR_SSP but it mis-interpreted the spec on access to CSR_SSP in M-mode. Gated to CSR_SSP is not gated via xSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer"

Do you mean "22.2.1. Shadow Stack Pointer (ssp) CSR access contr" in the priv spec?

Quoted 2 lines

of zicfiss specification. Thanks to Adam Zabrocki for bringing this to attention.

The thanks should probably be below the line

Quoted 6 lines
Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controls

for zicfiss"

The actual change looks good:

Alistair

Quoted 24 lines
---
 target/riscv/csr.c | 5 +++++
 1 file changed, 5 insertions(+)
target/riscv/csr.c +5 -1
index afb7544f07..75c661d2a1 100644
@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /* If ext implemented, M-mode always have access to SSP CSR */
+    if (env->priv == PRV_M) {
+        return RISCV_EXCP_NONE;
+    }
+
     /* if bcfi not active for current env, access to csr is illegal */
     if (!cpu_get_bcfien(env)) {
 #if !defined(CONFIG_USER_ONLY)
--

2.34.1

#2 lore raw

Re: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode

In reply to Deepak Gupta
Quoted 8 lines · Deepak Gupta

Commit f06bfe3dc38c ("target/riscv: implement zicfiss instructions") adds ssamoswap instruction. ssamoswap takes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), then ssamoswap must result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.

Quoted 4 lines

Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However, ssamoswap can still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior for ssamoswap.

Section "22.2.3. Shadow Stack Memory Protection " of the latest priv spec [1] seems to say: "When the effective privilege mode is M, any memory access by an SSAMOSWAP.W/D
instruction will result in a store/AMO access-fault exception."

1: https://github.com/riscv/riscv-isa-manual/releases/tag/riscv-isa-release-9cfaf37-2025-03-06

Alistair

Quoted 51 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
---
 target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
target/riscv/insn_trans/trans_rvzicfiss.c.inc +11 -3
index e3ebc4977c..ec016cd70f 100644
@@ -15,6 +15,13 @@
  * You should have received a copy of the GNU General Public License along with
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+
+ #define REQUIRE_ZICFISS(ctx) do {       \
+    if (!ctx->cfg_ptr->ext_zicfiss) {    \
+        return false;                    \
+    }                                    \
+} while (0)
+
 static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a)
 {
     if (!ctx->bcfi_enabled) {
@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a)
 static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_64BIT(ctx);
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
--

2.34.1

#7 lore raw
In reply to Alistair Francis
Quoted 9 lines · Alistair Francis
Quoted 5 lines · Deepak Gupta

Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced CSR_SSP but it mis-interpreted the spec on access to CSR_SSP in M-mode. Gated to CSR_SSP is not gated via xSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer"

Do you mean "22.2.1. Shadow Stack Pointer (ssp) CSR access contr" in the priv spec?

No I meant 2.2.4 of zicfiss specification. Section 22.2.1 of priv spec says same.

Quoted 5 lines
Quoted 3 lines

of zicfiss specification. Thanks to Adam Zabrocki for bringing this to attention.

The thanks should probably be below the line

Sure

Quoted 38 lines
Quoted 7 lines
Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controls

for zicfiss"

The actual change looks good:

Alistair

Quoted 24 lines
---
 target/riscv/csr.c | 5 +++++
 1 file changed, 5 insertions(+)
target/riscv/csr.c +5 -1
index afb7544f07..75c661d2a1 100644
@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /* If ext implemented, M-mode always have access to SSP CSR */
+    if (env->priv == PRV_M) {
+        return RISCV_EXCP_NONE;
+    }
+
     /* if bcfi not active for current env, access to csr is illegal */
     if (!cpu_get_bcfien(env)) {
 #if !defined(CONFIG_USER_ONLY)
--

2.34.1

#3 lore raw

Re: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode

In reply to Alistair Francis
Quoted 14 lines · Alistair Francis
Quoted 8 lines · Deepak Gupta

Commit f06bfe3dc38c ("target/riscv: implement zicfiss instructions") adds ssamoswap instruction. ssamoswap takes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), then ssamoswap must result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.

Quoted 4 lines

Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However, ssamoswap can still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior for ssamoswap.

Quoted 4 lines

Section "22.2.3. Shadow Stack Memory Protection " of the latest priv spec [1] seems to say: "When the effective privilege mode is M, any memory access by an SSAMOSWAP.W/D
instruction will result in a store/AMO access-fault exception."

Hmm I didn't look at priv spec. Let me fix this one.

Quoted 56 lines

1: https://github.com/riscv/riscv-isa-manual/releases/tag/riscv-isa-release-9cfaf37-2025-03-06

Alistair

Quoted 51 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
---
 target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
target/riscv/insn_trans/trans_rvzicfiss.c.inc +11 -3
index e3ebc4977c..ec016cd70f 100644
@@ -15,6 +15,13 @@
  * You should have received a copy of the GNU General Public License along with
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+
+ #define REQUIRE_ZICFISS(ctx) do {       \
+    if (!ctx->cfg_ptr->ext_zicfiss) {    \
+        return false;                    \
+    }                                    \
+} while (0)
+
 static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a)
 {
     if (!ctx->bcfi_enabled) {
@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a)
 static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_64BIT(ctx);
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
--

2.34.1

#8 lore raw
In reply to Deepak Gupta
Quoted 9 lines · Deepak Gupta
Quoted 7 lines · Alistair Francis
Quoted 5 lines · Deepak Gupta

Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced CSR_SSP but it mis-interpreted the spec on access to CSR_SSP in M-mode. Gated to CSR_SSP is not gated via xSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer"

Quoted 5 lines
Quoted 2 lines

Do you mean "22.2.1. Shadow Stack Pointer (ssp) CSR access contr" in the priv spec?

No I meant 2.2.4 of zicfiss specification. Section 22.2.1 of priv spec says same.

I meant that it's now just in the priv spec, the zicfiss spec is no longer maintained so we should just reference the priv spec

Alistair

Quoted 47 lines
Quoted 6 lines
Quoted 4 lines

of zicfiss specification. Thanks to Adam Zabrocki for bringing this to attention.

The thanks should probably be below the line

Sure

Quoted 38 lines
Quoted 7 lines
Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controls

for zicfiss"

The actual change looks good:

Alistair

Quoted 24 lines
---
 target/riscv/csr.c | 5 +++++
 1 file changed, 5 insertions(+)
target/riscv/csr.c +5 -1
index afb7544f07..75c661d2a1 100644
@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /* If ext implemented, M-mode always have access to SSP CSR */
+    if (env->priv == PRV_M) {
+        return RISCV_EXCP_NONE;
+    }
+
     /* if bcfi not active for current env, access to csr is illegal */
     if (!cpu_get_bcfien(env)) {
 #if !defined(CONFIG_USER_ONLY)
--

2.34.1

#9 lore raw
Quoted 10 lines · Alistair Francis
Quoted 9 lines · Deepak Gupta
Quoted 7 lines · Alistair Francis
Quoted 5 lines · Deepak Gupta

Commit:8205bc1 ("target/riscv: introduce ssp and enabling controls for zicfiss") introduced CSR_SSP but it mis-interpreted the spec on access to CSR_SSP in M-mode. Gated to CSR_SSP is not gated via xSSE. But rather rules clearly specified in section "2.2.4. Shadow Stack Pointer"

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Do you mean "22.2.1. Shadow Stack Pointer (ssp) CSR access contr" in the priv spec?

No I meant 2.2.4 of zicfiss specification. Section 22.2.1 of priv spec says same.

I meant that it's now just in the priv spec, the zicfiss spec is no longer maintained so we should just reference the priv spec

Got it.

Quoted 50 lines

Alistair

Quoted 47 lines
Quoted 6 lines
Quoted 4 lines

of zicfiss specification. Thanks to Adam Zabrocki for bringing this to attention.

The thanks should probably be below the line

Sure

Quoted 38 lines
Quoted 7 lines
Fixes 8205bc127a83 ("target/riscv: introduce ssp and enabling controls

for zicfiss"

The actual change looks good:

Alistair

Quoted 24 lines
---
 target/riscv/csr.c | 5 +++++
 1 file changed, 5 insertions(+)
target/riscv/csr.c +5 -1
index afb7544f07..75c661d2a1 100644
@@ -191,6 +191,11 @@ static RISCVException cfi_ss(CPURISCVState *env, int csrno)
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    /* If ext implemented, M-mode always have access to SSP CSR */
+    if (env->priv == PRV_M) {
+        return RISCV_EXCP_NONE;
+    }
+
     /* if bcfi not active for current env, access to csr is illegal */
     if (!cpu_get_bcfien(env)) {
 #if !defined(CONFIG_USER_ONLY)
--

2.34.1

#4 lore raw

Re: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode

In reply to Deepak Gupta
Quoted 16 lines · Deepak Gupta
Quoted 14 lines · Alistair Francis
Quoted 12 lines · Deepak Gupta

Commit f06bfe3dc38c ("target/riscv: implement zicfiss instructions") adds ssamoswap instruction. ssamoswap takes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), then ssamoswap must result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.

Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However, ssamoswap can still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior for ssamoswap.

Quoted 6 lines
Quoted 4 lines

Section "22.2.3. Shadow Stack Memory Protection " of the latest priv spec [1] seems to say: "When the effective privilege mode is M, any memory access by an SSAMOSWAP.W/D
instruction will result in a store/AMO access-fault exception."

Hmm I didn't look at priv spec. Let me fix this one.

I hope the two don't conflict, that will be a nightmare

Alistair

Quoted 57 lines
Quoted 57 lines

1: https://github.com/riscv/riscv-isa-manual/releases/tag/riscv-isa-release-9cfaf37-2025-03-06

Alistair

Quoted 51 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
---
 target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
target/riscv/insn_trans/trans_rvzicfiss.c.inc +11 -3
index e3ebc4977c..ec016cd70f 100644
@@ -15,6 +15,13 @@
  * You should have received a copy of the GNU General Public License along with
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+
+ #define REQUIRE_ZICFISS(ctx) do {       \
+    if (!ctx->cfg_ptr->ext_zicfiss) {    \
+        return false;                    \
+    }                                    \
+} while (0)
+
 static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a)
 {
     if (!ctx->bcfi_enabled) {
@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a)
 static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_64BIT(ctx);
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
--

2.34.1

#5 lore raw

Re: [PATCH 2/2] target/riscv: fixes a bug against `ssamoswap` behavior in M-mode

Quoted 17 lines · Alistair Francis
Quoted 16 lines · Deepak Gupta
Quoted 14 lines · Alistair Francis
Quoted 12 lines · Deepak Gupta

Commit f06bfe3dc38c ("target/riscv: implement zicfiss instructions") adds ssamoswap instruction. ssamoswap takes the code-point from existing reserved encoding (and not a zimop like other shadow stack instructions). If shadow stack is not enabled (via xenvcfg.SSE), then ssamoswap must result in an illegal instruction exception. However there is a slightly modified behavior for M-mode.

Shadow stack are not available in M-mode and all shadow stack instructions in M-mode exhibit zimop behavior. However, ssamoswap can still succeed if MPRV=1 and MPP is non-zero (see section 2.7 of zicfiss specification). This patch corrects that behavior for ssamoswap.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Section "22.2.3. Shadow Stack Memory Protection " of the latest priv spec [1] seems to say: "When the effective privilege mode is M, any memory access by an SSAMOSWAP.W/D
instruction will result in a store/AMO access-fault exception."

Hmm I didn't look at priv spec. Let me fix this one.

I hope the two don't conflict, that will be a nightmare

No they don't conflict. Last "else" block below basically means that it should be store/AMO access fault because there is no shadow stack page.

"""
if privilege_mode != M && menvcfg.SSE == 0
raise illegal-instruction exception
if S-mode not implemented
raise illegal-instruction exception
else if privilege_mode == U && senvcfg.SSE == 0
raise illegal-instruction exception
else if privilege_mode == VS && henvcfg.SSE == 0
raise virtual instruction exception
else if privilege_mode == VU && senvcfg.SSE == 0
raise virtual instruction exception
else

	temp[31:0] = mem[X(rs1)]
	X(rd) = SignExtend(temp[31:0])
	mem[X(rs1)] = X(rs2)[31:0]

endif
"""

Quoted 60 lines

Alistair

Quoted 57 lines
Quoted 57 lines

1: https://github.com/riscv/riscv-isa-manual/releases/tag/riscv-isa-release-9cfaf37-2025-03-06

Alistair

Quoted 51 lines
Fixes f06bfe3dc38c ("target/riscv: implement zicfiss instructions")
---
 target/riscv/insn_trans/trans_rvzicfiss.c.inc | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
target/riscv/insn_trans/trans_rvzicfiss.c.inc +11 -3
index e3ebc4977c..ec016cd70f 100644
@@ -15,6 +15,13 @@
  * You should have received a copy of the GNU General Public License along with
  * this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+
+ #define REQUIRE_ZICFISS(ctx) do {       \
+    if (!ctx->cfg_ptr->ext_zicfiss) {    \
+        return false;                    \
+    }                                    \
+} while (0)
+
 static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a)
 {
     if (!ctx->bcfi_enabled) {
@@ -77,7 +84,8 @@ static bool trans_ssrdp(DisasContext *ctx, arg_ssrdp *a)
 static bool trans_ssamoswap_w(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
@@ -97,7 +105,8 @@ static bool trans_ssamoswap_d(DisasContext *ctx, arg_amoswap_w *a)
 {
     REQUIRE_64BIT(ctx);
     REQUIRE_A_OR_ZAAMO(ctx);
-    if (!ctx->bcfi_enabled) {
+    REQUIRE_ZICFISS(ctx);
+    if ((ctx->priv != PRV_M) && !ctx->bcfi_enabled) {
         return false;
     }
 
--

2.34.1