Fix the Hypervisor access functions
cover Fix the Hypervisor access functions
Richard pointed out that the Hypervisor access functions don't work correctly, see:
https://www.mail-archive.com/[email protected]/msg751540.html.
This seris fixes them up by adding a new MMU index for the virtualised state.
Alistair Francis (5):
target/riscv: Add a virtualised MMU Mode
target/riscv: Set the virtualised MMU mode when doing hyp accesses target/riscv: Remove the HS_TWO_STAGE flag target/riscv: Remove the hyp load and store functions target/riscv: Split the Hypervisor execute load helpers
target/riscv/cpu-param.h | 2 +- target/riscv/cpu.h | 7 +- target/riscv/cpu_bits.h | 1 - target/riscv/helper.h | 6 +- target/riscv/cpu_helper.c | 67 ++++++------- target/riscv/op_helper.c | 90 ++--------------- target/riscv/insn_trans/trans_rvh.c.inc | 127 +++++++----------------- 7 files changed, 82 insertions(+), 218 deletions(-)
Signature · 1 line
2.28.0
PATCH 1/5 target/riscv: Add a virtualised MMU Mode
Add a new MMU mode that includes the current virt mode.
--- target/riscv/cpu-param.h | 2 +- target/riscv/cpu.h | 4 +++- target/riscv/cpu_helper.c | 6 +++++- 3 files changed, 9 insertions(+), 3 deletions(-)
target/riscv/cpu-param.h +1 -1
@@ -18,6 +18,6 @@# define TARGET_VIRT_ADDR_SPACE_BITS 32/* sv32 */ #endif #define TARGET_PAGE_BITS 12/* 4 KiB Pages */ -#define NB_MMU_MODES 4 +#define NB_MMU_MODES 8#endif
target/riscv/cpu.h +3 -1
@@ -365,7 +365,9 @@ void QEMU_NORETURN riscv_raise_exception(CPURISCVState *env,target_ulong riscv_cpu_get_fflags (CPURISCVState *env);void riscv_cpu_set_fflags (CPURISCVState *env,target_ulong ); -#define TB_FLAGS_MMU_MASK 3 +#define TB_FLAGS_MMU_MASK 7 +#define TB_FLAGS_PRIV_MMU_MASK 3 +#define TB_FLAGS_PRIV_HYP_ACCESS_MASK (1 << 2)#define TB_FLAGS_MSTATUS_FS MSTATUS_FStypedef CPURISCVState CPUArchState ;
target/riscv/cpu_helper.c +5 -1
@@ -30,6 +30,10 @@ int riscv_cpu_mmu_index(CPURISCVState *env, bool ifetch)#ifdef CONFIG_USER_ONLY return 0 ;#else +if (riscv_cpu_virt_enabled (env)) { +return env->priv |TB_FLAGS_PRIV_HYP_ACCESS_MASK ; + } +return env->priv;#endif }
@@ -336,7 +340,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,* (riscv_cpu_do_interrupt) is correct */ MemTxResult res;MemTxAttrs attrs =MEMTXATTRS_UNSPECIFIED ; -int mode = mmu_idx; +int mode = mmu_idx &0x3 ;bool use_background = false;/*
Signature · 1 line
2.28.0
Re: [PATCH v1 1/5] target/riscv: Add a virtualised MMU Mode
target/riscv/cpu-param.h line 21
Quoted 7 lines · Alistair Francis
+++
b/target/riscv/cpu-param.htarget/riscv/cpu-param.h +1
-1@@ -18,6 +18,6 @@# define TARGET_VIRT_ADDR_SPACE_BITS 32/* sv32 */ #endif #define TARGET_PAGE_BITS 12/* 4 KiB Pages */ -#define NB_MMU_MODES 4 +#define NB_MMU_MODES 8
Is there really a PRV_M + virt enabled state?
Quoted 1 line
target/riscv/cpu.h +1
-0+#define TB_FLAGS_PRIV_MMU_MASK 3
...
target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState *env, hwaddr *physical, · line 343
Quoted 2 lines
target/riscv/cpu_helper.c +1
-1-int mode = mmu_idx; +int mode = mmu_idx &0x3 ;
Use that MASK here?
r~
Re: [PATCH v1 1/5] target/riscv: Add a virtualised MMU Mode
Quoted 11 lines · Richard Henderson
target/riscv/cpu-param.h line 21
Quoted 7 lines · Alistair Francis
+++
b/target/riscv/cpu-param.htarget/riscv/cpu-param.h +1
-1@@ -18,6 +18,6 @@# define TARGET_VIRT_ADDR_SPACE_BITS 32/* sv32 */ #endif #define TARGET_PAGE_BITS 12/* 4 KiB Pages */ -#define NB_MMU_MODES 4 +#define NB_MMU_MODES 8Is there really a
PRV_M+ virt enabled state?
No, there isn't.
Quoted 7 lines
Quoted 2 lines
target/riscv/cpu.h +1
-0+#define TB_FLAGS_PRIV_MMU_MASK 3...
target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState *env, hwaddr *physical, · line 343
Quoted 2 lines
target/riscv/cpu_helper.c +1
-1-int mode = mmu_idx; +int mode = mmu_idx &0x3 ;Use that MASK here?
Good idea.
Alistair
Quoted 3 lines
r~
PATCH 2/5 target/riscv: Set the virtualised MMU mode when doing hyp accesses
In reply to Alistair FrancisWhen performing the hypervisor load/store operations set the MMU mode to indicate that we are virtualised.
--- target/riscv/op_helper.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-)
target/riscv/op_helper.c +17 -13
@@ -238,30 +238,31 @@ target_ulong helper_hyp_load(CPURISCVState *env, target_ulong address, (env->priv == PRV_U && !riscv_cpu_virt_enabled(env) && get_field(env->hstatus, HSTATUS_HU))) {target_ulong pte; +int mmu_idx =cpu_mmu_index (env, false) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ;riscv_cpu_set_two_stage_lookup (env, true);switch (memop) {case MO_SB : - pte =cpu_ldsb_data_ra (env, address,GETPC ()); + pte =cpu_ldsb_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;case MO_UB : - pte =cpu_ldub_data_ra (env, address,GETPC ()); + pte =cpu_ldub_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;case MO_TESW : - pte =cpu_ldsw_data_ra (env, address,GETPC ()); + pte =cpu_ldsw_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;case MO_TEUW : - pte =cpu_lduw_data_ra (env, address,GETPC ()); + pte =cpu_lduw_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;case MO_TESL : - pte =cpu_ldl_data_ra (env, address,GETPC ()); + pte =cpu_ldl_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;case MO_TEUL : - pte =cpu_ldl_data_ra (env, address,GETPC ()); + pte =cpu_ldl_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;case MO_TEQ : - pte =cpu_ldq_data_ra (env, address,GETPC ()); + pte =cpu_ldq_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;default :g_assert_not_reached ();
@@ -287,23 +288,25 @@ void helper_hyp_store(CPURISCVState *env, target_ulong address, (env->priv == PRV_S && !riscv_cpu_virt_enabled(env)) || (env->priv == PRV_U && !riscv_cpu_virt_enabled(env) && get_field(env->hstatus, HSTATUS_HU))) { +int mmu_idx =cpu_mmu_index (env, false) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ; +riscv_cpu_set_two_stage_lookup (env, true);switch (memop) {case MO_SB :case MO_UB : -cpu_stb_data_ra (env, address, val,GETPC ()); +cpu_stb_mmuidx_ra (env, address, val, mmu_idx,GETPC ());break ;case MO_TESW :case MO_TEUW : -cpu_stw_data_ra (env, address, val,GETPC ()); +cpu_stw_mmuidx_ra (env, address, val, mmu_idx,GETPC ());break ;case MO_TESL :case MO_TEUL : -cpu_stl_data_ra (env, address, val,GETPC ()); +cpu_stl_mmuidx_ra (env, address, val, mmu_idx,GETPC ());break ;case MO_TEQ : -cpu_stq_data_ra (env, address, val,GETPC ()); +cpu_stq_mmuidx_ra (env, address, val, mmu_idx,GETPC ());break ;default :g_assert_not_reached ();
@@ -329,15 +332,16 @@ target_ulong helper_hyp_x_load(CPURISCVState *env, target_ulong address, (env->priv == PRV_U && !riscv_cpu_virt_enabled(env) && get_field(env->hstatus, HSTATUS_HU))) {target_ulong pte; +int mmu_idx =cpu_mmu_index (env, false) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ;riscv_cpu_set_two_stage_lookup (env, true);switch (memop) {case MO_TEUW : - pte =cpu_lduw_data_ra (env, address,GETPC ()); + pte =cpu_lduw_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;case MO_TEUL : - pte =cpu_ldl_data_ra (env, address,GETPC ()); + pte =cpu_ldl_mmuidx_ra (env, address, mmu_idx,GETPC ());break ;default :g_assert_not_reached ();
Signature · 1 line
2.28.0
PATCH 3/5 target/riscv: Remove the HS_TWO_STAGE flag
In reply to Alistair FrancisThe HS_TWO_STAGE flag is no longer required as the MMU index contains the information if we are performing a two stage access.
--- target/riscv/cpu.h | 3 +- target/riscv/cpu_bits.h | 1 - target/riscv/cpu_helper.c | 61 ++++++++++++++++----------------------- target/riscv/op_helper.c | 12 -------- 4 files changed, 26 insertions(+), 51 deletions(-)
target/riscv/cpu.h +1 -2
@@ -325,8 +325,7 @@ bool riscv_cpu_virt_enabled(CPURISCVState *env);void riscv_cpu_set_virt_enabled (CPURISCVState *env,bool enable);bool riscv_cpu_force_hs_excep_enabled (CPURISCVState *env);void riscv_cpu_set_force_hs_excep (CPURISCVState *env,bool enable); -bool riscv_cpu_two_stage_lookup (CPURISCVState *env); -void riscv_cpu_set_two_stage_lookup (CPURISCVState *env,bool enable); +bool riscv_cpu_two_stage_lookup (int mmu_idx);int riscv_cpu_mmu_index (CPURISCVState *env,bool ifetch);hwaddr riscv_cpu_get_phys_page_debug (CPUState *cpu,vaddr addr); void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
target/riscv/cpu_bits.h +0 -1
@@ -480,7 +480,6 @@* page table fault. */ #define FORCE_HS_EXCEP 2 -#define HS_TWO_STAGE 4/* RV32 satp CSR field masks */ #define SATP32_MODE 0x80000000
target/riscv/cpu_helper.c +25 -36
135 lines not on this page · raw message
target/riscv/op_helper.c +0 -12
@@ -240,8 +240,6 @@ target_ulong helper_hyp_load(CPURISCVState *env, target_ulong address,target_ulong pte;int mmu_idx =cpu_mmu_index (env, false) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ; -riscv_cpu_set_two_stage_lookup (env, true); -switch (memop) {case MO_SB : pte =cpu_ldsb_mmuidx_ra (env, address, mmu_idx,GETPC ());
@@ -268,8 +266,6 @@ target_ulong helper_hyp_load(CPURISCVState *env, target_ulong address,g_assert_not_reached (); } -riscv_cpu_set_two_stage_lookup (env, false); -return pte; }
@@ -290,8 +286,6 @@ void helper_hyp_store(CPURISCVState *env, target_ulong address, get_field(env->hstatus, HSTATUS_HU))) {int mmu_idx =cpu_mmu_index (env, false) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ; -riscv_cpu_set_two_stage_lookup (env, true); -switch (memop) {case MO_SB :case MO_UB :
@@ -312,8 +306,6 @@ void helper_hyp_store(CPURISCVState *env, target_ulong address,g_assert_not_reached (); } -riscv_cpu_set_two_stage_lookup (env, false); -return ; }
@@ -334,8 +326,6 @@ target_ulong helper_hyp_x_load(CPURISCVState *env, target_ulong address,target_ulong pte;int mmu_idx =cpu_mmu_index (env, false) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ; -riscv_cpu_set_two_stage_lookup (env, true); -switch (memop) {case MO_TEUW : pte =cpu_lduw_mmuidx_ra (env, address, mmu_idx,GETPC ());
@@ -347,8 +337,6 @@ target_ulong helper_hyp_x_load(CPURISCVState *env, target_ulong address,g_assert_not_reached (); } -riscv_cpu_set_two_stage_lookup (env, false); -return pte; }
Signature · 1 line
2.28.0
PATCH 4/5 target/riscv: Remove the hyp load and store functions
In reply to Alistair FrancisRemove the special Virtulisation load and store functions and just use the standard tcg tcg_gen_qemu_ld_tl() and tcg_gen_qemu_st_tl() functions instead.
As part of this change we ensure we still run an access check to make sure we can perform the operations.
--- target/riscv/helper.h | 3 +- target/riscv/op_helper.c | 72 +-------------- target/riscv/insn_trans/trans_rvh.c.inc | 111 +++++++----------------- 3 files changed, 35 insertions(+), 151 deletions(-)
target/riscv/helper.h +1 -2
@@ -81,8 +81,7 @@ DEF_HELPER_1(tlb_flush, void, env)#ifndef CONFIG_USER_ONLY DEF_HELPER_1(hyp_tlb_flush, void, env) DEF_HELPER_1(hyp_gvma_tlb_flush, void, env) -DEF_HELPER_4(hyp_load, tl, env, tl, tl, tl) -DEF_HELPER_5(hyp_store, void, env, tl, tl, tl, tl) +DEF_HELPER_1(hyp_access_check, void, env) DEF_HELPER_4(hyp_x_load, tl, env, tl, tl, tl)#endif
target/riscv/op_helper.c +1 -71
83 lines not on this page · raw message
target/riscv/insn_trans/trans_rvh.c.inc +33 -78
253 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH 5/5 target/riscv: Split the Hypervisor execute load helpers
In reply to Alistair FrancisSplit the hypervisor execute load functions into two seperate functions. This avoids us having to pass the memop to the C helper functions.
--- target/riscv/helper.h | 3 +- target/riscv/op_helper.c | 38 ++++++++++++++----------- target/riscv/insn_trans/trans_rvh.c.inc | 16 ++--------- 3 files changed, 26 insertions(+), 31 deletions(-)
target/riscv/helper.h +2 -1
@@ -82,7 +82,8 @@ DEF_HELPER_1(tlb_flush, void, env) DEF_HELPER_1(hyp_tlb_flush, void, env) DEF_HELPER_1(hyp_gvma_tlb_flush, void, env) DEF_HELPER_1(hyp_access_check, void, env) -DEF_HELPER_4(hyp_x_load, tl, env, tl, tl, tl) +DEF_HELPER_2(hyp_hlvx_hu, tl, env, tl) +DEF_HELPER_2(hyp_hlvx_wu, tl, env, tl)#endif /* Vector functions */
target/riscv/op_helper.c +22 -16
@@ -246,28 +246,34 @@ void helper_hyp_access_check(CPURISCVState *env) } } -target_ulong helper_hyp_x_load (CPURISCVState *env,target_ulong address, -target_ulong attrs,target_ulong memop) +target_ulong helper_hyp_hlvx_hu (CPURISCVState *env,target_ulong address) {if (env->priv ==PRV_M || (env->priv ==PRV_S && !riscv_cpu_virt_enabled (env)) || (env->priv ==PRV_U && !riscv_cpu_virt_enabled (env) &&get_field (env->hstatus,HSTATUS_HU ))) { -target_ulong pte; -int mmu_idx =cpu_mmu_index (env, false) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ; - -switch (memop) { -case MO_TEUW : - pte =cpu_lduw_mmuidx_ra (env, address, mmu_idx,GETPC ()); -break ; -case MO_TEUL : - pte =cpu_ldl_mmuidx_ra (env, address, mmu_idx,GETPC ()); -break ; -default : -g_assert_not_reached (); - } +int mmu_idx =cpu_mmu_index (env, true) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ; + +return cpu_lduw_mmuidx_ra (env, address, mmu_idx,GETPC ()); + } + +if (riscv_cpu_virt_enabled (env)) { +riscv_raise_exception (env,RISCV_EXCP_VIRT_INSTRUCTION_FAULT ,GETPC ()); + }else { +riscv_raise_exception (env,RISCV_EXCP_ILLEGAL_INST ,GETPC ()); + } +return 0 ; +} + +target_ulong helper_hyp_hlvx_wu (CPURISCVState *env,target_ulong address) +{ +if (env->priv ==PRV_M || + (env->priv ==PRV_S && !riscv_cpu_virt_enabled (env)) || + (env->priv ==PRV_U && !riscv_cpu_virt_enabled (env) && +get_field (env->hstatus,HSTATUS_HU ))) { +int mmu_idx =cpu_mmu_index (env, true) |TB_FLAGS_PRIV_HYP_ACCESS_MASK ; -return pte; +return cpu_ldl_mmuidx_ra (env, address, mmu_idx,GETPC ()); }if (riscv_cpu_virt_enabled (env)) {
target/riscv/insn_trans/trans_rvh.c.inc +2 -14
@@ -265,20 +265,14 @@ static bool trans_hlvx_hu(DisasContext *ctx, arg_hlvx_hu *a) #ifndef CONFIG_USER_ONLY TCGv t0 = tcg_temp_new(); TCGv t1 = tcg_temp_new(); - TCGv mem_idx = tcg_temp_new(); - TCGv memop = tcg_temp_new(); gen_get_gpr(t0, a->rs1); - tcg_gen_movi_tl(mem_idx, ctx->mem_idx); - tcg_gen_movi_tl(memop, MO_TEUW); - gen_helper_hyp_x_load(t1, cpu_env, t0, mem_idx, memop); + gen_helper_hyp_hlvx_hu(t1, cpu_env, t0); gen_set_gpr(a->rd, t1); tcg_temp_free(t0); tcg_temp_free(t1); - tcg_temp_free(mem_idx); - tcg_temp_free(memop); return true; #else return false;
@@ -291,20 +285,14 @@ static bool trans_hlvx_wu(DisasContext *ctx, arg_hlvx_wu *a) #ifndef CONFIG_USER_ONLY TCGv t0 = tcg_temp_new(); TCGv t1 = tcg_temp_new(); - TCGv mem_idx = tcg_temp_new(); - TCGv memop = tcg_temp_new(); gen_get_gpr(t0, a->rs1); - tcg_gen_movi_tl(mem_idx, ctx->mem_idx); - tcg_gen_movi_tl(memop, MO_TEUL); - gen_helper_hyp_x_load(t1, cpu_env, t0, mem_idx, memop); + gen_helper_hyp_hlvx_wu(t1, cpu_env, t0); gen_set_gpr(a->rd, t1); tcg_temp_free(t0); tcg_temp_free(t1); - tcg_temp_free(mem_idx); - tcg_temp_free(memop); return true; #else return false;
Signature · 1 line
2.28.0