target-arm: Fix arm_el_is_aa64() function to support EL2 and EL3
v2 cover target-arm: Fix arm_el_is_aa64() function to support EL2 and EL3
Function arm_el_is_aa64() was fixed to support EL2 and EL3. It is needed for a future support of EL2 and/or EL3,
and 32 bit EL1/EL2 support for ARMv8 cpu. ARM_FEATURE_AARCH64 flag means that the highest exception level is in AArch64 state. The state of lower exception levels is controlled by the HCR_EL2.RW, SCR_EL3.RW and SCR_EL3.NS bits.
If EL2 or EL3 is not permitted by the appropriate ARM_FEATURE flag, then the function arm_el_is_aa64() aborts on an attempt to get the bitness of this EL. So we need to be sure that EL passed to the function is enabled. Therefore some conditional statements was slightly changed to check it.
Sergey Sorokin (2):
target-arm: Fix default_exception_el() function for the case when EL3
is not supported
target-arm: Fix arm_el_is_aa64() function to support EL2 and EL3
hw/arm/boot.c | 4 ++++ target-arm/cpu.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ target-arm/cpu.h | 51 +++++++++++++++++++++++++++------------ target-arm/helper.c | 23 +++++++++++++++++- target-arm/machine.c | 5 ++++ target-arm/translate-a64.c | 6 ++++- target-arm/translate.c | 6 ++++- target-arm/translate.h | 7 ++++-- 8 files changed, 141 insertions(+), 20 deletions(-)
Signature · 1 line
1.9.3
PATCH v2 1/2 target-arm: Fix default_exception_el() function for the case when EL3 is not supported
If EL3 is not supported in current configuration,
we should not try to get EL3 bitness.
--- target-arm/translate-a64.c | 6 +++++- target-arm/translate.c | 6 +++++- target-arm/translate.h | 7 +++++-- 3 files changed, 15 insertions(+), 4 deletions(-)
target-arm/translate-a64.c +5 -1
@@ -10946,7 +10946,11 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu, dc->condjmp =0 ; dc->aarch64 =1 ; - dc->el3_is_aa64 =arm_el_is_aa64 (env,3 ); +/* If we are coming from secure EL0 in a system with a 32-bit EL3, then +* there is no secure EL1, so we route exceptions to EL3. +*/ + dc->secure_routed_to_el3 =arm_feature (env,ARM_FEATURE_EL3 ) && + !arm_el_is_aa64 (env,3 ); dc->thumb =0 ; dc->bswap_code =0 ; dc->condexec_mask =0 ;
target-arm/translate.c +5 -1
@@ -11172,7 +11172,11 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu, dc->condjmp =0 ; dc->aarch64 =0 ; - dc->el3_is_aa64 =arm_el_is_aa64 (env,3 ); +/* If we are coming from secure EL0 in a system with a 32-bit EL3, then +* there is no secure EL1, so we route exceptions to EL3. +*/ + dc->secure_routed_to_el3 =arm_feature (env,ARM_FEATURE_EL3 ) && + !arm_el_is_aa64 (env,3 ); dc->thumb =ARM_TBFLAG_THUMB (tb->flags); dc->bswap_code =ARM_TBFLAG_BSWAP_CODE (tb->flags); dc->condexec_mask = (ARM_TBFLAG_CONDEXEC (tb->flags) &0xf ) <<1 ;
target-arm/translate.h +5 -2
@@ -23,7 +23,10 @@ typedef struct DisasContext {ARMMMUIdx mmu_idx;/* MMU index to use for normal loads/stores */ bool ns;/* Use non-secure CPREG bank on access */ int fp_excp_el;/* FP exception EL or 0 if enabled */ -bool el3_is_aa64;/* Flag indicating whether EL3 is AArch64 or not */ +/* Flag indicating that an exceptions from the secure mode +* are routed to EL3. +*/ +bool secure_routed_to_el3;bool vfp_enabled;/* FP enabled via FPSCR.EN */ int vec_len;int vec_stride;
@@ -84,7 +87,7 @@ static inline int default_exception_el(DisasContext *s)* exceptions can only be routed to ELs above 1, so we target the higher of * 1 or the current EL. */ -return (s->mmu_idx == ARMMMUIdx_S1SE0 && !s->el3_is_aa64) +return (s->mmu_idx == ARMMMUIdx_S1SE0 && s->secure_routed_to_el3) ?3 :MAX (1 , s->current_el); }
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v2 1/2] target-arm: Fix default_exception_el() function for the case when EL3 is not supported
Quoted 9 lines · Sergey Sorokin
If EL3 is not supported in current configuration,
we should not try to get EL3 bitness.Signed-off-by Sergey Sorokin <[email protected]>--- target-arm/translate-a64.c | 6 +++++- target-arm/translate.c | 6 +++++- target-arm/translate.h | 7 +++++-- 3 files changed, 15 insertions(+), 4 deletions(-)
I've applied this first patch to target-arm.next, to save you carrying it around. (I fixed a minor typo in a comment.)
thanks
-- PMM
Re: [Qemu-devel] [PATCH v2 1/2] target-arm: Fix default_exception_el() function for the case when EL3 is not supported
08.09.2015, 16:52, "Peter Maydell" <[email protected]>:
Quoted 11 lines · Sergey Sorokin
Quoted 9 lines
If EL3 is not supported in current configuration, we should not try to get EL3 bitness.
Signed-off-by Sergey Sorokin <[email protected]>---
target-arm/translate-a64.c | 6 +++++-
target-arm/translate.c | 6 +++++-
target-arm/translate.h | 7 +++++--3 files changed, 15 insertions(+), 4 deletions(-)
Quoted 7 lines
Reviewed-by Peter Maydell <[email protected]>I've applied this first patch to target-arm.next, to save you carrying it around. (I fixed a minor typo in a comment.)
thanks
-- PMM
Should I resend this patch with new version of the series?
Re: [Qemu-devel] [PATCH v2 1/2] target-arm: Fix default_exception_el() function for the case when EL3 is not supported
Quoted 14 lines · Sergey Sorokin
08.09.2015, 16:52, "Peter Maydell" <[email protected]>:
Quoted 11 lines · Sergey Sorokin
Quoted 9 lines
If EL3 is not supported in current configuration, we should not try to get EL3 bitness.
Signed-off-by Sergey Sorokin <[email protected]>---
target-arm/translate-a64.c | 6 +++++- target-arm/translate.c | 6 +++++- target-arm/translate.h | 7 +++++-- 3 files changed, 15 insertions(+), 4 deletions(-)
Quoted 6 lines
Quoted 4 lines
Reviewed-by Peter Maydell <[email protected]>I've applied this first patch to target-arm.next, to save you carrying it around. (I fixed a minor typo in a comment.)
Should I resend this patch with new version of the series?
No, you don't need to. It's now in master.
thanks
-- PMM
PATCH v2 2/2 target-arm: Fix arm_el_is_aa64() function to support EL2 and EL3
In reply to Sergey SorokinFunction arm_el_is_aa64() was fixed to support EL2 and EL3. It is needed for a future support of EL2 and/or EL3,
and 32 bit EL1/EL2 support for ARMv8 cpu. ARM_FEATURE_AARCH64 flag means that the highest exception level is in AArch64 state. The state of lower exception levels is controlled by the HCR_EL2.RW, SCR_EL3.RW and SCR_EL3.NS bits.
If EL2 or EL3 is not permitted by the appropriate ARM_FEATURE flag, then the function arm_el_is_aa64() aborts on an attempt to get the bitness of this EL. So we need to be sure that EL passed to the function is enabled. Therefore some conditional statements was slightly changed to check it.
---
Changes since previous version:
* Some typos was fixed.
* Extended comments was added.
* The initial patch was divided in two parts.
* The erroneous changes in arm_excp_unmasked() function was fixed.
hw/arm/boot.c | 4 ++++ target-arm/cpu.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ target-arm/cpu.h | 51 ++++++++++++++++++++++++++++++++------------- target-arm/helper.c | 23 +++++++++++++++++++- target-arm/machine.c | 5 +++++ 5 files changed, 126 insertions(+), 16 deletions(-)
hw/arm/boot.c +4 -0
@@ -486,6 +486,10 @@ static void do_cpu_reset(void *opaque)if (!info->secure_boot) {/* Linux expects non-secure state */ env->cp15.scr_el3 |=SCR_NS ; +/* EL bitness can depend on SCR.NS bit, so we need +* recalc it. +*/ +calc_el_bitness (env); } }
target-arm/cpu.c +59 -0
71 lines not on this page · raw message
target-arm/cpu.h +36 -15
88 lines not on this page · raw message
target-arm/helper.c +22 -1
@@ -838,6 +838,7 @@ static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)* current execution mode before directly using the feature bit. */ uint32_t valid_mask =SCR_AARCH64_MASK |SCR_AARCH32_MASK ; +uint64_t old_value;if (!arm_feature (env,ARM_FEATURE_EL2 )) { valid_mask &= ~SCR_HCE ;
@@ -856,7 +857,14 @@ static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)/* Clear all-context RES0 bits. */ value &= valid_mask; + old_value =raw_read (env, ri);raw_write (env, ri, value); + +/* We need to recalculate EL bitness if SCR_EL3.RW, SCR_EL3.NS +* or HCR_EL2.RW has been changed. */ +if ((old_value ^ value) & (SCR_RW |SCR_NS )) { +calc_el_bitness (env); + } }static uint64_t ccsidr_read (CPUARMState *env,const ARMCPRegInfo *ri)
@@ -3146,6 +3154,7 @@ static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) {ARMCPU *cpu =arm_env_get_cpu (env);uint64_t valid_mask =HCR_MASK ; +uint64_t old_value;if (arm_feature (env,ARM_FEATURE_EL3 )) { valid_mask &= ~HCR_HCD ;
@@ -3164,7 +3173,14 @@ static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)if ((raw_read (env, ri) ^ value) & (HCR_VM |HCR_PTW |HCR_DC )) {tlb_flush (CPU (cpu),1 ); } + old_value =raw_read (env, ri);raw_write (env, ri, value); + +/* We need to recalculate EL bitness if SCR_EL3.RW, SCR_EL3.NS +* or HCR_EL2.RW has been changed. */ +if ((old_value ^ value) &HCR_RW ) { +calc_el_bitness (env); + } }static const ARMCPRegInfo el2_cp_reginfo[] = {
@@ -5088,7 +5104,8 @@ uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,int scr;int hcr;int target_el; -int is64 =arm_el_is_aa64 (env,3 ); +/* Is the higher EL AArch64? */ +int is64 =arm_feature (env,ARM_FEATURE_AARCH64 );switch (excp_idx) {case EXCP_IRQ :
@@ -5645,6 +5662,10 @@ void arm_cpu_do_interrupt(CPUState *cs)if ((env->uncached_cpsr &CPSR_M ) ==ARM_CPU_MODE_MON ) { env->cp15.scr_el3 &= ~SCR_NS ; +/* SCR.NS bit is changed, but we don't need to call calc_el_bitness() +* here, because the transition to AArch32 EL3 supposes that +* all lower ELs are always 32-bit. +*/ }switch_mode (env, new_mode);
target-arm/machine.c +5 -0
@@ -265,6 +265,11 @@ static int cpu_post_load(void *opaque, int version_id) } } +/* It is not nesessary, because this call was made on +* HCR and SCR writes above, but just to make sure. +*/ +calc_el_bitness (&cpu->env); +hw_breakpoint_update_all (cpu);hw_watchpoint_update_all (cpu);
Signature · 1 line
1.9.3
Quoted 97 lines · Sergey Sorokin
Function
arm_el_is_aa64()was fixed to support EL2 and EL3. It is needed for a future support of EL2 and/or EL3,
and 32 bit EL1/EL2 support for ARMv8 cpu.ARM_FEATURE_AARCH64flag means that the highest exception level is in AArch64 state. The state of lower exception levels is controlled by the HCR_EL2.RW, SCR_EL3.RW and SCR_EL3.NS bits.
If EL2 or EL3 is not permitted by the appropriateARM_FEATUREflag, then the functionarm_el_is_aa64()aborts on an attempt to get the bitness of this EL. So we need to be sure that EL passed to the function is enabled. Therefore some conditional statements was slightly changed to check it.Signed-off-by Sergey Sorokin <[email protected]>---
Changes since previous version:
* Some typos was fixed.
* Extended comments was added.
* The initial patch was divided in two parts.
* The erroneous changes inarm_excp_unmasked()function was fixed.hw/arm/boot.c | 4 ++++ target-arm/cpu.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ target-arm/cpu.h | 51 ++++++++++++++++++++++++++++++++------------- target-arm/helper.c | 23 +++++++++++++++++++- target-arm/machine.c | 5 +++++ 5 files changed, 126 insertions(+), 16 deletions(-)hw/arm/boot.c +4
-0@@ -486,6 +486,10 @@ static void do_cpu_reset(void *opaque)if (!info->secure_boot) {/* Linux expects non-secure state */ env->cp15.scr_el3 |=SCR_NS ; +/* EL bitness can depend on SCR.NS bit, so we need +* recalc it. +*/ +calc_el_bitness (env); } }target-arm/cpu.c +47
-0@@ -100,6 +100,64 @@ static void cp_reg_check_reset(gpointer key, gpointer value, gpointer opaque)assert (oldvalue == newvalue); } +/* This function must be called if SCR_EL3.RW, SCR_EL3.NS +* or HCR_EL2.RW has been changed. +*/ +void calc_el_bitness (CPUARMState *env) +{ +/* -1 is invalid value */ + env->el_is_aa64[0 ] =-1 ; + +/* If the highest EL is not AArch64, then all ELs are AArch32. */ +if (!arm_feature (env,ARM_FEATURE_AARCH64 )) { +if (arm_feature (env,ARM_FEATURE_EL3 )) { + env->el_is_aa64[3 ] =0 ; + }else { + env->el_is_aa64[3 ] =-1 ; + } + +if (arm_feature (env,ARM_FEATURE_EL2 )) { + env->el_is_aa64[2 ] =0 ; + }else { + env->el_is_aa64[2 ] =-1 ; + } + + env->el_is_aa64[1 ] =0 ; +return ; + } + +if (arm_feature (env,ARM_FEATURE_EL3 )) { + env->el_is_aa64[3 ] =1 ; +int8_t scr_rw = env->cp15.scr_el3 &SCR_RW ?1 :0 ; + +if (arm_feature (env,ARM_FEATURE_EL2 ) && (env->cp15.scr_el3 &SCR_NS )) { +if (scr_rw) { + env->el_is_aa64[2 ] =1 ; + env->el_is_aa64[1 ] = env->cp15.hcr_el2 &HCR_RW ?1 :0 ; + }else { + env->el_is_aa64[2 ] =0 ; + env->el_is_aa64[1 ] =0 ; + } + }else { +/* If there is no EL2 or if secure. */ + env->el_is_aa64[2 ] =-1 ; + env->el_is_aa64[1 ] = scr_rw; + } + }else { + env->el_is_aa64[3 ] =-1 ; + +if (arm_feature (env,ARM_FEATURE_EL2 )) {
target-arm/cpu.c static void cp_reg_check_reset(gpointer key, gpointer value, gpointer opaque) · line 159
Quoted 10 lines
target-arm/cpu.c +10
-0+/* The highest EL is EL2, and it is AArch64. */ + env->el_is_aa64[2 ] =1 ; + env->el_is_aa64[1 ] = env->cp15.hcr_el2 &HCR_RW ?1 :0 ; + }else { +/* The highest EL is EL1, and it is AArch64. */ + env->el_is_aa64[2 ] =-1 ; + env->el_is_aa64[1 ] =1 ; + } + } +}
This is definitely rather overlarge for what it's doing. I think it's sufficient to write:
void calc_el_bitness(CPUARMState *env)
{
bool aa64 = arm_feature(env, ARM_FEATURE_AARCH64);
if (arm_feature(env, ARM_FEATURE_EL3)) {
env->el_is_aa64[3] = aa64;
aa64 = aa64 && (env->cp15.scr_el3 & SCR_RW);
} else {
env->el_is_aa64[3] = -1;
}
if (arm_feature(env, ARM_FEATURE_EL2) && !arm_is_secure_below_el3(env)) {
env->el_is_aa64[2] = aa64;
aa64 = aa64 && (env->cp15.hcr_el2 & HCR_RW);
} else {
env->el_is_aa64[2] = -1;
}
env->el_is_aa64[1] = aa64;
}And in fact this is pretty simple, so we could really just have
static inline bool arm_el_is_aa64(CPUARMState *env, int el)
{
/* This isn't valid for EL0 (if we're in EL0, is_a64() is what you want,
* and if we're not in EL0 then the state of EL0 isn't well defined.)
*/
assert(el >= 1 && el <= 3);
bool aa64 = arm_feature(env, ARM_FEATURE_AARCH64);
if (el == 3) {
return aa64;
}
if (arm_feature(env, ARM_FEATURE_EL3)) {
aa64 = aa64 && (env->cp15.scr_el3 & SCR_RW);
}
if (el == 2) {
return aa64;
}
if (arm_feature(env, ARM_FEATURE_EL2) && !arm_is_secure_below_el3(env)) {
aa64 = aa64 && (env->cp15.hcr_el2 & HCR_RW);
}
return aa64;
}and not bother with the caching at all. I think I'd prefer that unless you have performance figures showing the caching makes a noticeable difference.
Quoted 5 lines
target-arm/cpu.c +0
-0/* Function for determing whether guest cp register reads and writes should @@ -1012,11 +1031,11 @@ static inline bool access_secure_reg(CPUARMState *env)*/ #define A32_BANKED_CURRENT_REG_GET (_env, _regname) \ A32_BANKED_REG_GET((_env), _regname, \
target-arm/cpu.c static inline bool access_secure_reg(CPUARMState *env) · line 1041
Quoted 10 lines
target-arm/cpu.c +2
-2- ((!arm_el_is_aa64((_env), 3) && arm_is_secure(_env)))) + (arm_is_secure(_env) && !arm_el_is_aa64((_env), 3)))#define A32_BANKED_CURRENT_REG_SET (_env, _regname, _val) \ A32_BANKED_REG_SET((_env), _regname, \ - ((!arm_el_is_aa64((_env), 3) && arm_is_secure(_env))), \ + (arm_is_secure(_env) && !arm_el_is_aa64((_env), 3)), \ (_val))void arm_cpu_list (FILE *f,fprintf_function cpu_fprintf);
This hunk, and the two I've quoted below, are trying to avoid calling arm_el_is_aa64() for an unimplemented EL. I think it makes sense to do that, but they should be a different patch from the one that changes the implementation of arm_el_is_aa64().
target-arm/cpu.c static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, · line 1605
Quoted 6 lines
target-arm/cpu.c +1
-1@@ -1583,7 +1602,9 @@ static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,* interrupt. */ if ((target_el > cur_el) && (target_el !=1 )) { -if (arm_el_is_aa64 (env,3 ) || ((scr || hcr) && (!secure))) { +/* ARM_FEATURE_AARCH64 enabled means the higher EL is AArch64. */
"highest"
target-arm/cpu.c static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, · line 1610
Quoted 5 lines
target-arm/cpu.c +2
-0+if (arm_feature (env,ARM_FEATURE_AARCH64 ) || + ((scr || hcr) && (!secure))) { unmasked =1 ; } }
I think this change is correct, though it took me a while to convince myself of it. (Roughly, I think that in the case where we have AArch64 EL2 but not EL3 then the (hcr && !secure) half of the condition is going to be true anyway, so it doesn't matter what the test on the left side of the || is.)
target-arm/cpu.c uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, · line 5107
Quoted 6 lines
target-arm/cpu.c +1
-1@@ -5088,7 +5104,8 @@ uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,int scr;int hcr;int target_el; -int is64 =arm_el_is_aa64 (env,3 ); +/* Is the higher EL AArch64? */
"highest"
target-arm/cpu.c uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, · line 5111
Quoted 4 lines
target-arm/cpu.c +1
-0+int is64 =arm_feature (env,ARM_FEATURE_AARCH64 );switch (excp_idx) {case EXCP_IRQ :
I think this change is insufficient. If there is no EL3 but there is an EL2 and EL2 is AArch64, then we should route exceptions in the same way as if EL3 existed and was AArch64. For this to be the case we need to also get rw correct (ie reflecting the width of EL2). So something like:
if (arm_feature(env, ARM_FEATURE_EL3)) {
rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
} else {
rw = is64;
}target-arm/machine.c static int cpu_post_load(void *opaque, int version_id) · line 271
Quoted 12 lines
target-arm/machine.c +4
-0@@ -265,6 +265,11 @@ static int cpu_post_load(void *opaque, int version_id) } } +/* It is not nesessary, because this call was made on +* HCR and SCR writes above, but just to make sure. +*/ +calc_el_bitness (&cpu->env);
We should generally be confident about our code. Either this call *is* necessary, in which case the comment should say why, or it is *not* necessary, in which case it shouldn't be here (but you can have a comment saying why it isn't). In this case I think you are correct that it's not needed. (Some of the only-happens-on-value-change behaviour of register writes needs care in register loading, because you want the behaviour to happen anyway even if the old stale value in the CPU state happened to be the same as the new value from incoming migration data. But in this case we're OK because if the incoming migration data happens to be the same as the old data for all of the SCR and HCR bits then the old cached el_is_aa64[] values will all still be correct, even though we will never actually call calc_el_bitness().)
target-arm/machine.c static int cpu_post_load(void *opaque, int version_id) · line 274
Quoted 3 lines
target-arm/machine.c +1
-0+hw_breakpoint_update_all (cpu);hw_watchpoint_update_all (cpu);
thanks
-- PMM
08.09.2015, 16:44, "Peter Maydell" <[email protected]>:
Quoted 125 lines · Sergey Sorokin
Quoted 97 lines
Function
arm_el_is_aa64()was fixed to support EL2 and EL3. It is needed for a future support of EL2 and/or EL3,
and 32 bit EL1/EL2 support for ARMv8 cpu.ARM_FEATURE_AARCH64flag means that the highest exception level is in AArch64 state. The state of lower exception levels is controlled by the HCR_EL2.RW, SCR_EL3.RW and SCR_EL3.NS bits.
If EL2 or EL3 is not permitted by the appropriateARM_FEATUREflag, then the functionarm_el_is_aa64()aborts on an attempt to get the bitness of this EL. So we need to be sure that EL passed to the function is enabled. Therefore some conditional statements was slightly changed to check it.Signed-off-by Sergey Sorokin <[email protected]>---
Changes since previous version:
* Some typos was fixed.
* Extended comments was added.
* The initial patch was divided in two parts.
* The erroneous changes inarm_excp_unmasked()function was fixed.hw/arm/boot.c | 4 ++++ target-arm/cpu.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ target-arm/cpu.h | 51 ++++++++++++++++++++++++++++++++------------- target-arm/helper.c | 23 +++++++++++++++++++- target-arm/machine.c | 5 +++++ 5 files changed, 126 insertions(+), 16 deletions(-)hw/arm/boot.c +4
-0@@ -486,6 +486,10 @@ static void do_cpu_reset(void *opaque)if (!info->secure_boot) {/* Linux expects non-secure state */ env->cp15.scr_el3 |=SCR_NS ; +/* EL bitness can depend on SCR.NS bit, so we need +* recalc it. +*/ +calc_el_bitness (env); } }target-arm/cpu.c +47
-0@@ -100,6 +100,64 @@ static void cp_reg_check_reset(gpointer key, gpointer value, gpointer opaque)assert (oldvalue == newvalue); } +/* This function must be called if SCR_EL3.RW, SCR_EL3.NS +* or HCR_EL2.RW has been changed. +*/ +void calc_el_bitness (CPUARMState *env) +{ +/* -1 is invalid value */ + env->el_is_aa64[0 ] =-1 ; + +/* If the highest EL is not AArch64, then all ELs are AArch32. */ +if (!arm_feature (env,ARM_FEATURE_AARCH64 )) { +if (arm_feature (env,ARM_FEATURE_EL3 )) { + env->el_is_aa64[3 ] =0 ; + }else { + env->el_is_aa64[3 ] =-1 ; + } + +if (arm_feature (env,ARM_FEATURE_EL2 )) { + env->el_is_aa64[2 ] =0 ; + }else { + env->el_is_aa64[2 ] =-1 ; + } + + env->el_is_aa64[1 ] =0 ; +return ; + } + +if (arm_feature (env,ARM_FEATURE_EL3 )) { + env->el_is_aa64[3 ] =1 ; +int8_t scr_rw = env->cp15.scr_el3 &SCR_RW ?1 :0 ; + +if (arm_feature (env,ARM_FEATURE_EL2 ) && (env->cp15.scr_el3 &SCR_NS )) { +if (scr_rw) { + env->el_is_aa64[2 ] =1 ; + env->el_is_aa64[1 ] = env->cp15.hcr_el2 &HCR_RW ?1 :0 ; + }else { + env->el_is_aa64[2 ] =0 ; + env->el_is_aa64[1 ] =0 ; + } + }else { +/* If there is no EL2 or if secure. */ + env->el_is_aa64[2 ] =-1 ; + env->el_is_aa64[1 ] = scr_rw; + } + }else { + env->el_is_aa64[3 ] =-1 ; + +if (arm_feature (env,ARM_FEATURE_EL2 )) {target-arm/cpu.c static void cp_reg_check_reset(gpointer key, gpointer value, gpointer opaque) · line 159
Quoted 10 lines
target-arm/cpu.c +10
-0+/* The highest EL is EL2, and it is AArch64. */ + env->el_is_aa64[2 ] =1 ; + env->el_is_aa64[1 ] = env->cp15.hcr_el2 &HCR_RW ?1 :0 ; + }else { +/* The highest EL is EL1, and it is AArch64. */ + env->el_is_aa64[2 ] =-1 ; + env->el_is_aa64[1 ] =1 ; + } + } +}This is definitely rather overlarge for what it's doing. I think it's sufficient to write:
void
calc_el_bitness(CPUARMState *env){ bool aa64 = arm_feature(env, ARM_FEATURE_AARCH64); if (arm_feature(env, ARM_FEATURE_EL3)) { env->el_is_aa64[3] = aa64; aa64 = aa64 && (env->cp15.scr_el3 & SCR_RW); } else { env->el_is_aa64[3] = -1; } if (arm_feature(env, ARM_FEATURE_EL2) && !arm_is_secure_below_el3(env)) {
Quoted 8 lines
env->el_is_aa64[2] = aa64;
aa64 = aa64 && (env->cp15.hcr_el2 & HCR_RW);
} else {
env->el_is_aa64[2] = -1;
}
env->el_is_aa64[1] = aa64;
}I agree, this version looks better.
Quoted 21 lines
And in fact this is pretty simple, so we could really just have
static inline bool
arm_el_is_aa64(CPUARMState *env, int el){ /* This isn't valid for EL0 (if we're in EL0, is_a64() is what you want, * and if we're not in EL0 then the state of EL0 isn't well defined.) */ assert(el >= 1 && el <= 3); bool aa64 = arm_feature(env, ARM_FEATURE_AARCH64); if (el == 3) { return aa64; } if (arm_feature(env, ARM_FEATURE_EL3)) { aa64 = aa64 && (env->cp15.scr_el3 & SCR_RW); } if (el == 2) { return aa64;
Quoted 8 lines
}
if (arm_feature(env, ARM_FEATURE_EL2) && !arm_is_secure_below_el3(env)) {
aa64 = aa64 && (env->cp15.hcr_el2 & HCR_RW);
}
return aa64;
}I supposed that the function should abort on attempt to call it with unimplemented EL. Just to avoid bugs. This your version does not abort in such case. With the check it will not be so short.
Quoted 4 lines
and not bother with the caching at all. I think I'd prefer that unless you have performance figures showing the caching makes a noticeable difference.
arm_el_is_aa64() is used (maybe indirectly) in address translations, in do_interrupt() functions, some sysreg access, cpu_get_tb_cpu_state () and arm_current_el() functions, etc. And obviously it will be used more widely in future.
I think it's enough to show the caching makes sense. Nevertheless, if you disagree with me, I could remove the caching from the patch.
Quoted 36 lines
Quoted 5 lines
target-arm/cpu.c +0
-0/* Function for determing whether guest cp register reads and writes should @@ -1012,11 +1031,11 @@ static inline bool access_secure_reg(CPUARMState *env)*/ #define A32_BANKED_CURRENT_REG_GET (_env, _regname) \ A32_BANKED_REG_GET((_env), _regname, \target-arm/cpu.c static inline bool access_secure_reg(CPUARMState *env) · line 1041
Quoted 10 lines
target-arm/cpu.c +2
-2- ((!arm_el_is_aa64((_env), 3) && arm_is_secure(_env)))) + (arm_is_secure(_env) && !arm_el_is_aa64((_env), 3)))#define A32_BANKED_CURRENT_REG_SET (_env, _regname, _val) \ A32_BANKED_REG_SET((_env), _regname, \ - ((!arm_el_is_aa64((_env), 3) && arm_is_secure(_env))), \ + (arm_is_secure(_env) && !arm_el_is_aa64((_env), 3)), \ (_val))void arm_cpu_list (FILE *f,fprintf_function cpu_fprintf);This hunk, and the two I've quoted below, are trying to avoid calling
arm_el_is_aa64()for an unimplemented EL. I think it makes sense to do that, but they should be a different patch from the one that changes the implementation ofarm_el_is_aa64().target-arm/cpu.c static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, · line 1605
Quoted 6 lines
target-arm/cpu.c +1
-1@@ -1583,7 +1602,9 @@ static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,* interrupt. */ if ((target_el > cur_el) && (target_el !=1 )) { -if (arm_el_is_aa64 (env,3 ) || ((scr || hcr) && (!secure))) { +/* ARM_FEATURE_AARCH64 enabled means the higher EL is AArch64. */ "highest"
target-arm/cpu.c static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, · line 1610
Quoted 5 lines
target-arm/cpu.c +2
-0+if (arm_feature (env,ARM_FEATURE_AARCH64 ) || + ((scr || hcr) && (!secure))) { unmasked =1 ; } }
Quoted 6 lines
I think this change is correct, though it took me a while to convince myself of it. (Roughly, I think that in the case where we have AArch64 EL2 but not EL3 then the (hcr && !secure) half of the condition is going to be true anyway, so it doesn't matter what the test on the left side of the || is.)
The change makes the check more intelligible in case of unimplemented EL3. Moreover, arm_el_is_aa64() will abort with el argument == 3
if there is no EL3 in current configuration.
Quoted 20 lines
target-arm/cpu.c uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, · line 5107
Quoted 6 lines
target-arm/cpu.c +1
-1@@ -5088,7 +5104,8 @@ uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,int scr;int hcr;int target_el; -int is64 =arm_el_is_aa64 (env,3 ); +/* Is the higher EL AArch64? */ "highest"
target-arm/cpu.c uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, · line 5111
Quoted 4 lines
target-arm/cpu.c +1
-0+int is64 =arm_feature (env,ARM_FEATURE_AARCH64 );switch (excp_idx) {case EXCP_IRQ :I think this change is insufficient. If there is no EL3 but there is an EL2 and EL2 is AArch64, then we should route exceptions in the same way as if EL3 existed and was AArch64. For this to be the case we need to also get rw correct (ie reflecting the width of EL2). So something like:
Quoted 6 lines
if (arm_feature(env, ARM_FEATURE_EL3)) {
rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
} else {
rw = is64;
}Yes, I agree.
Quoted 19 lines
target-arm/machine.c static int cpu_post_load(void *opaque, int version_id) · line 271
Quoted 12 lines
target-arm/machine.c +4
-0@@ -265,6 +265,11 @@ static int cpu_post_load(void *opaque, int version_id) } } +/* It is not nesessary, because this call was made on +* HCR and SCR writes above, but just to make sure. +*/ +calc_el_bitness (&cpu->env);We should generally be confident about our code. Either this call *is* necessary, in which case the comment should say why, or it is *not* necessary, in which case it shouldn't be here (but you can have a comment saying why it isn't). In this case I think you are correct that it's not needed. (Some of the only-happens-on-value-change behaviour of register
Quoted 9 lines
writes needs care in register loading, because you want the behaviour to happen anyway even if the old stale value in the CPU state happened to be the same as the new value from incoming migration data. But in this case we're OK because if the incoming migration data happens to be the same as the old data for all of the SCR and HCR bits then the old cached el_is_aa64[] values will all still be correct, even though we will never actually call
calc_el_bitness().)
I have made this change acording to your last comment on the first version of the patch. I will be glad to remove it :)
Quoted 27 lines · Sergey Sorokin
08.09.2015, 16:44, "Peter Maydell" <[email protected]>:
Quoted 26 lines
And in fact this is pretty simple, so we could really just have
static inline bool
arm_el_is_aa64(CPUARMState *env, int el){ /* This isn't valid for EL0 (if we're in EL0, is_a64() is what you want, * and if we're not in EL0 then the state of EL0 isn't well defined.) */ assert(el >= 1 && el <= 3); bool aa64 = arm_feature(env, ARM_FEATURE_AARCH64); if (el == 3) { return aa64; } if (arm_feature(env, ARM_FEATURE_EL3)) { aa64 = aa64 && (env->cp15.scr_el3 & SCR_RW); } if (el == 2) { return aa64; } if (arm_feature(env, ARM_FEATURE_EL2) && !arm_is_secure_below_el3(env)) { aa64 = aa64 && (env->cp15.hcr_el2 & HCR_RW); }
Quoted 7 lines
Quoted 2 lines
return aa64; }I supposed that the function should abort on attempt to call it with unimplemented EL. Just to avoid bugs. This your version does not abort in such case. With the check it will not be so short.
Assertions are nice when they're easy, but we don't have to necessarily make the code more complex purely in order to try to provide a place to assert. In practice I would expect that pretty much all 64-bit CPUs we emulate will implement both EL2 and EL3 anyway.
Quoted 11 lines
Quoted 3 lines
and not bother with the caching at all. I think I'd prefer that unless you have performance figures showing the caching makes a noticeable difference.
arm_el_is_aa64()is used (maybe indirectly) in address translations, indo_interrupt()functions, some sysreg access,cpu_get_tb_cpu_state() andarm_current_el()functions, etc. And obviously it will be used more widely in future.
I think it's enough to show the caching makes sense. Nevertheless, if you disagree with me, I could remove the caching from the patch.
If you can show that the cacheing is worth having (by showing an actual speedup on some workload), then that's fine. But it does complicate the code, so I'd prefer not to unless we have some evidence for the benefit side of things. Gut feeling about "I think this will be faster" is often inaccurate.
Quoted 43 lines
Quoted 36 lines
Quoted 5 lines
/* Function for determing whether guest cp register reads and writes should
target-arm/cpu.h +0
-0@@ -1012,11 +1031,11 @@ static inline bool access_secure_reg(CPUARMState *env)*/ #define A32_BANKED_CURRENT_REG_GET (_env, _regname) \ A32_BANKED_REG_GET((_env), _regname, \target-arm/cpu.h static inline bool access_secure_reg(CPUARMState *env) · line 1041
Quoted 10 lines
target-arm/cpu.h +2
-2- ((!arm_el_is_aa64((_env), 3) && arm_is_secure(_env)))) + (arm_is_secure(_env) && !arm_el_is_aa64((_env), 3)))#define A32_BANKED_CURRENT_REG_SET (_env, _regname, _val) \ A32_BANKED_REG_SET((_env), _regname, \ - ((!arm_el_is_aa64((_env), 3) && arm_is_secure(_env))), \ + (arm_is_secure(_env) && !arm_el_is_aa64((_env), 3)), \ (_val))void arm_cpu_list (FILE *f,fprintf_function cpu_fprintf);This hunk, and the two I've quoted below, are trying to avoid calling
arm_el_is_aa64()for an unimplemented EL. I think it makes sense to do that, but they should be a different patch from the one that changes the implementation ofarm_el_is_aa64().target-arm/cpu.h static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, · line 1605
Quoted 6 lines
target-arm/cpu.h +1
-1@@ -1583,7 +1602,9 @@ static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx,* interrupt. */ if ((target_el > cur_el) && (target_el !=1 )) { -if (arm_el_is_aa64 (env,3 ) || ((scr || hcr) && (!secure))) { +/* ARM_FEATURE_AARCH64 enabled means the higher EL is AArch64. */ "highest"
target-arm/cpu.h static inline bool arm_excp_unmasked(CPUState *cs, unsigned int excp_idx, · line 1610
Quoted 5 lines
target-arm/cpu.h +2
-0+if (arm_feature (env,ARM_FEATURE_AARCH64 ) || + ((scr || hcr) && (!secure))) { unmasked =1 ; } }Quoted 6 lines
I think this change is correct, though it took me a while to convince myself of it. (Roughly, I think that in the case where we have AArch64 EL2 but not EL3 then the (hcr && !secure) half of the condition is going to be true anyway, so it doesn't matter what the test on the left side of the || is.)
Quoted 3 lines
The change makes the check more intelligible in case of unimplemented EL3. Moreover,
arm_el_is_aa64()will abort with el argument == 3
if there is no EL3 in current configuration.
Yeah, I see why you've done it. But it's only "more intelligible" if you can match the code up with what the spec requires... As I say, I think it's correct, it's just not very obvious.
thanks
-- PMM