Skip to contentSkip to conversation

KVM Guest Debug fixes (plus TCG EL2 debug tweaks)

PATCH Superseded Alex Bennée 2 participants 14 messages Nov 8, 2018

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM Guest Debug fixes (plus TCG EL2 debug tweaks)

Hi,

These are fixes for guest debug when running under KVM. While re-spinning these I came across an anomaly which pointed to a kernel bug that caused the 1st single-step to fail. This is being discussed on the kvm-arm list:

Subject: [RFC PATCH] KVM: arm64: don't single-step for non-emulated faults
Date: Wed, 7 Nov 2018 17:10:31 +0000
Message-Id: <[email protected]>

As debugging HYP mode code is next to impossible on real hardware I tried re-creating the single-step bug under TCG. As a result I ran into some debug and EL2 cases that failed. The final two patches are some fixes but I'm still seeing some weird behaviour although it is currently obscured by timer interrupts constantly firing as I enter the to be single-stepped guest EL1 instruction so they can probably be skipped for 3.1.

Alex Bennée (6):
target/arm64: properly handle DBGVR RESS bits target/arm64: hold BQL when calling do_interrupt() target/arm64: kvm debug set target_el when passing exception to guest tests/guest-debug: fix scoping of failcount
arm: use symbolic MDCR_TDE in arm_debug_target_el
arm: fix aa64_generate_debug_exceptions to work with EL2

 target/arm/cpu.h                  | 29 ++++++++++++++++++-----------
 target/arm/kvm64.c                | 20 ++++++++++++++++++--
 tests/guest-debug/test-gdbstub.py |  1 +
 3 files changed, 37 insertions(+), 13 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/6 target/arm64: properly handle DBGVR RESS bits

This only fails with some (broken) versions of gdb but we should treat the top bits of DBGBVR as RESS. Properly sign extend QEMU's reference copy of dbgbvr and also update the register descriptions in the comment.

---
v2
- sanitise register on insertion
- update reference description

---
 target/arm/kvm64.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
target/arm/kvm64.c +15 -2
index 5de8ff0ac5..b92ce3437f 100644
@@ -103,7 +103,7 @@ static void kvm_arm_init_debug(CPUState *cs)
  * capable of fancier matching but that will require exposing that
  * fanciness to GDB's interface
  *
- * D7.3.2 DBGBCR<n>_EL1, Debug Breakpoint Control Registers
+ * DBGBCR<n>_EL1, Debug Breakpoint Control Registers
  *
  *  31  24 23  20 19   16 15 14  13  12   9 8   5 4    3 2   1  0
  * +------+------+-------+-----+----+------+-----+------+-----+---+
@@ -115,12 +115,25 @@ static void kvm_arm_init_debug(CPUState *cs)
  * SSC/HMC/PMC: Security, Higher and Priv access control (Table D-12)
  * BAS: Byte Address Select (RES1 for AArch64)
  * E: Enable bit
+ *
+ * DBGBVR<n>_EL1, Debug Breakpoint Value Registers
+ *
+ *  63  53 52       49 48       2  1 0
+ * +------+-----------+----------+-----+
+ * | RESS | VA[52:49] | VA[48:2] | 0 0 |
+ * +------+-----------+----------+-----+
+ *
+ * Depending on the addressing mode bits the top bits of the register
+ * are a sign extension of the highest applicable VA bit. Some
+ * versions of GDB don't do it correctly so we ensure they are correct
+ * here so future PC comparisons will work properly.
  */
+
 static int insert_hw_breakpoint(target_ulong addr)
 {
     HWBreakpoint brk = {
         .bcr = 0x1,                             /* BCR E=1, enable */
-        .bvr = addr
+        .bvr = sextract64(addr, 52, 53)
     };
 
     if (cur_hw_bps >= max_hw_bps) {
Signature · 1 line
2.17.1
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/6] target/arm64: properly handle DBGVR RESS bits

target/arm/kvm64.c static void kvm_arm_init_debug(CPUState *cs) · line 136

Quoted 2 lines · Alex Bennée
target/arm/kvm64.c +1 -1
-        .bvr = addr
+        .bvr = sextract64(addr, 52, 53)

I think you meant sextract64(addr, 0, 53).
What you wrote *should* have asserted, since 52+53 > 64.

r~

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/6] target/arm64: properly handle DBGVR RESS bits

Quoted 6 lines · Richard Henderson

target/arm/kvm64.c static void kvm_arm_init_debug(CPUState *cs) · line 136

Quoted 2 lines · Alex Bennée
target/arm/kvm64.c +1 -1
-        .bvr = addr
+        .bvr = sextract64(addr, 52, 53)

I think you meant sextract64(addr, 0, 53).
What you wrote *should* have asserted, since 52+53 > 64.

Dam, I did fix that. I must have failed to propagate the fix from where I was hacking :-/

Quoted 3 lines

r~

--
Alex Bennée

#4 lore raw

PATCH v2 2/6 target/arm64: hold BQL when calling do_interrupt()

In reply to Alex Bennée

Fix the assertion failure when running interrupts.

---
 target/arm/kvm64.c | 2 ++
 1 file changed, 2 insertions(+)
target/arm/kvm64.c +2 -0
index b92ce3437f..03b0f78831 100644
@@ -1000,7 +1000,9 @@ bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)
     cs->exception_index = EXCP_BKPT;
     env->exception.syndrome = debug_exit->hsr;
     env->exception.vaddress = debug_exit->far;
+    qemu_mutex_lock_iothread();
     cc->do_interrupt(cs);
+    qemu_mutex_unlock_iothread();
 
     return false;
 }
Signature · 1 line
2.17.1
#6 lore raw

PATCH v2 3/6 target/arm64: kvm debug set target_el when passing exception to guest

In reply to Alex Bennée

When we are debugging the guest all exceptions come our way but might be for the guest's own debug exceptions. We use the ->do_interrupt() infrastructure to inject the exception into the guest. However, we are missing a full setup of the exception structure, causing an assert later down the line.

---
v2
- tweak commit msg for grammar

---
 target/arm/kvm64.c | 1 +
 1 file changed, 1 insertion(+)
target/arm/kvm64.c +1 -0
index 03b0f78831..bf7824d862 100644
@@ -1000,6 +1000,7 @@ bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)
     cs->exception_index = EXCP_BKPT;
     env->exception.syndrome = debug_exit->hsr;
     env->exception.vaddress = debug_exit->far;
+    env->exception.target_el = 1;
     qemu_mutex_lock_iothread();
     cc->do_interrupt(cs);
     qemu_mutex_unlock_iothread();
Signature · 1 line
2.17.1
Reviewed-by on v2 3/6
#7 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] target/arm64: kvm debug set target_el when passing exception to guest

Quoted 8 lines · Alex Bennée

When we are debugging the guest all exceptions come our way but might be for the guest's own debug exceptions. We use the ->do_interrupt() infrastructure to inject the exception into the guest. However, we are missing a full setup of the exception structure, causing an assert later down the line.

r~

#8 lore raw

PATCH v2 4/6 tests/guest-debug: fix scoping of failcount

In reply to Alex Bennée

You should declare you are using a global version of a variable before you attempt to modify it in a function.

---
 tests/guest-debug/test-gdbstub.py | 1 +
 1 file changed, 1 insertion(+)
tests/guest-debug/test-gdbstub.py +1 -0
index 0e4ac01426..c7e3986a24 100644
@@ -16,6 +16,7 @@ def report(cond, msg):
         print ("PASS: %s" % (msg))
     else:
         print ("FAIL: %s" % (msg))
+        global failcount
         failcount += 1
 
 
Signature · 1 line
2.17.1
Reviewed-by on v2 4/6
#9 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] tests/guest-debug: fix scoping of failcount

tests/guest-debug/test-gdbstub.py def report(cond, msg): · line 20

Quoted 6 lines · Alex Bennée
tests/guest-debug/test-gdbstub.py +1 -0
@@ -16,6 +16,7 @@ def report(cond, msg):
         print ("PASS: %s" % (msg))
     else:
         print ("FAIL: %s" % (msg))
+        global failcount
         failcount += 1

Do we usually prefer such declarations at the start of the function? Anyway,

r~

#10 lore raw

PATCH v2 5/6 arm: use symbolic MDCR_TDE in arm_debug_target_el

In reply to Alex Bennée

We already have this symbol defined so lets use it.

---
 target/arm/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/cpu.h +1 -1
index b5eff79f73..1efff21a18 100644
@@ -2743,7 +2743,7 @@ static inline int arm_debug_target_el(CPUARMState *env)
 
     if (arm_feature(env, ARM_FEATURE_EL2) && !secure) {
         route_to_el2 = env->cp15.hcr_el2 & HCR_TGE ||
-                       env->cp15.mdcr_el2 & (1 << 8);
+                       env->cp15.mdcr_el2 & MDCR_TDE;
     }
 
     if (route_to_el2) {
Signature · 1 line
2.17.1
#12 lore raw

PATCH v2 6/6 arm: fix aa64_generate_debug_exceptions to work with EL2

In reply to Alex Bennée

The test was incomplete and incorrectly caused debug exceptions to be generated when returning to EL2 after a failed attempt to single-step an EL1 instruction. Fix this while cleaning up the function a little.

---
 target/arm/cpu.h | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
target/arm/cpu.h +17 -10
index 1efff21a18..a6d8eb14f6 100644
@@ -2764,23 +2764,33 @@ static inline bool arm_v7m_csselr_razwi(ARMCPU *cpu)
     return (cpu->clidr & R_V7M_CLIDR_CTYPE_ALL_MASK) != 0;
 }
 
+/* See AArch64.GenerateDebugExceptionsFrom() in ARM ARM pseudocode */
 static inline bool aa64_generate_debug_exceptions(CPUARMState *env)
 {
+    int cur_el = arm_current_el(env);
+    int debug_el;
+
     if (arm_is_secure(env)) {
         /* MDCR_EL3.SDD disables debug events from Secure state */
         if (extract32(env->cp15.mdcr_el3, 16, 1) != 0
-            || arm_current_el(env) == 3) {
+            || cur_el == 3) {
             return false;
         }
     }
 
-    if (arm_current_el(env) == arm_debug_target_el(env)) {
-        if ((extract32(env->cp15.mdscr_el1, 13, 1) == 0)
-            || (env->daif & PSTATE_D)) {
-            return false;
-        }
+    /*
+     * Same EL to same EL debug exceptions need MDSCR_KDE enabled
+     * while not masking the (D)ebug bit in DAIF.
+     */
+    debug_el = arm_debug_target_el(env);
+
+    if (cur_el == debug_el) {
+        return extract32(env->cp15.mdscr_el1, 13, 1)
+            && !(env->daif & PSTATE_D);
     }
-    return true;
+
+    /* Otherwise the debug target needs to be a higher EL */
+    return debug_el > cur_el;
 }
 
 static inline bool aa32_generate_debug_exceptions(CPUARMState *env)
@@ -2833,9 +2843,6 @@ static inline bool aa32_generate_debug_exceptions(CPUARMState *env)
  * since the pseudocode has it at all callsites except for the one in
  * CheckSoftwareStep(), where it is elided because both branches would
  * always return the same value.
- *
- * Parts of the pseudocode relating to EL2 and EL3 are omitted because we
- * don't yet implement those exception levels or their associated trap bits.
  */
 static inline bool arm_generate_debug_exceptions(CPUARMState *env)
 {
Signature · 1 line
2.17.1
#13 lore raw

Re: [Qemu-devel] [PATCH v2 6/6] arm: fix aa64_generate_debug_exceptions to work with EL2

Quoted 18 lines · Alex Bennée

The test was incomplete and incorrectly caused debug exceptions to be generated when returning to EL2 after a failed attempt to single-step an EL1 instruction. Fix this while cleaning up the function a little.

---
 target/arm/cpu.h | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
target/arm/cpu.h +1 -0
index 1efff21a18..a6d8eb14f6 100644
@@ -2764,23 +2764,33 @@ static inline bool arm_v7m_csselr_razwi(ARMCPU *cpu)
     return (cpu->clidr & R_V7M_CLIDR_CTYPE_ALL_MASK) != 0;
 }
 
+/* See AArch64.GenerateDebugExceptionsFrom() in ARM ARM pseudocode */

target/arm/cpu.h static inline bool arm_v7m_csselr_razwi(ARMCPU *cpu) · line 2776

Quoted 10 lines
target/arm/cpu.h +4 -1
 static inline bool aa64_generate_debug_exceptions(CPUARMState *env)
 {
+    int cur_el = arm_current_el(env);
+    int debug_el;
+
     if (arm_is_secure(env)) {
         /* MDCR_EL3.SDD disables debug events from Secure state */
         if (extract32(env->cp15.mdcr_el3, 16, 1) != 0
-            || arm_current_el(env) == 3) {
+            || cur_el == 3) {

Hmm. Perhaps better as

    if (cur_el == 3) {
        return false;
    }
    /* MDCR_EL3.SDD disables... */
    if (arm_is_secure_below_el3(env)
        && extract32(env->cp15.mdcr_el3, 16, 1)) {
        return false;
    }

and of course more symbols would be nice, but it's not wrong as-is.

r~