semihosting fixes
v3 cover semihosting fixes
Hi Peter,
I've re-based and re-tested on the post-decodetree changes. In the end I merged the A32/T32 patches and dropped Richard's r-b as the code has changed a bit.
Alex Bennée (3):
target/arm: handle M-profile semihosting at translate time target/arm: handle A-profile semihosting at translate time target/arm: remove run time semihosting checks
Emilio G. Cota (1):
atomic_template: fix indentation in GEN_ATOMIC_HELPER
accel/tcg/atomic_template.h | 2 +- target/arm/helper.c | 96 +++++++++---------------------------- target/arm/m_helper.c | 18 +++---- target/arm/translate.c | 30 ++++++++++-- 4 files changed, 54 insertions(+), 92 deletions(-)
Signature · 1 line
2.20.1
PATCH v3 1/4 target/arm: handle M-profile semihosting at translate time
We do this for other semihosting calls so we might as well do it for M-profile as well.
---
v2
- update for change to gen_exception_internal_insn API
v3
- update for decode tree
--- target/arm/m_helper.c | 18 ++++++------------ target/arm/translate.c | 11 ++++++++++- 2 files changed, 16 insertions(+), 13 deletions(-)
target/arm/m_helper.c +6 -12
@@ -2114,19 +2114,13 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs)break ; }break ; +case EXCP_SEMIHOST : +qemu_log_mask (CPU_LOG_INT , +"...handling as semihosting call 0x%x\n" , + env->regs[0 ]); + env->regs[0 ] =do_arm_semihosting (env); +return ;case EXCP_BKPT : -if (semihosting_enabled ()) { -int nr; - nr =arm_lduw_code (env, env->regs[15 ],arm_sctlr_b (env)) &0xff ; -if (nr ==0xab ) { - env->regs[15 ] +=2 ; -qemu_log_mask (CPU_LOG_INT , -"...handling as semihosting call 0x%x\n" , - env->regs[0 ]); - env->regs[0 ] =do_arm_semihosting (env); -return ; - } - }armv7m_nvic_set_pending (env->nvic,ARMV7M_EXCP_DEBUG , false);break ;case EXCP_IRQ :
target/arm/translate.c +10 -1
@@ -8424,7 +8424,16 @@ static bool trans_BKPT(DisasContext *s, arg_BKPT *a)if (!ENABLE_ARCH_5 ) {return false; } -gen_exception_bkpt_insn (s,syn_aa32_bkpt (a->imm, false)); + if (arm_dc_feature(s, ARM_FEATURE_M) && + semihosting_enabled() && +#ifndef CONFIG_USER_ONLY + s->current_el != 0 && +#endif + (a->imm == 0xab)) { + gen_exception_internal_insn(s, s->base.pc_next, EXCP_SEMIHOST); + } else { +gen_exception_bkpt_insn (s,syn_aa32_bkpt (a->imm, false)); + } return true; }
Signature · 1 line
2.20.1
Re: [PATCH v3 1/4] target/arm: handle M-profile semihosting at translate time
target/arm/translate.c static bool trans_BKPT(DisasContext *s, arg_BKPT *a) · line 8431
Quoted 5 lines · Alex Bennée
target/arm/translate.c +5
-0+ if (arm_dc_feature(s, ARM_FEATURE_M) && + semihosting_enabled() && +#ifndef CONFIG_USER_ONLY + s->current_el != 0 && +#endif
This last should be !IS_USER(s), no ifdef.
r~
PATCH v3 2/4 target/arm: handle A-profile semihosting at translate time
In reply to Alex BennéeAs for the other semihosting calls we can resolve this at translate time.
---
v2
- update for change to gen_exception_internal_insn API
v3
- update for decode tree, merge T32 & A32 commits
- dropped r-b due to changes
--- target/arm/translate.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
target/arm/translate.c +15 -4
@@ -10222,14 +10222,25 @@ static bool trans_CBZ(DisasContext *s, arg_CBZ *a) }/* -* Supervisor call +* Supervisor call - both T32 & A32 come here so we need to check +* which mode we are in when checking for semihosting. */ static bool trans_SVC (DisasContext *s,arg_SVC *a) { -gen_set_pc_im (s, s->base.pc_next); - s->svc_imm = a->imm; - s->base.is_jmp =DISAS_SWI ; +const uint32_t semihost_imm = s->thumb ?0xab :0x123456 ; + + if (semihosting_enabled() && +#ifndef CONFIG_USER_ONLY + s->current_el != 0 && +#endif + (a->imm == semihost_imm)) { + gen_exception_internal_insn(s, s->base.pc_next, EXCP_SEMIHOST); + } else { +gen_set_pc_im (s, s->base.pc_next); + s->svc_imm = a->imm; + s->base.is_jmp =DISAS_SWI ; + } return true; }
Signature · 1 line
2.20.1
Re: [PATCH v3 2/4] target/arm: handle A-profile semihosting at translate time
Quoted 39 lines · Alex Bennée
As for the other semihosting calls we can resolve this at translate time.
Signed-off-by Alex Bennée <[email protected]>---
v2
- update for change togen_exception_internal_insnAPI
v3
- update for decode tree, merge T32 & A32 commits
- dropped r-b due to changes--- target/arm/translate.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)target/arm/translate.c +7
-4@@ -10222,14 +10222,25 @@ static bool trans_CBZ(DisasContext *s, arg_CBZ *a) }/* -* Supervisor call +* Supervisor call - both T32 & A32 come here so we need to check +* which mode we are in when checking for semihosting. */ static bool trans_SVC (DisasContext *s,arg_SVC *a) { -gen_set_pc_im (s, s->base.pc_next); - s->svc_imm = a->imm; - s->base.is_jmp =DISAS_SWI ; +const uint32_t semihost_imm = s->thumb ?0xab :0x123456 ; + + if (semihosting_enabled() && +#ifndef CONFIG_USER_ONLY + s->current_el != 0 &&
target/arm/translate.c static bool trans_CBZ(DisasContext *s, arg_CBZ *a) · line 10245
Quoted 10 lines
target/arm/translate.c +8
-0+#endif + (a->imm == semihost_imm)) { +gen_exception_internal_insn (s, s->base.pc_next,EXCP_SEMIHOST ); + } else { +gen_set_pc_im (s, s->base.pc_next); + s->svc_imm = a->imm; + s->base.is_jmp =DISAS_SWI ; + }return true; }
Doesn't this accidentally enable semihosting via SVC for M-profile ?
thanks
-- PMM
Re: [PATCH v3 2/4] target/arm: handle A-profile semihosting at translate time
Quoted 45 lines · Peter Maydell
Quoted 44 lines · Alex Bennée
As for the other semihosting calls we can resolve this at translate time.
Signed-off-by Alex Bennée <[email protected]>---
v2
- update for change togen_exception_internal_insnAPI
v3
- update for decode tree, merge T32 & A32 commits
- dropped r-b due to changes--- target/arm/translate.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)target/arm/translate.c +12
-4@@ -10222,14 +10222,25 @@ static bool trans_CBZ(DisasContext *s, arg_CBZ *a) }/* -* Supervisor call +* Supervisor call - both T32 & A32 come here so we need to check +* which mode we are in when checking for semihosting. */ static bool trans_SVC (DisasContext *s,arg_SVC *a) { -gen_set_pc_im (s, s->base.pc_next); - s->svc_imm = a->imm; - s->base.is_jmp =DISAS_SWI ; +const uint32_t semihost_imm = s->thumb ?0xab :0x123456 ; + + if (semihosting_enabled() && +#ifndef CONFIG_USER_ONLY + s->current_el != 0 && +#endif + (a->imm == semihost_imm)) { + gen_exception_internal_insn(s, s->base.pc_next, EXCP_SEMIHOST); + } else { +gen_set_pc_im (s, s->base.pc_next);
Quoted 8 lines
target/arm/translate.c static bool trans_CBZ(DisasContext *s, arg_CBZ *a) · line 10245
Quoted 5 lines
target/arm/translate.c +3
-0+ s->svc_imm = a->imm; + s->base.is_jmp =DISAS_SWI ; + }return true; }Doesn't this accidentally enable semihosting via SVC for M-profile ?
We must have done that before then. Just gate it with && !arm_dc_feature(s, ARM_FEATURE_M) then?
Quoted 3 lines
thanks
-- PMM
--
Alex Bennée
Re: [PATCH v3 2/4] target/arm: handle A-profile semihosting at translate time
Quoted 11 lines · Alex Bennée
Quoted 5 lines · Peter Maydell
On Fri, 6 Sep 2019 at 13:47, Alex Bennée <[email protected]> wrote:
Doesn't this accidentally enable semihosting via SVC for M-profile ?
We must have done that before then.
No, we didn't do it before, because we were handling it in arm_cpu_do_interrupt(), which is A/R-profile only. The M-profile code goes via arm_v7m_cpu_do_interrupt() which doesn't check for semihosting when it sees an EXCP_SWI.
Quoted 1 line
Just gate it with && !arm_dc_feature(s,
ARM_FEATURE_M) then?
Yes, I think that's the right fix.
thanks
-- PMM
Re: [Qemu-devel] [PATCH v3 2/4] target/arm: handle A-profile semihosting at translate time
In reply to Alex Bennéetarget/arm/translate.c static bool trans_CBZ(DisasContext *s, arg_CBZ *a) · line 10236
Quoted 4 lines · Alex Bennée
target/arm/translate.c +4
-0+ if (semihosting_enabled() && +#ifndef CONFIG_USER_ONLY + s->current_el != 0 && +#endif
!IS_USER(s).
r~
PATCH v3 3/4 target/arm: remove run time semihosting checks
In reply to Alex BennéeNow we do all our checking and use a common EXCP_SEMIHOST for semihosting operations we can make helper code a lot simpler.
---
v2
- fix re-base conflicts
- hoist EXCP_SEMIHOST check
- comment cleanups
v5
- move CONFIG_TCG ifdefs
--- target/arm/helper.c | 96 +++++++++++---------------------------------- 1 file changed, 22 insertions(+), 74 deletions(-)
target/arm/helper.c +22 -74
@@ -8339,88 +8339,32 @@ static void arm_cpu_do_interrupt_aarch64(CPUState *cs) new_el, env->pc, pstate_read(env)); } -static inline bool check_for_semihosting (CPUState *cs) -{ +/* +* Do semihosting call and set the appropriate return value. All the +* permission and validity checks have been done at translate time. +* +* We only see semihosting exceptions in TCG only as they are not +* trapped to the hypervisor in KVM. +*/ #ifdef CONFIG_TCG -/* Check whether this exception is a semihosting call; if so -* then handle it and return true; otherwise return false. -*/ +static void handle_semihosting (CPUState *cs) +{ARMCPU *cpu =ARM_CPU (cs);CPUARMState *env = &cpu->env;if (is_a64 (env)) { -if (cs->exception_index ==EXCP_SEMIHOST ) { -/* This is always the 64-bit semihosting exception. -* The "is this usermode" and "is semihosting enabled" -* checks have been done at translate time. -*/ -qemu_log_mask (CPU_LOG_INT , -"...handling as semihosting call 0x%" PRIx64"\n" , - env->xregs[0 ]); - env->xregs[0 ] =do_arm_semihosting (env); -return true; - } -return false; +qemu_log_mask (CPU_LOG_INT , +"...handling as semihosting call 0x%" PRIx64"\n" , + env->xregs[0 ]); + env->xregs[0 ] =do_arm_semihosting (env); }else { -uint32_t imm; - -/* Only intercept calls from privileged modes, to provide some -* semblance of security. -*/ -if (cs->exception_index !=EXCP_SEMIHOST && - (!semihosting_enabled () || - ((env->uncached_cpsr &CPSR_M ) ==ARM_CPU_MODE_USR ))) { -return false; - } - -switch (cs->exception_index) { -case EXCP_SEMIHOST : -/* This is always a semihosting call; the "is this usermode" -* and "is semihosting enabled" checks have been done at -* translate time. -*/ -break ; -case EXCP_SWI : -/* Check for semihosting interrupt. */ -if (env->thumb) { - imm =arm_lduw_code (env, env->regs[15 ] -2 ,arm_sctlr_b (env)) - &0xff ; -if (imm ==0xab ) { -break ; - } - }else { - imm =arm_ldl_code (env, env->regs[15 ] -4 ,arm_sctlr_b (env)) - &0xffffff ; -if (imm ==0x123456 ) { -break ; - } - } -return false; -case EXCP_BKPT : -/* See if this is a semihosting syscall. */ -if (env->thumb) { - imm =arm_lduw_code (env, env->regs[15 ],arm_sctlr_b (env)) - &0xff ; -if (imm ==0xab ) { - env->regs[15 ] +=2 ; -break ; - } - } -return false; -default : -return false; - } -qemu_log_mask (CPU_LOG_INT ,"...handling as semihosting call 0x%x\n" , env->regs[0 ]); env->regs[0 ] =do_arm_semihosting (env); -return true; } -#else -return false; -#endif } +#endif /* Handle a CPU exception for A and R profile CPUs. * Do any appropriate logging, handle PSCI calls, and then hand off
@@ -8451,13 +8395,17 @@ void arm_cpu_do_interrupt(CPUState *cs)return ; } -/* Semihosting semantics depend on the register width of the -* code that caused the exception, not the target exception level, -* so must be handled here. +/* +* Semihosting semantics depend on the register width of the code +* that caused the exception, not the target exception level, so +* must be handled here. */ -if (check_for_semihosting (cs)) { +#ifdef CONFIG_TCG +if (cs->exception_index ==EXCP_SEMIHOST ) { +handle_semihosting (cs);return ; } +#endif /* Hooks may change global state so BQL should be held, also the * BQL needs to be held for any modification of
Signature · 1 line
2.20.1
PATCH v3 4/4 atomic_template: fix indentation in GEN_ATOMIC_HELPER
In reply to Alex BennéeFrom: "Emilio G. Cota" <[email protected]>
--- accel/tcg/atomic_template.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
accel/tcg/atomic_template.h +1 -1
@@ -149,7 +149,7 @@ ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,#define GEN_ATOMIC_HELPER (X ) \ ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr, \ - ABI_TYPE val EXTRA_ARGS) \ + ABI_TYPE val EXTRA_ARGS) \ { \ ATOMIC_MMU_DECLS; \ DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP; \
Signature · 1 line
2.20.1