@samitouri / QOSamiQemu / commits / 40540c8a92

target/riscv: Fix pointer masking for virtual-machine load/store insns

The effective privilege of explicit memory accesses made by virtual-machine load/store instructions (HLV.* and HSV.*) is controlled by hstatus.SPVP. mstatus.MPRV does not affect these virtual-machine load/store instructions. Signed-off-by: Frank Chang <frank.chang@sifive.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Message-ID: <20260421093715.2995067-5-frank.chang@sifive.com> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

Frank Chang committed Apr 21, 2026 at 17:37 UTC 40540c8a929f165420e775b41800262578e6712d
1 file changed +15 -8
target/riscv/cpu_helper.c
+15 -8
@@ -217,16 +217,23 @@ RISCVPmPmm riscv_pm_get_pmm(CPURISCVState *env)
217 RISCVPmPmm riscv_pm_get_virt_pmm(CPURISCVState *env)
218 {
219 #ifndef CONFIG_USER_ONLY
220 - int priv_mode = cpu_address_mode(env);
220 + int priv_mode;
221 +
222 + if (!riscv_cpu_cfg(env)->ext_ssnpm ||
223 + get_field(env->mstatus, MSTATUS_MXR) ||
224 + get_field(env->vsstatus, MSTATUS_MXR)) {
225 + return PMM_FIELD_DISABLED;
226 + }
227 +
228 + priv_mode = get_field(env->hstatus, HSTATUS_SPVP);
229
222 - if (priv_mode == PRV_U) {
223 - return get_field(env->hstatus, HSTATUS_HUPMM);
230 + if (priv_mode == PRV_S) {
231 + /* Effective privilege mode: VS */
232 + return get_field(env->henvcfg, HENVCFG_PMM);
233 } else {
225 - if (get_field(env->hstatus, HSTATUS_SPVP)) {
226 - return get_field(env->henvcfg, HENVCFG_PMM);
227 - } else {
228 - return get_field(env->senvcfg, SENVCFG_PMM);
229 - }
234 + /* Effective privilege mode: VU */
235 + return (env->priv == PRV_U) ? get_field(env->hstatus, HSTATUS_HUPMM) :
236 + get_field(env->senvcfg, SENVCFG_PMM);
237 }
238 #else
239 return PMM_FIELD_DISABLED;