target/arm: Misc cleanups surrounding TBI
cover target/arm: Misc cleanups surrounding TBI
We have a bug at present wherein we do not supply the memory tag to the memory system, so that on fault FAR_ELx does not contain the correct value.
For system mode, we already handle ignoring TBI in get_phys_addr_lpae, as long as we don't actually drop the tag during translation. For user mode, we don't have that option, so for now we must simply accept that we'll get the wrong value in the siginfo_t.
In the process of looking at all that I found:
* Exception return was not applying TBI in copying ELR_ELx to PC,
- Extracting the current mmu_idx can be improved,
- Replicating the TBI bits can allow bit 55 to be used unconditionally, eliminating a test.
* DC_ZVA was not handling TBI (now only for user-mode)
- The helper need not have been in op_helper.c,
- The helper could have better tcg markup.
* TBI still applies when translation is disabled, and we weren't raising AddressSpace for bad physical addresses.
* SVE hasn't been updated to handle TBI. I have done nothing about this for now. For the moment, system mode will work properly, while user-only will only work without tags. I'll have to touch the same places to add MTE support, so it'll get done shortly.
r~
Richard Henderson (8):
target/arm: Replicate TBI/TBID bits for single range regimes target/arm: Optimize cpu_mmu_index
target/arm: Apply TBI to ESR_ELx in helper_exception_return target/arm: Move helper_dc_zva to helper-a64.c target/arm: Use DEF_HELPER_FLAGS for helper_dc_zva target/arm: Clean address for DC ZVA
target/arm: Check addresses for disabled regimes target/arm: Disable clean_data_tbi for system mode
target/arm/cpu.h | 23 ++++---- target/arm/helper-a64.h | 1 + target/arm/helper.h | 1 - target/arm/helper-a64.c | 114 ++++++++++++++++++++++++++++++++++++- target/arm/helper.c | 44 +++++++++++--- target/arm/op_helper.c | 93 ------------------------------ target/arm/translate-a64.c | 13 ++++- 7 files changed, 175 insertions(+), 114 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/8 target/arm: Replicate TBI/TBID bits for single range regimes
Replicate the single TBI bit from TCR_EL2 and TCR_EL3 so that we can unconditionally use pointer bit 55 to index into our composite TBI1:TBI0 field.
--- target/arm/helper.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
target/arm/helper.c +4 -2
@@ -10297,7 +10297,8 @@ static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx) }else if (mmu_idx == ARMMMUIdx_Stage2) {return 0 ;/* VTCR_EL2 */ }else { -return extract32 (tcr,20 ,1 ); +/* Replicate the single TBI bit so we always have 2 bits. */ +return extract32 (tcr,20 ,1 ) *3 ; } }
@@ -10308,7 +10309,8 @@ static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx) }else if (mmu_idx == ARMMMUIdx_Stage2) {return 0 ;/* VTCR_EL2 */ }else { -return extract32 (tcr,29 ,1 ); +/* Replicate the single TBID bit so we always have 2 bits. */ +return extract32 (tcr,29 ,1 ) *3 ; } }
Signature · 1 line
2.20.1
Re: [PATCH 1/8] target/arm: Replicate TBI/TBID bits for single range regimes
Quoted 9 lines · Richard Henderson
Replicate the single TBI bit from
TCR_EL2andTCR_EL3so that we can unconditionally use pointer bit 55 to index into our composite TBI1:TBI0 field.Signed-off-by Richard Henderson <[email protected]>--- target/arm/helper.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
thanks
-- PMM
PATCH 2/8 target/arm: Optimize cpu_mmu_index
In reply to Richard HendersonWe now cache the core mmu_idx in env->hflags. Rather than recompute from scratch, extract the field. All of the uses of cpu_mmu_index within target/arm are within helpers where env->hflags is stable.
--- target/arm/cpu.h | 23 +++++++++++++---------- target/arm/helper.c | 5 ----- 2 files changed, 13 insertions(+), 15 deletions(-)
target/arm/cpu.h +13 -10
@@ -2939,16 +2939,6 @@ typedef enum ARMMMUIdxBit {#define MMU_USER_IDX 0 -/** -* cpu_mmu_index: -* @env: The cpu environment -* @ifetch: True for code access, false for data access. -* -* Return the core mmu index for the current translation regime. -* This function is used by generic TCG code paths. -*/ -int cpu_mmu_index (CPUARMState *env,bool ifetch); -/* Indexes used when registering address spaces with cpu_address_space_init */ typedef enum ARMASIdx { ARMASIdx_NS = 0,
@@ -3228,6 +3218,19 @@ FIELD(TBFLAG_A64, BTYPE, 10, 2) /* Not cached. */ FIELD(TBFLAG_A64, TBID, 12, 2)FIELD (TBFLAG_A64 ,UNPRIV ,14 ,1 ) +/** +* cpu_mmu_index: +* @env: The cpu environment +* @ifetch: True for code access, false for data access. +* +* Return the core mmu index for the current translation regime. +* This function is used by generic TCG code paths. + */ +static inline int cpu_mmu_index (CPUARMState *env,bool ifetch) +{ +return FIELD_EX32 (env->hflags,TBFLAG_ANY ,MMUIDX ); +} +static inline bool bswap_code (bool sctlr_b) {#ifdef CONFIG_USER_ONLY
target/arm/helper.c +0 -5
@@ -12109,11 +12109,6 @@ ARMMMUIdx arm_mmu_idx(CPUARMState *env)return arm_mmu_idx_el (env,arm_current_el (env)); } -int cpu_mmu_index (CPUARMState *env,bool ifetch) -{ -return arm_to_core_mmu_idx (arm_mmu_idx (env)); -} -#ifndef CONFIG_USER_ONLY ARMMMUIdx arm_stage1_mmu_idx (CPUARMState *env) {
Signature · 1 line
2.20.1
PATCH 3/8 target/arm: Apply TBI to ESR_ELx in helper_exception_return
We missed this case within AArch64.ExceptionReturn.
--- target/arm/helper-a64.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
target/arm/helper-a64.c +22 -1
@@ -1031,6 +1031,8 @@ void HELPER(exception_return)(CPUARMState *env, uint64_t new_pc)"AArch32 EL%d PC 0x%" PRIx32"\n" , cur_el, new_el, env->regs[15]); }else { +int tbii; + env->aarch64 =1 ; spsr &=aarch64_pstate_valid_mask (&env_archcpu (env)->isar);pstate_write (env, spsr);
@@ -1038,8 +1040,27 @@ void HELPER(exception_return)(CPUARMState *env, uint64_t new_pc) env->pstate &= ~PSTATE_SS ; }aarch64_restore_sp (env, new_el); - env->pc = new_pc;helper_rebuild_hflags_a64 (env, new_el); + +/* +* Apply TBI to the exception return address. We had to delay this +* until after we selected the new EL, so that we could select the +* correct TBI+TBID bits. This is made easier by waiting until after +* the hflags rebuild, since we can pull the composite TBII field +* from there. +*/ + tbii =FIELD_EX32 (env->hflags,TBFLAG_A64 ,TBII ); +if ((tbii >>extract64 (new_pc,55 ,1 )) &1 ) { +/* TBI is enabled. */ +int core_mmu_idx =cpu_mmu_index (env, false); +if (regime_has_2_ranges (core_mmu_idx |ARM_MMU_IDX_A )) { + new_pc =sextract64 (new_pc,0 ,56 ); + }else { + new_pc =extract64 (new_pc,0 ,56 ); + } + } + env->pc = new_pc; +qemu_log_mask (CPU_LOG_INT ,"Exception return from AArch64 EL%d to " "AArch64 EL%d PC 0x%" PRIx64"\n" , cur_el, new_el, env->pc);
Signature · 1 line
2.20.1
Re: [PATCH 3/8] target/arm: Apply TBI to ESR_ELx in helper_exception_return
Quoted 30 lines · Richard Henderson
We missed this case within AArch64.ExceptionReturn.
Signed-off-by Richard Henderson <[email protected]>--- target/arm/helper-a64.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)target/arm/helper-a64.c +5
-1@@ -1031,6 +1031,8 @@ void HELPER(exception_return)(CPUARMState *env, uint64_t new_pc)"AArch32 EL%d PC 0x%" PRIx32"\n" , cur_el, new_el, env->regs[15]); }else { +int tbii; + env->aarch64 =1 ; spsr &=aarch64_pstate_valid_mask (&env_archcpu (env)->isar);pstate_write (env, spsr);@@ -1038,8 +1040,27 @@ void HELPER(exception_return)(CPUARMState *env, uint64_t new_pc) env->pstate &= ~PSTATE_SS ; }aarch64_restore_sp (env, new_el); - env->pc = new_pc;helper_rebuild_hflags_a64 (env, new_el); + +/* +* Apply TBI to the exception return address. We had to delay this
target/arm/helper-a64.c void HELPER(exception_return)(CPUARMState *env, uint64_t new_pc) · line 1056
Quoted 10 lines
target/arm/helper-a64.c +10
-0+* until after we selected the new EL, so that we could select the +* correct TBI+TBID bits. This is made easier by waiting until after +* the hflags rebuild, since we can pull the composite TBII field +* from there. +*/ + tbii =FIELD_EX32 (env->hflags,TBFLAG_A64 ,TBII ); +if ((tbii >>extract64 (new_pc,55 ,1 )) &1 ) { +/* TBI is enabled. */ +int core_mmu_idx =cpu_mmu_index (env, false); +if (regime_has_2_ranges (core_mmu_idx |ARM_MMU_IDX_A )) {
We have core_to_arm_mmu_idx() so you don't need to open-code this. Or just call arm_mmu_idx(env) to get the ARMMMUIdx directly.
target/arm/helper-a64.c void HELPER(exception_return)(CPUARMState *env, uint64_t new_pc) · line 1066
Quoted 10 lines
target/arm/helper-a64.c +7
-0+ new_pc =sextract64 (new_pc,0 ,56 ); + }else { + new_pc =extract64 (new_pc,0 ,56 ); + } + } + env->pc = new_pc; +qemu_log_mask (CPU_LOG_INT ,"Exception return from AArch64 EL%d to " "AArch64 EL%d PC 0x%" PRIx64"\n" , cur_el, new_el, env->pc);
thanks
-- PMM
PATCH 4/8 target/arm: Move helper_dc_zva to helper-a64.c
In reply to Richard HendersonThis is an aarch64-only function. Move it out of the shared file. This patch is code movement only.
--- target/arm/helper-a64.h | 1 + target/arm/helper.h | 1 - target/arm/helper-a64.c | 91 ++++++++++++++++++++++++++++++++++++++++ target/arm/op_helper.c | 93 ----------------------------------------- 4 files changed, 92 insertions(+), 94 deletions(-)
target/arm/helper-a64.h +1 -0
@@ -90,6 +90,7 @@ DEF_HELPER_2(advsimd_f16touinth, i32, f16, ptr) DEF_HELPER_2(sqrt_f16, f16, f16, ptr) DEF_HELPER_2(exception_return, void, env, i64) +DEF_HELPER_2(dc_zva, void, env, i64) DEF_HELPER_FLAGS_3(pacia, TCG_CALL_NO_WG, i64, env, i64, i64)DEF_HELPER_FLAGS_3 (pacib,TCG_CALL_NO_WG , i64, env, i64, i64)
target/arm/helper.h +0 -1
@@ -559,7 +559,6 @@ DEF_HELPER_FLAGS_3(crypto_sm4ekey, TCG_CALL_NO_RWG, void, ptr, ptr, ptr) DEF_HELPER_FLAGS_3(crc32, TCG_CALL_NO_RWG_SE, i32, i32, i32, i32) DEF_HELPER_FLAGS_3(crc32c, TCG_CALL_NO_RWG_SE, i32, i32, i32, i32) -DEF_HELPER_2(dc_zva, void, env, i64)DEF_HELPER_FLAGS_5 (gvec_qrdmlah_s16,TCG_CALL_NO_RWG , void, ptr, ptr, ptr, ptr, i32)
target/arm/helper-a64.c +91 -0
101 lines not on this page · raw message
target/arm/op_helper.c +0 -93
102 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH 4/8] target/arm: Move helper_dc_zva to helper-a64.c
Quoted 5 lines · Richard Henderson
This is an aarch64-only function. Move it out of the shared file. This patch is code movement only.
Signed-off-by Richard Henderson <[email protected]>
thanks
-- PMM
PATCH 5/8 target/arm: Use DEF_HELPER_FLAGS for helper_dc_zva
In reply to Richard HendersonThe function does not write registers, and only reads them by implication via the exception path.
--- target/arm/helper-a64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/helper-a64.h +1 -1
@@ -90,7 +90,7 @@ DEF_HELPER_2(advsimd_f16touinth, i32, f16, ptr) DEF_HELPER_2(sqrt_f16, f16, f16, ptr) DEF_HELPER_2(exception_return, void, env, i64) -DEF_HELPER_2(dc_zva, void, env, i64) +DEF_HELPER_FLAGS_2(dc_zva, TCG_CALL_NO_WG, void, env, i64) DEF_HELPER_FLAGS_3(pacia, TCG_CALL_NO_WG, i64, env, i64, i64)DEF_HELPER_FLAGS_3 (pacib,TCG_CALL_NO_WG , i64, env, i64, i64)
Signature · 1 line
2.20.1
Re: [PATCH 5/8] target/arm: Use DEF_HELPER_FLAGS for helper_dc_zva
Quoted 14 lines · Richard Henderson
The function does not write registers, and only reads them by implication via the exception path.
Signed-off-by Richard Henderson <[email protected]>--- target/arm/helper-a64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/arm/helper-a64.h +0
-0
target/arm/helper-a64.h DEF_HELPER_2(advsimd_f16touinth, i32, f16, ptr)
Quoted 9 lines
target/arm/helper-a64.h +1
-2@@ -90,7 +90,7 @@ DEF_HELPER_2(advsimd_f16touinth, i32, f16, ptr) DEF_HELPER_2(sqrt_f16, f16, f16, ptr) DEF_HELPER_2(exception_return, void, env, i64) -DEF_HELPER_2(dc_zva, void, env, i64) +DEF_HELPER_FLAGS_2(dc_zva, TCG_CALL_NO_WG, void, env, i64) DEF_HELPER_FLAGS_3(pacia, TCG_CALL_NO_WG, i64, env, i64, i64)DEF_HELPER_FLAGS_3 (pacib,TCG_CALL_NO_WG , i64, env, i64, i64) --
thanks
-- PMM
PATCH 6/8 target/arm: Clean address for DC ZVA
In reply to Richard HendersonThis data access was forgotten when we added support for cleaning addresses of TBI information.
--- target/arm/translate-a64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/translate-a64.c +1 -1
@@ -1784,7 +1784,7 @@ static void handle_sys(DisasContext *s, uint32_t insn, bool isread,return ;case ARM_CP_DC_ZVA :/* Writes clear the aligned block of memory which rt points into. */ - tcg_rt =cpu_reg (s, rt); + tcg_rt =clean_data_tbi (s,cpu_reg (s, rt));gen_helper_dc_zva (cpu_env, tcg_rt);return ;default :
Signature · 1 line
2.20.1
PATCH 7/8 target/arm: Check addresses for disabled regimes
In reply to Richard HendersonWe fail to validate the upper bits of a virtual address on a translation disabled regime, as per AArch64.TranslateAddressS1Off.
--- target/arm/helper.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)
target/arm/helper.c +32 -1
@@ -11615,7 +11615,38 @@ bool get_phys_addr(CPUARMState *env, target_ulong address,/* Definitely a real MMU, not an MPU */ if (regime_translation_disabled (env, mmu_idx)) { -/* MMU disabled. */ +/* +* MMU disabled. S1 addresses are still checked for bounds. +* C.f. AArch64.TranslateAddressS1Off. +*/ +if (is_a64 (env) && mmu_idx != ARMMMUIdx_Stage2) { +int pamax =arm_pamax (env_archcpu (env)); +uint64_t tcr =regime_tcr (env, mmu_idx)->raw_tcr; +int addrtop, tbi; + + tbi =aa64_va_parameter_tbi (tcr, mmu_idx); +if (access_type ==MMU_INST_FETCH ) { + tbi &= ~aa64_va_parameter_tbid (tcr, mmu_idx); + } + tbi = (tbi >>extract64 (address,55 ,1 )) &1 ; + addrtop = (tbi ?55 :63 ); + +if (extract64 (address, pamax, addrtop - pamax +1 ) !=0 ) { + fi->type = ARMFault_AddressSize; + fi->level =0 ; + fi->stage2 = false; +return 1 ; + } + +/* +* The ARM pseudocode copies bits [51:0] to addrdesc.paddress. +* Except for TBI, we've just validated everything above PAMax +* is zero. So we only need to drop TBI. +*/ +if (tbi) { + address =extract64 (address,0 ,56 ); + } + } *phys_ptr = address; *prot =PAGE_READ |PAGE_WRITE |PAGE_EXEC ; *page_size =TARGET_PAGE_SIZE ;
Signature · 1 line
2.20.1
PATCH 8/8 target/arm: Disable clean_data_tbi for system mode
In reply to Richard HendersonWe must include the tag in the FAR_ELx register when raising an addressing exception. Which means that we should not clear out the tag during translation.
We cannot at present comply with this for user mode, so we retain the clean_data_tbi function for the moment, though it no longer does what it says on the tin for system mode. This function is to be replaced with MTE, so don't worry about the slight misnaming.
--- target/arm/translate-a64.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
target/arm/translate-a64.c +11 -0
@@ -228,7 +228,18 @@ static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)static TCGv_i64 clean_data_tbi (DisasContext *s,TCGv_i64 addr) {TCGv_i64 clean =new_tmp_a64 (s); +/* +* In order to get the correct value in the FAR_ELx register, +* we must present the memory subsystem with the "dirty" address +* including the TBI. In system mode we can make this work via +* the TLB, dropping the TBI during translation. But for user-only +* mode we don't have that option, and must remove the top byte now. +*/ +#ifdef CONFIG_USER_ONLY gen_top_byte_ignore (s, clean, addr, s->tbid); +#else +tcg_gen_mov_i64 (clean, addr); +#endif return clean; }
Signature · 1 line
2.20.1
Re: [PATCH 2/8] target/arm: Optimize cpu_mmu_index
In reply to Richard HendersonQuoted 4 lines · Richard Henderson
We now cache the core
mmu_idxinenv->hflags. Rather than recompute from scratch, extract the field. All of the uses ofcpu_mmu_indexwithin target/arm are within helpers whereenv->hflagsis stable.
Do you mean "within helpers, and env->hflags is always stable in a helper", or "within helpers, and env->hflags is stable for the particular set of helpers where we use cpu_mmu_index, though it might not be in other helpers" ?
thanks
-- PMM
Re: [PATCH 2/8] target/arm: Optimize cpu_mmu_index
Quoted 11 lines · Peter Maydell
Quoted 4 lines · Richard Henderson
We now cache the core
mmu_idxinenv->hflags. Rather than recompute from scratch, extract the field. All of the uses ofcpu_mmu_indexwithin target/arm are within helpers whereenv->hflagsis stable.Do you mean "within helpers, and
env->hflagsis always stable in a helper", or "within helpers, andenv->hflagsis stable for the particular set of helpers where we usecpu_mmu_index, though it might not be in other helpers" ?
The former.
With the caveat that it's pretty clear when a helper is doing things that make it the exception to that rule. E.g. helper_exception_return, which itself invokes rebuild_hflags.
r~
Quoted 9 lines · Richard Henderson
We have a bug at present wherein we do not supply the memory tag to the memory system, so that on fault
FAR_ELxdoes not contain the correct value.For system mode, we already handle ignoring TBI in
get_phys_addr_lpae, as long as we don't actually drop the tag during translation. For user mode, we don't have that option, so for now we must simply accept that we'll get the wrong value in thesiginfo_t.
Something weird happened to this series: it looks like the first 2 patches were sent as replies to the cover letter, but then patches 3-8 were all replies to patch 2:
https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06699.html
The result is that neither patches nor patchew think they got the entire series. Could you resend it, please?
thanks
-- PMM