target-arm: fix CPU breakpoint handling
PATCH 1/1 target-arm: fix CPU breakpoint handling
A QEMU breakpoint match is not definitely an architectural breakpoint match. If an exception is generated unconditionally during translation, it is hardly possible to ignore it in the debug exceptoin hanlder.
Generate a call to helper to check CPU breakpoints and raise an exception only if any breakpoint architecturally matches.
--- target-arm/helper.h | 2 ++ target-arm/op_helper.c | 20 +++++++++++++++++++- target-arm/translate-a64.c | 12 +++++++----- target-arm/translate.c | 12 +++++++----- 4 files changed, 35 insertions(+), 11 deletions(-)
target-arm/helper.h +2 -0
@@ -54,6 +54,8 @@ DEF_HELPER_1(yield, void, env) DEF_HELPER_1(pre_hvc, void, env) DEF_HELPER_2(pre_smc, void, env, i32) +DEF_HELPER_1(check_breakpoints, void, env) + DEF_HELPER_3(cpsr_write, void, env, i32, i32)DEF_HELPER_1 (cpsr_read, i32, env)
target-arm/op_helper.c +19 -1
@@ -877,6 +877,15 @@ bool arm_debug_check_watchpoint(CPUState *cs)return check_watchpoints (cpu); } +void HELPER (check_breakpoints )(CPUARMState *env) +{ +ARMCPU *cpu =arm_env_get_cpu (env); + +if (check_breakpoints (cpu)) { +HELPER (exception_internal (env,EXCP_DEBUG )); + } +} +void arm_debug_excp_handler (CPUState *cs) {/* Called by core code when a watchpoint or breakpoint fires;
@@ -904,7 +913,16 @@ void arm_debug_excp_handler(CPUState *cs) arm_debug_target_el(env)); } }else { -if (check_breakpoints (cpu)) { +CPUBreakpoint *bp; +uint64_t pc =is_a64 (env) ? env->pc : env->regs[15 ]; + + QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { +if (bp->pc == pc && !(bp->flags &BP_CPU )) { +return ; + } + } + + {bool same_el = (arm_debug_target_el (env) ==arm_current_el (env));if (extended_addresses_enabled (env)) { env->exception.fsr = (1 <<9 ) |0x22 ;
target-arm/translate-a64.c +7 -5
@@ -11000,11 +11000,13 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,if (unlikely (!QTAILQ_EMPTY (&cs->breakpoints))) { QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {if (bp->pc == dc->pc) { -gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); -/* Advance PC so that clearing the breakpoint will -invalidate this TB. */ - dc->pc +=2 ; - goto done_generating; +if (bp->flags &BP_CPU ) { +gen_helper_check_breakpoints (cpu_env); +break ; + }else { +gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); + goto done_generating; + } } } }
target-arm/translate.c +7 -5
@@ -11292,11 +11292,13 @@ static inline void gen_intermediate_code_internal(ARMCPU *cpu,if (unlikely (!QTAILQ_EMPTY (&cs->breakpoints))) { QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {if (bp->pc == dc->pc) { -gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); -/* Advance PC so that clearing the breakpoint will -invalidate this TB. */ - dc->pc +=2 ; - goto done_generating; +if (bp->flags &BP_CPU ) { +gen_helper_check_breakpoints (cpu_env); +break ; + }else { +gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); + goto done_generating; + } } } }
Signature · 1 line
1.9.1
Quoted 3 lines · Sergey Fedorov
A QEMU breakpoint match is not definitely an architectural breakpoint match. If an exception is generated unconditionally during translation, it is hardly possible to ignore it in the debug exceptoin hanlder.
"exception".
Quoted 48 lines
Generate a call to helper to check CPU breakpoints and raise an exception only if any breakpoint architecturally matches.
Signed-off-by Sergey Fedorov <[email protected]>--- target-arm/helper.h | 2 ++ target-arm/op_helper.c | 20 +++++++++++++++++++- target-arm/translate-a64.c | 12 +++++++----- target-arm/translate.c | 12 +++++++----- 4 files changed, 35 insertions(+), 11 deletions(-)target-arm/helper.h +2
-0@@ -54,6 +54,8 @@ DEF_HELPER_1(yield, void, env) DEF_HELPER_1(pre_hvc, void, env) DEF_HELPER_2(pre_smc, void, env, i32) +DEF_HELPER_1(check_breakpoints, void, env) + DEF_HELPER_3(cpsr_write, void, env, i32, i32)DEF_HELPER_1 (cpsr_read, i32, env)target-arm/op_helper.c +9
-0@@ -877,6 +877,15 @@ bool arm_debug_check_watchpoint(CPUState *cs)return check_watchpoints (cpu); } +void HELPER (check_breakpoints )(CPUARMState *env) +{ +ARMCPU *cpu =arm_env_get_cpu (env); + +if (check_breakpoints (cpu)) { +HELPER (exception_internal (env,EXCP_DEBUG )); + } +} +void arm_debug_excp_handler (CPUState *cs) {/* Called by core code when a watchpoint or breakpoint fires; @@ -904,7 +913,16 @@ void arm_debug_excp_handler(CPUState *cs) arm_debug_target_el(env)); }
target-arm/op_helper.c void arm_debug_excp_handler(CPUState *cs) · line 923
Quoted 10 lines
target-arm/op_helper.c +8
-1}else { -if (check_breakpoints (cpu)) { +CPUBreakpoint *bp; +uint64_t pc =is_a64 (env) ? env->pc : env->regs[15 ]; + + QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { +if (bp->pc == pc && !(bp->flags &BP_CPU )) { +return ; + } + }
This extra code looks right, but isn't it fixing a different bug?
target-arm/op_helper.c void arm_debug_excp_handler(CPUState *cs) · line 925
Quoted 2 lines
target-arm/op_helper.c +2
-0+ + {
Rather than adding the extra block I would just de-indent the code that used to live inside the if() and move the variable declaration to the top of the new block.
Quoted 13 lines
target-arm/op_helper.c +0
-0bool same_el = (arm_debug_target_el (env) ==arm_current_el (env));if (extended_addresses_enabled (env)) { env->exception.fsr = (1 <<9 ) |0x22 ;target-arm/translate-a64.c +0
-2@@ -11000,11 +11000,13 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,if (unlikely (!QTAILQ_EMPTY (&cs->breakpoints))) { QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {if (bp->pc == dc->pc) { -gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); -/* Advance PC so that clearing the breakpoint will
target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11009
Quoted 10 lines
target-arm/translate-a64.c +7
-3-invalidate this TB. */ - dc->pc +=2 ; - goto done_generating; +if (bp->flags &BP_CPU ) { +gen_helper_check_breakpoints (cpu_env); +break ; + }else { +gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); + goto done_generating; + }
You seem to have dropped the "advance the PC" code -- why is that ok?
thanks
-- PMM
Quoted 5 lines · Peter Maydell
Quoted 3 lines · Sergey Fedorov
A QEMU breakpoint match is not definitely an architectural breakpoint match. If an exception is generated unconditionally during translation, it is hardly possible to ignore it in the debug exceptoin hanlder.
"exception".
Will fix it, thanks.
Quoted 51 lines
Quoted 51 lines
Generate a call to helper to check CPU breakpoints and raise an exception only if any breakpoint architecturally matches.
Signed-off-by Sergey Fedorov <[email protected]>--- target-arm/helper.h | 2 ++ target-arm/op_helper.c | 20 +++++++++++++++++++- target-arm/translate-a64.c | 12 +++++++----- target-arm/translate.c | 12 +++++++----- 4 files changed, 35 insertions(+), 11 deletions(-)target-arm/helper.h +2
-0@@ -54,6 +54,8 @@ DEF_HELPER_1(yield, void, env) DEF_HELPER_1(pre_hvc, void, env) DEF_HELPER_2(pre_smc, void, env, i32) +DEF_HELPER_1(check_breakpoints, void, env) + DEF_HELPER_3(cpsr_write, void, env, i32, i32)DEF_HELPER_1 (cpsr_read, i32, env)target-arm/op_helper.c +10
-1@@ -877,6 +877,15 @@ bool arm_debug_check_watchpoint(CPUState *cs)return check_watchpoints (cpu); } +void HELPER (check_breakpoints )(CPUARMState *env) +{ +ARMCPU *cpu =arm_env_get_cpu (env); + +if (check_breakpoints (cpu)) { +HELPER (exception_internal (env,EXCP_DEBUG )); + } +} +void arm_debug_excp_handler (CPUState *cs) {/* Called by core code when a watchpoint or breakpoint fires; @@ -904,7 +913,16 @@ void arm_debug_excp_handler(CPUState *cs) arm_debug_target_el(env)); } }else { -if (check_breakpoints (cpu)) { +CPUBreakpoint *bp;
Quoted 8 lines
target-arm/op_helper.c void arm_debug_excp_handler(CPUState *cs) · line 923
Quoted 7 lines
target-arm/op_helper.c +7
-0+uint64_t pc =is_a64 (env) ? env->pc : env->regs[15 ]; + + QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { +if (bp->pc == pc && !(bp->flags &BP_CPU )) { +return ; + } + }This extra code looks right, but isn't it fixing a different bug?
You are right, it would better come to separate patch.
Quoted 6 lines
target-arm/op_helper.c void arm_debug_excp_handler(CPUState *cs) · line 925
Quoted 3 lines
target-arm/op_helper.c +2
-0+ + {Rather than adding the extra block I would just de-indent the code that used to live inside the
if()and move the variable declaration to the top of the new block.
Okay, I will adjust the code.
Quoted 17 lines
Quoted 17 lines
target-arm/op_helper.c +0
-0bool same_el = (arm_debug_target_el (env) ==arm_current_el (env));if (extended_addresses_enabled (env)) { env->exception.fsr = (1 <<9 ) |0x22 ;target-arm/translate-a64.c +0
-5@@ -11000,11 +11000,13 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,if (unlikely (!QTAILQ_EMPTY (&cs->breakpoints))) { QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {if (bp->pc == dc->pc) { -gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); -/* Advance PC so that clearing the breakpoint will -invalidate this TB. */ - dc->pc +=2 ; - goto done_generating;
Quoted 9 lines
target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11009
Quoted 7 lines
target-arm/translate-a64.c +7
-0+if (bp->flags &BP_CPU ) { +gen_helper_check_breakpoints (cpu_env); +break ; + }else { +gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); + goto done_generating; + }You seem to have dropped the "advance the PC" code -- why is that ok?
I also dropped the immediately following goto statement. With these changes PC is advanced in the same way as it happens during normal translation. That is because we actually have to do the instruction translation process here to support the case when a breakpoint with matching PC is architecturally mismatched. As I understand, that "advance the PC" code was necessary to produce a TB with non-zero size so that it can be invalidated later when we clear the breakpoint.
Best,
Sergey
Quoted 24 lines · Sergey Fedorov
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Sergey Fedorov
target-arm/translate-a64.c +0
-5@@ -11000,11 +11000,13 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,if (unlikely (!QTAILQ_EMPTY (&cs->breakpoints))) { QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {if (bp->pc == dc->pc) { -gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); -/* Advance PC so that clearing the breakpoint will -invalidate this TB. */ - dc->pc +=2 ; - goto done_generating;Quoted 9 lines
target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11009
Quoted 7 lines
target-arm/translate-a64.c +7
-0+if (bp->flags &BP_CPU ) { +gen_helper_check_breakpoints (cpu_env); +break ; + }else { +gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); + goto done_generating; + }You seem to have dropped the "advance the PC" code -- why is that ok?
Quoted 7 lines
I also dropped the immediately following goto statement. With these changes PC is advanced in the same way as it happens during normal translation. That is because we actually have to do the instruction translation process here to support the case when a breakpoint with matching PC is architecturally mismatched. As I understand, that "advance the PC" code was necessary to produce a TB with non-zero size so that it can be invalidated later when we clear the breakpoint.
OK, that makes sense for the BP_CPU case but you still have the "goto done_generating;" in the else clause...
Also, should we maybe make this TB be only one insn long even for the BP_CPU case? It seems like in the common case we will only execute one insn.
thanks
-- PMM
Quoted 30 lines · Peter Maydell
Quoted 22 lines · Sergey Fedorov
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Sergey Fedorov
target-arm/translate-a64.c +0
-5@@ -11000,11 +11000,13 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,if (unlikely (!QTAILQ_EMPTY (&cs->breakpoints))) { QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {if (bp->pc == dc->pc) { -gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); -/* Advance PC so that clearing the breakpoint will -invalidate this TB. */ - dc->pc +=2 ; - goto done_generating;Quoted 9 lines
target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11009
Quoted 7 lines
target-arm/translate-a64.c +7
-0+if (bp->flags &BP_CPU ) { +gen_helper_check_breakpoints (cpu_env); +break ; + }else { +gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); + goto done_generating; + }You seem to have dropped the "advance the PC" code -- why is that ok?
Quoted 7 lines
I also dropped the immediately following goto statement. With these changes PC is advanced in the same way as it happens during normal translation. That is because we actually have to do the instruction translation process here to support the case when a breakpoint with matching PC is architecturally mismatched. As I understand, that "advance the PC" code was necessary to produce a TB with non-zero size so that it can be invalidated later when we clear the breakpoint.
Quoted 7 lines
OK, that makes sense for the
BP_CPUcase but you still have the "gotodone_generating;" in the else clause...Also, should we maybe make this TB be only one insn long even for the
BP_CPUcase? It seems like in the common case we will only execute one insn.
Right, I have to fix this PC advancement. But I can't think of why we will only execute one insn...
Best,
Sergey
Quoted 34 lines · Sergey Fedorov
Quoted 33 lines · Peter Maydell
Quoted 22 lines · Sergey Fedorov
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Sergey Fedorov
target-arm/translate-a64.c +0
-5@@ -11000,11 +11000,13 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,if (unlikely (!QTAILQ_EMPTY (&cs->breakpoints))) { QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {if (bp->pc == dc->pc) { -gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); -/* Advance PC so that clearing the breakpoint will -invalidate this TB. */ - dc->pc +=2 ; - goto done_generating;Quoted 9 lines
target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11009
Quoted 7 lines
target-arm/translate-a64.c +7
-0+if (bp->flags &BP_CPU ) { +gen_helper_check_breakpoints (cpu_env); +break ; + }else { +gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); + goto done_generating; + }You seem to have dropped the "advance the PC" code -- why is that ok?
Quoted 7 lines
I also dropped the immediately following goto statement. With these changes PC is advanced in the same way as it happens during normal translation. That is because we actually have to do the instruction translation process here to support the case when a breakpoint with matching PC is architecturally mismatched. As I understand, that "advance the PC" code was necessary to produce a TB with non-zero size so that it can be invalidated later when we clear the breakpoint.
OK, that makes sense for the
BP_CPUcase but you still have the "gotodone_generating;" in the else clause...
Quoted 7 lines
Quoted 4 lines
Also, should we maybe make this TB be only one insn long even for the
BP_CPUcase? It seems like in the common case we will only execute one insn.Right, I have to fix this PC advancement. But I can't think of why we will only execute one insn...
Well, typically we'll take the BP exception in the helper function. There's nothing inherently wrong with translating further code after this insn, but it's usually not going to be executed, so we might as well end the TB early.
thanks
-- PMM
Quoted 39 lines · Peter Maydell
Quoted 38 lines · Sergey Fedorov
Quoted 37 lines · Peter Maydell
Quoted 22 lines · Sergey Fedorov
Quoted 12 lines · Peter Maydell
Quoted 11 lines · Sergey Fedorov
target-arm/translate-a64.c +0
-5@@ -11000,11 +11000,13 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,if (unlikely (!QTAILQ_EMPTY (&cs->breakpoints))) { QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {if (bp->pc == dc->pc) { -gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); -/* Advance PC so that clearing the breakpoint will -invalidate this TB. */ - dc->pc +=2 ; - goto done_generating;Quoted 9 lines
target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11009
Quoted 7 lines
target-arm/translate-a64.c +7
-0+if (bp->flags &BP_CPU ) { +gen_helper_check_breakpoints (cpu_env); +break ; + }else { +gen_exception_internal_insn (dc,0 ,EXCP_DEBUG ); + goto done_generating; + }You seem to have dropped the "advance the PC" code -- why is that ok?
Quoted 7 lines
I also dropped the immediately following goto statement. With these changes PC is advanced in the same way as it happens during normal translation. That is because we actually have to do the instruction translation process here to support the case when a breakpoint with matching PC is architecturally mismatched. As I understand, that "advance the PC" code was necessary to produce a TB with non-zero size so that it can be invalidated later when we clear the breakpoint.
OK, that makes sense for the
BP_CPUcase but you still have the "gotodone_generating;" in the else clause...Also, should we maybe make this TB be only one insn long even for the
BP_CPUcase? It seems like in the common case we will only execute one insn.
Quoted 7 lines
Quoted 2 lines
Right, I have to fix this PC advancement. But I can't think of why we will only execute one insn...
Well, typically we'll take the BP exception in the helper function. There's nothing inherently wrong with translating further code after this insn, but it's usually not going to be executed, so we might as well end the TB early.
Thank, it is clear now. What about getting rid of "goto done generating" and always translate one insn to update PC accordingly? Sometimes in_asm log complains about that when disassembler and translator disagree about the instruction size.
Best,
Sergey
Quoted 18 lines · Sergey Fedorov
Quoted 15 lines · Peter Maydell
On 14 September 2015 at 11:51, Sergey Fedorov <[email protected]> wrote:
target-arm/op_helper.c +8
-1@@ -904,7 +913,16 @@ void arm_debug_excp_handler(CPUState *cs) arm_debug_target_el(env)); } }else { -if (check_breakpoints (cpu)) { +CPUBreakpoint *bp; +uint64_t pc =is_a64 (env) ? env->pc : env->regs[15 ]; + + QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { +if (bp->pc == pc && !(bp->flags &BP_CPU )) { +return ; + } + }Quoted 1 line
This extra code looks right, but isn't it fixing a different bug?
You are right, it would better come to separate patch.
Actually, I can't think of it as a separate patch. This change is really required only if we remove check_breakpoints() here. Otherwise check_breakpoints() calls bp_wp_matches() which do the necessary check.
Best regards,
Sergey
Quoted 18 lines · Sergey Fedorov
Quoted 17 lines · Sergey Fedorov
Quoted 14 lines · Peter Maydell
On 14 September 2015 at 11:51, Sergey Fedorov <[email protected]> wrote:
target-arm/op_helper.c +8
-1@@ -904,7 +913,16 @@ void arm_debug_excp_handler(CPUState *cs) arm_debug_target_el(env)); } }else { -if (check_breakpoints (cpu)) { +CPUBreakpoint *bp; +uint64_t pc =is_a64 (env) ? env->pc : env->regs[15 ]; + + QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { +if (bp->pc == pc && !(bp->flags &BP_CPU )) { +return ; + } + }Quoted 1 line
This extra code looks right, but isn't it fixing a different bug?
You are right, it would better come to separate patch.
Quoted 4 lines
Actually, I can't think of it as a separate patch. This change is really required only if we remove
check_breakpoints()here. Otherwisecheck_breakpoints()callsbp_wp_matches()which do the necessary check.
...but considering the order of breakpoint enumeration it is not so simple. The difference is when we have GDB and CPU breakpoint to the same address. In this case check_breakpoints() returns true, but we should handle GDB breakpoints first. Sorry for my misunderstanding, I will split this patch as you suggested.
Best regards,
Sergey