From: Frank Chang <[email protected]> This helper returns the current effective privilege mode.
Signed-off-by: Frank Chang <[email protected]> --- target/riscv/cpu.h | 37 +++++++++++++++++++++++++++++++++++++ target/riscv/cpu_helper.c | 15 +++++---------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 8899bf7667a..f59052fe7dc 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -799,6 +799,43 @@ static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env) } #endif +/* + * Returns the current effective privilege mode. + * + * @env: CPURISCVState + * @priv: The returned effective privilege mode. + * @virt: The returned effective virtualization mode. + * + * Returns true if the effective privilege mode is modified. + */ +static inline QEMU_ALWAYS_INLINE +bool riscv_cpu_eff_priv(CPURISCVState *env, int *priv, bool *virt) +{ + int mode = env->priv; + bool virt_enabled = false; + bool mode_modified = false; + +#ifndef CONFIG_USER_ONLY + if (mode == PRV_M && get_field(env->mstatus, MSTATUS_MPRV)) { + mode = get_field(env->mstatus, MSTATUS_MPP); + virt_enabled = get_field(env->mstatus, MSTATUS_MPV) && (mode != PRV_M); + mode_modified = true; + } else { + virt_enabled = env->virt_enabled; + } +#endif + + if (priv) { + *priv = mode; + } + + if (virt) { + *virt = virt_enabled; + } + + return mode_modified; +} + static inline bool riscv_cpu_allow_16bit_insn(const RISCVCPUConfig *cfg, target_long priv_ver, uint32_t misa_ext) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index dd6c861a90e..da6e2d8fe3a 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -45,19 +45,14 @@ int riscv_env_mmu_index(CPURISCVState *env, bool ifetch) #else bool virt = env->virt_enabled; int mode = env->priv; + bool mode_modified = false; /* All priv -> mmu_idx mapping are here */ if (!ifetch) { - uint64_t status = env->mstatus; - - if (mode == PRV_M && get_field(status, MSTATUS_MPRV)) { - mode = get_field(env->mstatus, MSTATUS_MPP); - virt = get_field(env->mstatus, MSTATUS_MPV) && - (mode != PRV_M); - if (virt) { - status = env->vsstatus; - } - } + mode_modified = riscv_cpu_eff_priv(env, &mode, &virt); + uint64_t status = (mode_modified && virt) ? env->vsstatus : + env->mstatus; + if (mode == PRV_S && get_field(status, MSTATUS_SUM)) { mode = MMUIdx_S_SUM; } -- 2.43.0
