target/riscv: make Zicfiss CBO checks independent of TLB state
PATCH v2 1/1 target/riscv: make Zicfiss CBO checks independent of TLB state
From: Abhigyan Kumar <[email protected]>
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
---
v2:
- Rebase on alistair/riscv-to-apply.next (4ac269ea5c).
- Declare fault_addr only for CONFIG_USER_ONLY, fixing the -Werror=unused-variable failure in softmmu builds.
--- include/hw/core/cpu.h | 5 ++++ target/riscv/tcg/cpu_helper.c | 49 +++++++++++++++++++++++++---------- target/riscv/tcg/op_helper.c | 19 +++++++++++++- 3 files changed, 59 insertions(+), 14 deletions(-)
include/hw/core/cpu.h +5 -0
@@ -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 +18 -1
@@ -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,
@@ -231,8 +234,12 @@ static void check_zicbom_access(CPURISCVState *env, {uint16_t cbomlen =riscv_cpu_cfg (env)->cbom_blocksize;int mmu_idx =riscv_env_mmu_index (env, false); -uint64_t fault_addr = address;void *phost; +#ifdef CONFIG_USER_ONLY +uint64_t fault_addr = address; +#else +CPUTLBEntryFull *full; +#endif int ret;/* Mask off low-bits to align-down to the cache-block. */
@@ -250,6 +257,7 @@ 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_MMIO ) {
@@ -259,6 +267,15 @@ static void check_zicbom_access(CPURISCVState *env,/* 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