Skip to contentSkip to conversation

target-arm: Fix breakpoint handling

PATCH Superseded Sergey Fedorov 2 participants 15 messages Sep 28, 2015 – Oct 12, 2015

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover target-arm: Fix breakpoint handling

This series is intended to fix ARM breakpoint emulation misbehavior. QEMU hangs when QEMU breakpoint fires but it does not pass additional architectural checks in ARM CPU debug exception handler. For details, please see individual patches. The most relevant parts of the original discussion about ARM breakpoint emulation misbehavior can be found at:
https://lists.gnu.org/archive/html/qemu-devel/2015-08/msg02715.html

Changes in v2:
* The initial commit split into two parts
* Extra block eliminated in arm_debug_excp_handler()
* Just one instruction always translated in case of breakpoint PC match

Sergey Fedorov (2):
target-arm: Fix GDB breakpoint handling
target-arm: Fix CPU breakpoint handling

 target-arm/helper.h        |  2 ++
 target-arm/op_helper.c     | 36 ++++++++++++++++++++++++++----------
 target-arm/translate-a64.c | 14 ++++++++------
 target-arm/translate.c     | 13 ++++++++-----
 4 files changed, 44 insertions(+), 21 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/2 target-arm: Fix GDB breakpoint handling

GDB breakpoints have higher priority so they have to be checked first. Should GDB breakpoint match, just return from the debug exception handler.

---
 target-arm/op_helper.c | 9 +++++++++
 1 file changed, 9 insertions(+)
target-arm/op_helper.c +9 -0
index 1425a1d..1d4d8cb 100644
@@ -897,6 +897,15 @@ void arm_debug_excp_handler(CPUState *cs)
             }
         }
     } else {
+        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;
+            }
+        }
+
         if (check_breakpoints(cpu)) {
             bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
             if (extended_addresses_enabled(env)) {
Signature · 1 line
1.9.1
#3 lore raw

PATCH v2 2/2 target-arm: Fix CPU breakpoint handling

In reply to 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 exception handler.

Generate a call to a helper to check CPU breakpoints and raise an exception only if any breakpoint matches architecturally.

---
 target-arm/helper.h        |  2 ++
 target-arm/op_helper.c     | 29 ++++++++++++++++++-----------
 target-arm/translate-a64.c | 14 ++++++++------
 target-arm/translate.c     | 13 ++++++++-----
 4 files changed, 36 insertions(+), 22 deletions(-)
target-arm/helper.h +2 -0
index 827b33d..c2a85c7 100644
@@ -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 +18 -11
index 1d4d8cb..8ec8590 100644
@@ -867,6 +867,15 @@ static bool check_breakpoints(ARMCPU *cpu)
     return false;
 }
 
+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;
@@ -899,6 +908,7 @@ void arm_debug_excp_handler(CPUState *cs)
     } else {
         CPUBreakpoint *bp;
         uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
+        bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
 
         QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
             if (bp->pc == pc && !(bp->flags & BP_CPU)) {
@@ -906,18 +916,15 @@ void arm_debug_excp_handler(CPUState *cs)
             }
         }
 
-        if (check_breakpoints(cpu)) {
-            bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
-            if (extended_addresses_enabled(env)) {
-                env->exception.fsr = (1 << 9) | 0x22;
-            } else {
-                env->exception.fsr = 0x2;
-            }
-            /* FAR is UNKNOWN, so doesn't need setting */
-            raise_exception(env, EXCP_PREFETCH_ABORT,
-                            syn_breakpoint(same_el),
-                            arm_debug_target_el(env));
+        if (extended_addresses_enabled(env)) {
+            env->exception.fsr = (1 << 9) | 0x22;
+        } else {
+            env->exception.fsr = 0x2;
         }
+        /* FAR is UNKNOWN, so doesn't need setting */
+        raise_exception(env, EXCP_PREFETCH_ABORT,
+                        syn_breakpoint(same_el),
+                        arm_debug_target_el(env));
     }
 }
 
target-arm/translate-a64.c +8 -6
index ec0936c..426229f 100644
@@ -11082,11 +11082,14 @@ 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);
+                    } else {
+                        gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
+                    }
+                    /* End the TB early; it's likely not going to be executed */
+                    dc->is_jmp = DISAS_UPDATE;
+                    break;
                 }
             }
         }
@@ -11209,7 +11212,6 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,
         }
     }
 
-done_generating:
     gen_tb_end(tb, num_insns);
 
 #ifdef DEBUG_DISAS
target-arm/translate.c +8 -5
index 84a21ac..405d6d0 100644
@@ -11328,11 +11328,14 @@ 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);
+                    } else {
+                        gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
+                    }
+                    /* End the TB early; it's likely not going to be executed */
+                    dc->is_jmp = DISAS_UPDATE;
+                    break;
                 }
             }
         }
Signature · 1 line
1.9.1
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] target-arm: Fix GDB breakpoint handling

In reply to Sergey Fedorov
Quoted 16 lines · Sergey Fedorov

GDB breakpoints have higher priority so they have to be checked first. Should GDB breakpoint match, just return from the debug exception handler.

---
 target-arm/op_helper.c | 9 +++++++++
 1 file changed, 9 insertions(+)
target-arm/op_helper.c +0 -0
index 1425a1d..1d4d8cb 100644
@@ -897,6 +897,15 @@ void arm_debug_excp_handler(CPUState *cs)
             }
         }

target-arm/op_helper.c void arm_debug_excp_handler(CPUState *cs) · line 908

Quoted 10 lines
target-arm/op_helper.c +9 -0
     } else {
+        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;
+            }
+        }
+

In current master you can write this more simply as

    if (cpu_breakpoint_test(cs, pc, BP_GDB)) {
        return;
    }

since rth's patchset introduced that utility function.

thanks
-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

In reply to Sergey Fedorov
Quoted 16 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 exception handler.

Generate a call to a helper to check CPU breakpoints and raise an exception only if any breakpoint matches architecturally.

target-arm/translate-a64.c +0 -0
index ec0936c..426229f 100644
@@ -11082,11 +11082,14 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,
         if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
             QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {

target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11088

Quoted 10 lines
target-arm/translate-a64.c +4 -5
                 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);
+                    } else {
+                        gen_exception_internal_insn(dc, 0, EXCP_DEBUG);

We shouldn't just continue here, because now we'll try to generate the code for the instruction even in the "we know this will always be a bp" case. Also, you've dropped the "advance PC" part which we need so this TB is not zero length.

target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11091

Quoted 3 lines
target-arm/translate-a64.c +3 -0
+                    }
+                    /* End the TB early; it's likely not going to be executed */
+                    dc->is_jmp = DISAS_UPDATE;

gen_exception_internal_insn sets is_jmp to DISAS_EXC, and then this overrides it; so this line should go inside the "is this a BP_CPU bp?" if clause. (I think the effect is just that we pointlessly generate some unreachable code after the exception generating call.)

Quoted 22 lines
target-arm/translate-a64.c +1 -1
+                    break;
                 }
             }
         }
@@ -11209,7 +11212,6 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,
         }
     }
 
-done_generating:
     gen_tb_end(tb, num_insns);
 
 #ifdef DEBUG_DISAS
target-arm/translate.c +0 -2
index 84a21ac..405d6d0 100644
@@ -11328,11 +11328,14 @@ 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

target-arm/translate.c static inline void gen_intermediate_code_internal(ARMCPU *cpu, · line 11337

Quoted 10 lines
target-arm/translate.c +7 -3
-                       invalidate this TB.  */
-                    dc->pc += 2;
-                    goto done_generating;
+                    if (bp->flags & BP_CPU) {
+                        gen_helper_check_breakpoints(cpu_env);
+                    } else {
+                        gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
+                    }
+                    /* End the TB early; it's likely not going to be executed */
+                    dc->is_jmp = DISAS_UPDATE;

Similar comments here.

Annoying corner case which I don't think we need to handle necessarily:
if you set a breakpoint on a 32-bit Thumb instruction which spans a page boundary, and the second page is not present, we will end up taking the page fault when I think we should take the breakpoint. I can't think of a way to get that right, so just commenting that it isn't handled right would do.

Quoted 7 lines
target-arm/translate.c +1 -1
+                    break;
                 }
             }
         }
--

1.9.1

Otherwise I think this is the right approach.

thanks
-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

Quoted 22 lines · Peter Maydell
Quoted 21 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 exception handler.

Generate a call to a helper to check CPU breakpoints and raise an exception only if any breakpoint matches architecturally.

target-arm/translate-a64.c +0 -5
index ec0936c..426229f 100644
@@ -11082,11 +11082,14 @@ 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 8 lines

target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11088

Quoted 4 lines
target-arm/translate-a64.c +4 -0
+                    if (bp->flags & BP_CPU) {
+                        gen_helper_check_breakpoints(cpu_env);
+                    } else {
+                        gen_exception_internal_insn(dc, 0, EXCP_DEBUG);

We shouldn't just continue here, because now we'll try to generate the code for the instruction even in the "we know this will always be a bp" case. Also, you've dropped the "advance PC" part which we need so this TB is not zero length.

Actually, I was going to do the same way as some architectures (e.g. alpha) did: always translate one instruction so that TB size is determined by actual instruction decoding. At least, it makes sense for AArch32 where we can have 16/32-bit insns. If we advance PC incorrectly, we will get "Disassembler disagrees with translator over instruction decoding" warning messages when in_asm log enabled. I can rewrite it with PC advancement, but at least, I would like to change the advancement to 4 bytes for A64 translation.

Best regards,
Sergey

Quoted 61 lines

target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11091

Quoted 4 lines
target-arm/translate-a64.c +3 -0
+                    }
+                    /* End the TB early; it's likely not going to be executed */
+                    dc->is_jmp = DISAS_UPDATE;

gen_exception_internal_insn sets is_jmp to DISAS_EXC, and then this overrides it; so this line should go inside the "is this a BP_CPU bp?" if clause. (I think the effect is just that we pointlessly generate some unreachable code after the exception generating call.)

Quoted 22 lines
target-arm/translate-a64.c +1 -1
+                    break;
                 }
             }
         }
@@ -11209,7 +11212,6 @@ void gen_intermediate_code_internal_a64(ARMCPU *cpu,
         }
     }
 
-done_generating:
     gen_tb_end(tb, num_insns);
 
 #ifdef DEBUG_DISAS
target-arm/translate.c +0 -2
index 84a21ac..405d6d0 100644
@@ -11328,11 +11328,14 @@ 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

target-arm/translate.c static inline void gen_intermediate_code_internal(ARMCPU *cpu, · line 11337

Quoted 10 lines
target-arm/translate.c +7 -3
-                       invalidate this TB.  */
-                    dc->pc += 2;
-                    goto done_generating;
+                    if (bp->flags & BP_CPU) {
+                        gen_helper_check_breakpoints(cpu_env);
+                    } else {
+                        gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
+                    }
+                    /* End the TB early; it's likely not going to be executed */
+                    dc->is_jmp = DISAS_UPDATE;

Similar comments here.

Annoying corner case which I don't think we need to handle necessarily:
if you set a breakpoint on a 32-bit Thumb instruction which spans a page boundary, and the second page is not present, we will end up taking the page fault when I think we should take the breakpoint. I can't think of a way to get that right, so just commenting that it isn't handled right would do.

Quoted 7 lines
target-arm/translate.c +1 -1
+                    break;
                 }
             }
         }
--

1.9.1

Otherwise I think this is the right approach.

thanks
-- PMM

#8 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

Annoying corner case which I don't think we need to handle necessarily:
if you set a breakpoint on a 32-bit Thumb instruction which spans a page boundary, and the second page is not present, we will end up taking the page fault when I think we should take the breakpoint. I can't think of a way to get that right, so just commenting that it isn't handled right would do.

Could you please point out the piece of code which will generate the page fault? Maybe I will give it a thought :)

Best regards,
Sergey

#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

In reply to Sergey Fedorov
Quoted 32 lines · Sergey Fedorov
Quoted 22 lines · Peter Maydell
Quoted 21 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 exception handler.

Generate a call to a helper to check CPU breakpoints and raise an exception only if any breakpoint matches architecturally.

target-arm/translate-a64.c +0 -5
index ec0936c..426229f 100644
@@ -11082,11 +11082,14 @@ 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 8 lines

target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11088

Quoted 4 lines
target-arm/translate-a64.c +4 -0
+                    if (bp->flags & BP_CPU) {
+                        gen_helper_check_breakpoints(cpu_env);
+                    } else {
+                        gen_exception_internal_insn(dc, 0, EXCP_DEBUG);

We shouldn't just continue here, because now we'll try to generate the code for the instruction even in the "we know this will always be a bp" case. Also, you've dropped the "advance PC" part which we need so this TB is not zero length.

Quoted 8 lines

Actually, I was going to do the same way as some architectures (e.g. alpha) did: always translate one instruction so that TB size is determined by actual instruction decoding. At least, it makes sense for AArch32 where we can have 16/32-bit insns. If we advance PC incorrectly, we will get "Disassembler disagrees with translator over instruction decoding" warning messages when in_asm log enabled. I can rewrite it with PC advancement, but at least, I would like to change the advancement to 4 bytes for A64 translation.

Hmm, I see. I'm not sure it makes sense to do a bunch of extra work at codegen just to avoid a debug message. It would be nicer to suppress that warning some other way.

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

Quoted 40 lines · Peter Maydell
Quoted 31 lines · Sergey Fedorov
Quoted 22 lines · Peter Maydell
Quoted 21 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 exception handler.

Generate a call to a helper to check CPU breakpoints and raise an exception only if any breakpoint matches architecturally.

target-arm/translate-a64.c +0 -5
index ec0936c..426229f 100644
@@ -11082,11 +11082,14 @@ 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 8 lines

target-arm/translate-a64.c void gen_intermediate_code_internal_a64(ARMCPU *cpu, · line 11088

Quoted 4 lines
target-arm/translate-a64.c +4 -0
+                    if (bp->flags & BP_CPU) {
+                        gen_helper_check_breakpoints(cpu_env);
+                    } else {
+                        gen_exception_internal_insn(dc, 0, EXCP_DEBUG);

We shouldn't just continue here, because now we'll try to generate the code for the instruction even in the "we know this will always be a bp" case. Also, you've dropped the "advance PC" part which we need so this TB is not zero length.

Quoted 8 lines

Actually, I was going to do the same way as some architectures (e.g. alpha) did: always translate one instruction so that TB size is determined by actual instruction decoding. At least, it makes sense for AArch32 where we can have 16/32-bit insns. If we advance PC incorrectly, we will get "Disassembler disagrees with translator over instruction decoding" warning messages when in_asm log enabled. I can rewrite it with PC advancement, but at least, I would like to change the advancement to 4 bytes for A64 translation.

Quoted 4 lines

Hmm, I see. I'm not sure it makes sense to do a bunch of extra work at codegen just to avoid a debug message. It would be nicer to suppress that warning some other way.

I think we could try figuring out the actual instruction length in case of Thumb instruction, i.e. to do partial instruction decoding.

#9 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

In reply to Sergey Fedorov
Quoted 10 lines · Sergey Fedorov
Quoted 6 lines · Peter Maydell

Annoying corner case which I don't think we need to handle necessarily:
if you set a breakpoint on a 32-bit Thumb instruction which spans a page boundary, and the second page is not present, we will end up taking the page fault when I think we should take the breakpoint. I can't think of a way to get that right, so just commenting that it isn't handled right would do.

Could you please point out the piece of code which will generate the page fault? Maybe I will give it a thought :)

When you call arm_ldl_code() and friends, they will end up longjmp()ing out of the codegen phase if the load faults. This then turns into a guest-visible fault in the usual way.

To avoid this you'd need to instead call functions which return a transaction status, but then:
(a) you need to restructure the translate.c code so it can deal with the idea of backing out if the instruction isn't actually present
(b) this still wouldn't work for linux-user mode, where we don't have any way to say "do a memory access, but let me know if it would fail rather than longjmping"

thanks
-- PMM

#10 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

Quoted 14 lines · Peter Maydell
Quoted 9 lines · Sergey Fedorov
Quoted 6 lines · Peter Maydell

Annoying corner case which I don't think we need to handle necessarily:
if you set a breakpoint on a 32-bit Thumb instruction which spans a page boundary, and the second page is not present, we will end up taking the page fault when I think we should take the breakpoint. I can't think of a way to get that right, so just commenting that it isn't handled right would do.

Could you please point out the piece of code which will generate the page fault? Maybe I will give it a thought :)

When you call arm_ldl_code() and friends, they will end up longjmp()ing out of the codegen phase if the load faults. This then turns into a guest-visible fault in the usual way.

Quoted 8 lines

To avoid this you'd need to instead call functions which return a transaction status, but then:
(a) you need to restructure the translate.c code so it can deal with the idea of backing out if the instruction isn't actually present
(b) this still wouldn't work for linux-user mode, where we don't have any way to say "do a memory access, but let me know if it would fail rather than longjmping"

Thank you for the explanation, Peter. I see, if we do insn translation then we take the page fault instead of the CPU breakpoint. As of user mode, can we actually set any CPU breakpoint? If not, as I guess, then
(b) is not applicable to the possible solution that you have described.

Best regards,
Sergey

#11 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

Quoted 4 lines · Sergey Fedorov

Thank you for the explanation, Peter. I see, if we do insn translation then we take the page fault instead of the CPU breakpoint. As of user mode, can we actually set any CPU breakpoint? If not, as I guess, then
(b) is not applicable to the possible solution that you have described.

You're right, we can only have GDB breakpoints in user-mode.

-- PMM

#12 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

In reply to Peter Maydell
Quoted 14 lines · Peter Maydell
Quoted 9 lines · Sergey Fedorov
Quoted 6 lines · Peter Maydell

Annoying corner case which I don't think we need to handle necessarily:
if you set a breakpoint on a 32-bit Thumb instruction which spans a page boundary, and the second page is not present, we will end up taking the page fault when I think we should take the breakpoint. I can't think of a way to get that right, so just commenting that it isn't handled right would do.

Could you please point out the piece of code which will generate the page fault? Maybe I will give it a thought :)

When you call arm_ldl_code() and friends, they will end up longjmp()ing out of the codegen phase if the load faults. This then turns into a guest-visible fault in the usual way.

Quoted 8 lines

To avoid this you'd need to instead call functions which return a transaction status, but then:
(a) you need to restructure the translate.c code so it can deal with the idea of backing out if the instruction isn't actually present
(b) this still wouldn't work for linux-user mode, where we don't have any way to say "do a memory access, but let me know if it would fail rather than longjmping"

Hm... Seems we will get the page fault instead of the CPU breakpoint not only on a 32-bit Thumb instruction which spans a page boundary but on every instruction translation fetch when the page is not present. Do I understand it correctly?

Best regards,
Sergey

#13 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

Annoying corner case which I don't think we need to handle necessarily:
if you set a breakpoint on a 32-bit Thumb instruction which spans a page boundary, and the second page is not present, we will end up taking the page fault when I think we should take the breakpoint. I can't think of a way to get that right, so just commenting that it isn't handled right would do.

What I found in ARM ARMv8 (DDI0487A_g) section G2.9.1 About Breakpoint exceptions:

For each instruction in the program flow, all of the breakpoints are tested. When a breakpoint is tested, it generates
a Breakpoint debug event if all of the following are true:
• The breakpoint is enabled. That is, the breakpoint enable control for it, DBGBCR<n>.E, is 1.
• The conditions specified in the DBGBCR<n> are met.
• The comparisons with the values held in one or both of the DBGBVR<n> and DBGBXVR<n>, as applicable,
are successful.
• If the breakpoint is linked to another breakpoint, the comparisons made by that other breakpoint are also
successful.
• The instruction is committed for execution.

If I understand correctly, the last item in the list specifies that any page fault exception which would occur in the normal instruction execution has more priority than the breakpoint exception. If so, Everything should be okay.

Best regards,
Sergey

#14 lore raw

Re: [Qemu-devel] [PATCH v2 2/2] target-arm: Fix CPU breakpoint handling

Quoted 4 lines · Sergey Fedorov

If I understand correctly, the last item in the list specifies that any page fault exception which would occur in the normal instruction execution has more priority than the breakpoint exception. If so, Everything should be okay.

Oh, good catch. This is more clearly noted in the sections about synchronous exception prioritization in D1.13.2 and G1.11.2, which clearly list prefetch abort as higher priority than breakpoints.

thanks
-- PMM