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")
Signed-off-by: Abhigyan Kumar <[email protected]>
---
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(-)
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index 5b7c330047..71666a5497 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -277,6 +277,11 @@ struct CPUTLBEntryFull {
uint8_t shareability;
bool guarded;
} arm;
+
+ struct {
+ /* Zicfiss shadow stack page. */
+ bool sstack;
+ } riscv;
} extra;
};
diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c
index 58e98e975c..d1b902fea2 100644
--- a/target/riscv/tcg/cpu_helper.c
+++ b/target/riscv/tcg/cpu_helper.c
@@ -1096,7 +1096,7 @@ static int get_physical_address(CPURISCVState *env,
hwaddr *physical,
hwaddr *fault_pte_addr,
int access_type, int mmu_idx,
bool first_stage, bool two_stage,
- bool is_debug, bool is_probe)
+ bool is_debug, bool *is_sstack_page)
{
/*
* NOTE: the env->pc value visible here will not be
@@ -1254,7 +1254,7 @@ static int get_physical_address(CPURISCVState *env,
hwaddr *physical,
int vbase_ret = get_physical_address(env, &vbase, &vbase_prot,
base, NULL, MMU_DATA_LOAD,
MMUIdx_U, false, true,
- is_debug, false);
+ is_debug, NULL);
if (vbase_ret != TRANSLATE_SUCCESS) {
if (fault_pte_addr) {
@@ -1411,6 +1411,9 @@ static int get_physical_address(CPURISCVState *env,
hwaddr *physical,
return TRANSLATE_FAIL;
}
+ /* If gets stale after a goto restart. */
+ sstack_page = false;
+
target_ulong rwx = pte & (PTE_R | PTE_W | PTE_X);
/* Check for reserved combinations of RWX flags. */
switch (rwx) {
@@ -1421,10 +1424,10 @@ static int get_physical_address(CPURISCVState *env,
hwaddr *physical,
if (cpu_get_bcfien(env) && first_stage) {
sstack_page = true;
/*
- * if ss index, read and write allowed. else if not a probe
- * then only read allowed
+ * If ss index, read and write allowed, else allow only reads.
+ * Page flagged in TLB for CBOs to be able to tell them apart.
*/
- rwx = is_sstack_idx ? (PTE_R | PTE_W) : (is_probe ? 0 : PTE_R);
+ rwx = is_sstack_idx ? (PTE_R | PTE_W) : PTE_R;
break;
}
return TRANSLATE_FAIL;
@@ -1539,7 +1542,7 @@ static int get_physical_address(CPURISCVState *env,
hwaddr *physical,
int gpa_ret = get_physical_address(env, &gpa_paddr, &gpa_prot,
pte_gpa, NULL, MMU_DATA_STORE,
MMUIdx_U, false, true,
- is_debug, false);
+ is_debug, NULL);
if (gpa_ret != TRANSLATE_SUCCESS) {
if (fault_pte_addr) {
*fault_pte_addr = pte_gpa >> 2;
@@ -1612,6 +1615,10 @@ static int get_physical_address(CPURISCVState *env,
hwaddr *physical,
}
*ret_prot = prot;
+ if (is_sstack_page) {
+ *is_sstack_page = sstack_page;
+ }
+
return TRANSLATE_SUCCESS;
}
@@ -1669,13 +1676,13 @@ bool riscv_cpu_translate_for_debug(CPUState *cs, vaddr
addr,
int mmu_idx = riscv_env_mmu_index(&cpu->env, false);
if (get_physical_address(env, &phys_addr, &prot, addr, NULL, 0, mmu_idx,
- true, env->virt_enabled, true, false)) {
+ true, env->virt_enabled, true, NULL)) {
return false;
}
if (env->virt_enabled) {
if (get_physical_address(env, &phys_addr, &prot, phys_addr, NULL,
- 0, MMUIdx_U, false, true, true, false)) {
+ 0, MMUIdx_U, false, true, true, NULL)) {
return false;
}
}
@@ -1779,6 +1786,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int
size,
bool first_stage_error = true;
bool two_stage_lookup = mmuidx_2stage(mmu_idx);
bool two_stage_indirect_error = false;
+ bool sstack_page = false;
int ret = TRANSLATE_FAIL;
privilege_mode_t mode = mmuidx_priv(mmu_idx);
/* default TLB page size */
@@ -1794,7 +1802,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int
size,
/* Two stage lookup */
ret = get_physical_address(env, &pa, &prot, address,
&env->guest_phys_fault_addr, access_type,
- mmu_idx, true, true, false, probe);
+ mmu_idx, true, true, false, &sstack_page);
/*
* A G-stage exception may be triggered during two state lookup.
* And the env->guest_phys_fault_addr has already been set in
@@ -1816,7 +1824,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int
size,
ret = get_physical_address(env, &pa, &prot2, im_address, NULL,
access_type, MMUIdx_U, false, true,
- false, probe);
+ false, NULL);
qemu_log_mask(CPU_LOG_MMU,
"%s 2nd-stage address=%" VADDR_PRIx
@@ -1854,7 +1862,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int
size,
/* Single stage lookup */
ret = get_physical_address(env, &pa, &prot, address, NULL,
access_type, mmu_idx, true, false, false,
- probe);
+ &sstack_page);
qemu_log_mask(CPU_LOG_MMU,
"%s address=%" VADDR_PRIx " ret %d physical "
@@ -1880,8 +1888,23 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int
size,
}
if (ret == TRANSLATE_SUCCESS) {
- tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1),
- prot, mmu_idx, tlb_size);
+ /*
+ * tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1),
+ * prot, mmu_idx, tlb_size);
+ */
+ CPUTLBEntryFull full = {
+ .phys_addr = pa & ~(tlb_size - 1),
+ .attrs = MEMTXATTRS_UNSPECIFIED,
+ .prot = prot,
+ .lg_page_size = ctz64(tlb_size),
+ /*
+ * Allows CBO.* to differentiate between a shadow-ss from a usual,
+ * readable one.
+ */
+ .extra.riscv.sstack = sstack_page,
+ };
+
+ tlb_set_page_full(cs, mmu_idx, address & ~(tlb_size - 1), &full);
return true;
} else if (probe) {
return false;
diff --git a/target/riscv/tcg/op_helper.c b/target/riscv/tcg/op_helper.c
index f9c79fe6b9..a289cb1a84 100644
--- a/target/riscv/tcg/op_helper.c
+++ b/target/riscv/tcg/op_helper.c
@@ -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
--
2.56.1