Skip to contentSkip to conversation

target/riscv: make Zicfiss CBO checks independent of TLB state

PATCH In review Abhigyan Kumar 2 participants 2 messages Nov 14 – Nov 17 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: make Zicfiss CBO checks independent of TLB state

The Zicfiss extension requires CBO.CLEAN/FLUSH/INVAL on a shadow stack page to raise a store/AMO access fault. Citing section 15.1.2.3 ("Shadow Stack Memory Protection") of the RISC-V spec:

"Memory mapped as an SS page cannot be written to by instructions other than SSAMOSWAP.W/D, SSPUSH, and C.SSPUSH. Attempts will raise a store/AMO access-fault exception. Access to a SS page using cache-block operation (CBO.*) instructions is not permitted. Such accesses will raise a store/AMO access-fault exception. Implicit accesses, including instruction fetches to an SS page, are not permitted. Such accesses will raise an access-fault exception appropriate to the access type. However, the shadow stack is readable by all instructions that only load from memory."

Currently, CBO.* instructions quietly return when operated on a shadow stack page. check_zicomb_access probes pages for read with non-fault set. Then, get_physical_address reports a shadow stack page as unreadable when called from a probing tlb_fill. But it only works when probe reaches tlb_fill. After a usual load, probe hits TLB and reports the page as readable. Hence, causing CBOs to quietly return.

This commit introduces CPUTLBEntryFull.extra.riscv.sstack as a marker for shadow stack pages inside TLB entries. When riscv.sstack is set, check_zicbom_access falls through it to probe_write.

With this commit, the now-redundant special case in get_physical_address for probe is removed. is_probe in a call to get_physical_address is replaced with is_sstack_page. As a side effect non-fault probes of shadow stack pages no longer see them as unreadable, matching what a usual load does.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4789

Fixes 669b4867495c ("target/riscv: mmu changes for zicfiss shadow stack protection")
---
 include/hw/core/cpu.h         |  5 ++++
 target/riscv/tcg/cpu_helper.c | 49 +++++++++++++++++++++++++----------
 target/riscv/tcg/op_helper.c  | 16 ++++++++++++
 3 files changed, 57 insertions(+), 13 deletions(-)
include/hw/core/cpu.h +5 -0
index 5b7c330047..71666a5497 100644
@@ -277,6 +277,11 @@ struct CPUTLBEntryFull {
             uint8_t shareability;
             bool guarded;
         } arm;
+
+        struct {
+            /* Zicfiss shadow stack page. */
+            bool sstack;
+        } riscv;
     } extra;
 };
 
target/riscv/tcg/cpu_helper.c +36 -13

127 lines not on this page · raw message

target/riscv/tcg/op_helper.c +16 -0
index f9c79fe6b9..a289cb1a84 100644
@@ -224,6 +224,9 @@ void HELPER(cbo_zero)(CPURISCVState *env, target_ulong address)
  * section 2.5.2 of the CMO specification) for Zicbom, raising
  * either store page-fault (non-virtualized) or store guest-page
  * fault (virtualized).
+ *
+ * Zicfiss: CBOs on a shadow stack page always raise a store/AMO access fault
+ * even though loads are allowed.
  */
 static void check_zicbom_access(CPURISCVState *env,
                                 target_ulong address,
@@ -233,6 +236,9 @@ static void check_zicbom_access(CPURISCVState *env,
     int mmu_idx = riscv_env_mmu_index(env, false);
     uint16_t cbomlen = cpu->cfg.cbom_blocksize;
     void *phost;
+#ifndef CONFIG_USER_ONLY
+    CPUTLBEntryFull *full;
+#endif
     int ret;
 
     /* Mask off low-bits to align-down to the cache-block. */
@@ -250,12 +256,22 @@ static void check_zicbom_access(CPURISCVState *env,
      * addresses, whether a cache-block management instruction is
      * permitted to access the cache block is UNSPECIFIED."
      */
+#ifdef CONFIG_USER_ONLY
     ret = probe_access_flags(env, address, cbomlen, MMU_DATA_LOAD,
                              mmu_idx, true, &phost, ra);
     if (ret != TLB_INVALID_MASK) {
         /* Success: readable */
         return;
     }
+#else
+    ret = probe_access_full(env, address, cbomlen, MMU_DATA_LOAD,
+            mmu_idx, true, &phost, &full, ra);
+    /* A shadow stack page is readable but faults after fall through. */
+    if (ret != TLB_INVALID_MASK && !full->extra.riscv.sstack) {
+        /* Success: readable */
+        return;
+    }
+#endif
 
     /*
      * Since not readable, must be writable. On failure, store
Signature · 1 line
2.56.1
Tested-by on v1 1/1
#1 lore raw
Quoted 10 lines · Abhigyan Kumar

This commit introduces CPUTLBEntryFull.extra.riscv.sstack as a marker for shadow stack pages inside TLB entries. When riscv.sstack is set, check_zicbom_access falls through it to probe_write.

With this commit, the now-redundant special case in get_physical_address for probe is removed. is_probe in a call to get_physical_address is replaced with is_sstack_page. As a side effect non-fault probes of shadow stack pages no longer see them as unreadable, matching what a usual load does.

Quoted 3 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4789

Fixes 669b4867495c ("target/riscv: mmu changes for zicfiss shadow stack protection")

With this patch on master (97c88c61d45d), the reproducer from #4789 passes and exits 0.

Tested on Debian 13 x86_64 (riscv64-softmmu and riscv64-linux-user build).

Thanks,
Karl