Skip to contentSkip to conversation

target/riscv: Nick's debug fixes for 11.1

PATCH In review Daniel Henrique Barboza 2 participants 20 messages Jul 16 – Jul 17 RISC-V

lore
Share & tools
lore
#0 lore raw

cover target/riscv: Nick's debug fixes for 11.1

Greetings,

Nick sent a RFC with several debug fixes and some new features back in January:

https://lore.kernel.org/qemu-devel/[email protected]/

I took all the bug fixes in that RFC, rebased and I'm now sending for 11.1 upstreaming. All patches are fixing something that is currently broken, with the adequate "Fixes" tag, so I'm comfortable in sending all of them for 11.1.

Mister Chao, if you could take a quick look in these that would be terrific (you have acked patches 1-4 already). This is the first step to finally upstream your sdext work. Next step is to split debug 0.13 and 1.0 support, which I am going to do by maybe taking some of Nick's work in that RFC, maybe with some new stuff, and then we'll add your sdext on top of the 1.0 codebase. This is all 11.2 business that, hopefully, we'll get queued and ready to go as soon as the 11.2 window opens.

All patches acked. Patches based on master.

Nicholas Piggin (17):
target/riscv/debug: Check only mcontrol triggers for break/watchpoint
matching
target/riscv/debug: Handle changing trigger types target/riscv/debug: Implement permissive type unavailable trigger target/riscv/debug: Fix icount trigger privilege check target/riscv/debug: Update itrigger_enabled after changing privilege target/riscv/debug: Implement get_trigger_action for icount type
trigger
target/riscv/debug: Fix icount privilege matching icount_enabled()
test
target/riscv/debug: Implement icount trigger textra matching target/riscv/debug: Maintain itrigger_enabled in
helper_itrigger_match()
target/riscv/debug: Fix breakpoint matching action target/riscv/debug: Put mcontrol load/store match address into tval target/riscv/debug: Remove breakpoints on reset target/riscv/debug: Fix migration post_load icount_enabled() test target/riscv/debug: Insert breakpoints after migration target/riscv/debug: Advertise icount trigger type in tinfo target/riscv/debug: Reset trigger type to unavailable target/riscv/debug: Fix minor comment typos

 target/riscv/machine.c        |  13 +-
 target/riscv/tcg/cpu_helper.c |   7 +
 target/riscv/tcg/debug.c      | 326 ++++++++++++++++++++++------------
 target/riscv/tcg/debug.h      |   5 +-
 4 files changed, 229 insertions(+), 122 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/17 target/riscv/debug: Check only mcontrol triggers for break/watchpoint matching

From: Nicholas Piggin <[email protected]>

trigger_common_match() has some warning log messages in it for unsupported triggers. Don't call it in BP/WP checking except for mcontrol/mcontrol6 triggers applicable to BP/WPs.

Fixes b5f6379d13 ("target/riscv: debug: Implement debug related TCGCPUOps")
---
 target/riscv/tcg/debug.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
target/riscv/tcg/debug.c +18 -4
index 3c0fe70101..3ee598b86c 100644
@@ -954,6 +954,14 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
         for (i = 0; i < env->num_triggers; i++) {
             trigger_type = get_trigger_type(env, i);
 
+            switch (trigger_type) {
+            case TRIGGER_TYPE_AD_MATCH:
+            case TRIGGER_TYPE_AD_MATCH6:
+                break;
+            default:
+                continue; /* No other types match breakpoint */
+            }
+
             if (!trigger_common_match(env, trigger_type, i)) {
                 continue;
             }
@@ -978,8 +986,7 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                 }
                 break;
             default:
-                /* other trigger types are not supported or irrelevant */
-                break;
+                g_assert_not_reached();
             }
         }
     }
@@ -1000,6 +1007,14 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
     for (i = 0; i < env->num_triggers; i++) {
         trigger_type = get_trigger_type(env, i);
 
+        switch (trigger_type) {
+        case TRIGGER_TYPE_AD_MATCH:
+        case TRIGGER_TYPE_AD_MATCH6:
+            break;
+        default:
+            continue; /* No other types match watchpoint */
+        }
+
         if (!trigger_common_match(env, trigger_type, i)) {
             continue;
         }
@@ -1038,8 +1053,7 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
             }
             break;
         default:
-            /* other trigger types are not supported */
-            break;
+            g_assert_not_reached();
         }
     }
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/17 target/riscv/debug: Handle changing trigger types

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

Updating debug registers will first remove the existing TCG breakpoint / watchpoint, then adds it back with new values.

Writing TDATA1 with a value that changes the trigger type attempts to remove the facility for the new trigger type rather than the existing one. That is, it will not remove a breakpoint if the type is changed to a non-breakpoint type.

Fix this by removing based on the old trigger type, then inserting based on the new type.

Fixes a42bd00166 ("target/riscv: debug: Determine the trigger type from tdata1.type")
---
 target/riscv/tcg/debug.c | 64 +++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 31 deletions(-)
target/riscv/tcg/debug.c +33 -31
index 3ee598b86c..9e6c907742 100644
@@ -530,23 +530,12 @@ static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index)
 static void type2_reg_write(CPURISCVState *env, target_ulong index,
                             int tdata_index, target_ulong val)
 {
-    target_ulong new_val;
-
     switch (tdata_index) {
     case TDATA1:
-        new_val = type2_mcontrol_validate(env, val);
-        if (new_val != env->tdata1[index]) {
-            env->tdata1[index] = new_val;
-            type2_breakpoint_remove(env, index);
-            type2_breakpoint_insert(env, index);
-        }
+        env->tdata1[index] = type2_mcontrol_validate(env, val);
         break;
     case TDATA2:
-        if (val != env->tdata2[index]) {
-            env->tdata2[index] = val;
-            type2_breakpoint_remove(env, index);
-            type2_breakpoint_insert(env, index);
-        }
+        env->tdata2[index] = val;
         break;
     case TDATA3:
         env->tdata3[index] = textra_validate(env, val);
@@ -554,6 +543,8 @@ static void type2_reg_write(CPURISCVState *env, target_ulong index,
     default:
         g_assert_not_reached();
     }
+
+    type2_breakpoint_insert(env, index);
 }
 
 /* type 6 trigger */
@@ -644,23 +635,12 @@ static void type6_breakpoint_remove(CPURISCVState *env, target_ulong index)
 static void type6_reg_write(CPURISCVState *env, target_ulong index,
                             int tdata_index, target_ulong val)
 {
-    target_ulong new_val;
-
     switch (tdata_index) {
     case TDATA1:
-        new_val = type6_mcontrol6_validate(env, val);
-        if (new_val != env->tdata1[index]) {
-            env->tdata1[index] = new_val;
-            type6_breakpoint_remove(env, index);
-            type6_breakpoint_insert(env, index);
-        }
+        env->tdata1[index] = type6_mcontrol6_validate(env, val);
         break;
     case TDATA2:
-        if (val != env->tdata2[index]) {
-            env->tdata2[index] = val;
-            type6_breakpoint_remove(env, index);
-            type6_breakpoint_insert(env, index);
-        }
+        env->tdata2[index] = val;
         break;
     case TDATA3:
         env->tdata3[index] = textra_validate(env, val);
@@ -668,6 +648,7 @@ static void type6_reg_write(CPURISCVState *env, target_ulong index,
     default:
         g_assert_not_reached();
     }
+    type6_breakpoint_insert(env, index);
 }
 
 /* icount trigger type */
@@ -833,8 +814,6 @@ static void itrigger_reg_write(CPURISCVState *env, target_ulong index,
                 /* set the count to timer */
                 timer_mod(env->itrigger_timer[index],
                           env->last_icount + itrigger_get_count(env, index));
-            } else {
-                env->itrigger_enabled = riscv_itrigger_enabled(env);
             }
         }
         break;
@@ -883,12 +862,30 @@ target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index)
 
 void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
 {
-    int trigger_type;
+    int trigger_type = get_trigger_type(env, env->trigger_cur);
+    bool check_itrigger = false;
+
+    switch (trigger_type) {
+    case TRIGGER_TYPE_AD_MATCH:
+        type2_breakpoint_remove(env, env->trigger_cur);
+        break;
+    case TRIGGER_TYPE_AD_MATCH6:
+        type6_breakpoint_remove(env, env->trigger_cur);
+        break;
+    case TRIGGER_TYPE_INST_CNT:
+        /*
+         * itrigger_enabled is the union of all enabled icount triggers,
+         * so it's easiest to recheck all if any have changed (removed or
+         * added or modified).
+         */
+        check_itrigger = true;
+        break;
+    default:
+        break;
+    }
 
     if (tdata_index == TDATA1) {
         trigger_type = extract_trigger_type(env, val);
-    } else {
-        trigger_type = get_trigger_type(env, env->trigger_cur);
     }
 
     switch (trigger_type) {
@@ -900,6 +897,7 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
         break;
     case TRIGGER_TYPE_INST_CNT:
         itrigger_reg_write(env, env->trigger_cur, tdata_index, val);
+        check_itrigger = true;
         break;
     case TRIGGER_TYPE_INT:
     case TRIGGER_TYPE_EXCP:
@@ -915,6 +913,10 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
     default:
         g_assert_not_reached();
     }
+
+    if (check_itrigger && !icount_enabled()) {
+        env->itrigger_enabled = riscv_itrigger_enabled(env);
+    }
 }
 
 target_ulong tinfo_csr_read(CPURISCVState *env)
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/17 target/riscv/debug: Implement permissive type unavailable trigger

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

When a trigger type is "disabled", tdata2 and tdata3 must accept values that are valid for some supported trigger type. Additionally, writing 0 to tdata1 must result in the type becoming "disabled". This is important for the prescribed sequences for updating triggers.

Implement write tdata=0 -> disabled behaviour and permissive accepting of tdata2/3 values in disabled state. This implementation could be improved by checking tdata2/3 values against supported trigger types, but it is good enough to be usable by software.

From the RISC-V Debug Specification for tdata1:

Writing 0 to this register must result in a trigger that is disabled. If this trigger supports multiple types, then the hardware should disable it by changing type to 15.

and, when type=15:

This trigger is disabled. In this state, tdata2 and tdata3 can be written with any value that is supported for any of the types this trigger implements.

Fixes a42bd00166 ("target/riscv: debug: Determine the trigger type from tdata1.type")
---
 target/riscv/tcg/debug.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
target/riscv/tcg/debug.c +29 -1
index 9e6c907742..c7cc67e244 100644
@@ -829,6 +829,28 @@ static void itrigger_reg_write(CPURISCVState *env, target_ulong index,
     }
 }
 
+static void anytype_reg_write(CPURISCVState *env, target_ulong index,
+                              int tdata_index, target_ulong val)
+{
+    /*
+     * This should check the value is valid for at least one of the supported
+     * trigger types.
+     */
+    switch (tdata_index) {
+    case TDATA1:
+        env->tdata1[env->trigger_cur] = val;
+        break;
+    case TDATA2:
+        env->tdata2[env->trigger_cur] = val;
+        break;
+    case TDATA3:
+        env->tdata3[env->trigger_cur] = val;
+        break;
+    default:
+        g_assert_not_reached();
+    }
+}
+
 static int itrigger_get_adjust_count(CPURISCVState *env)
 {
     int count = itrigger_get_count(env, env->trigger_cur), executed;
@@ -885,6 +907,10 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
     }
 
     if (tdata_index == TDATA1) {
+        if (val == 0) {
+            /* special case, writing 0 results in disabled trigger */
+            val = build_tdata1(env, TRIGGER_TYPE_UNAVAIL, 0, 0);
+        }
         trigger_type = extract_trigger_type(env, val);
     }
 
@@ -899,6 +925,9 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
         itrigger_reg_write(env, env->trigger_cur, tdata_index, val);
         check_itrigger = true;
         break;
+    case TRIGGER_TYPE_UNAVAIL:
+        anytype_reg_write(env, env->trigger_cur, tdata_index, val);
+        break;
     case TRIGGER_TYPE_INT:
     case TRIGGER_TYPE_EXCP:
     case TRIGGER_TYPE_EXT_SRC:
@@ -906,7 +935,6 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
                       trigger_type);
         break;
     case TRIGGER_TYPE_NO_EXIST:
-    case TRIGGER_TYPE_UNAVAIL:
         qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n",
                       trigger_type);
         break;
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/17 target/riscv/debug: Fix icount trigger privilege check

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

The check_itrigger_priv() did not check privilege bits properly. Move all priv checks into functions and have the icount check follow the same form as the others.

Fixes 2c9d747121 ("target/riscv: Add itrigger support when icount is not enabled")
---
 target/riscv/tcg/debug.c | 82 ++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 45 deletions(-)
target/riscv/tcg/debug.c +37 -45
index c7cc67e244..3c777694e5 100644
@@ -308,48 +308,50 @@ static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
  * Check the privilege level of specific trigger matches CPU's current privilege
  * level.
  */
+static bool type2_priv_match(CPURISCVState *env, target_ulong tdata1)
+{
+    /* type 2 trigger cannot be fired in VU/VS mode */
+    if (env->virt_enabled) {
+        return false;
+    }
+    /* check U/S/M bit against current privilege level */
+    return ((tdata1 >> 3) & 0b1011) & BIT(env->priv);
+}
+
+static bool type6_priv_match(CPURISCVState *env, target_ulong tdata1)
+{
+    if (env->virt_enabled) {
+        /* check VU/VS bit against current privilege level */
+        return ((tdata1 >> 23) & 0b11) & BIT(env->priv);
+    } else {
+        /* check U/S/M bit against current privilege level */
+        return ((tdata1 >> 3) & 0b1011) & BIT(env->priv);
+    }
+}
+
+static bool icount_priv_match(CPURISCVState *env, target_ulong tdata1)
+{
+    if (env->virt_enabled) {
+        /* check VU/VS bit against current privilege level */
+        return ((tdata1 >> 25) & 0b11) & BIT(env->priv);
+    } else {
+        /* check U/S/M bit against current privilege level */
+        return ((tdata1 >> 6) & 0b1011) & BIT(env->priv);
+    }
+}
+
 static bool trigger_priv_match(CPURISCVState *env, trigger_type_t type,
                                int trigger_index)
 {
-    target_ulong ctrl = env->tdata1[trigger_index];
+    target_ulong tdata1 = env->tdata1[trigger_index];
 
     switch (type) {
     case TRIGGER_TYPE_AD_MATCH:
-        /* type 2 trigger cannot be fired in VU/VS mode */
-        if (env->virt_enabled) {
-            return false;
-        }
-        /* check U/S/M bit against current privilege level */
-        if ((ctrl >> 3) & BIT(env->priv)) {
-            return true;
-        }
-        break;
+        return type2_priv_match(env, tdata1);
     case TRIGGER_TYPE_AD_MATCH6:
-        if (env->virt_enabled) {
-            /* check VU/VS bit against current privilege level */
-            if ((ctrl >> 23) & BIT(env->priv)) {
-                return true;
-            }
-        } else {
-            /* check U/S/M bit against current privilege level */
-            if ((ctrl >> 3) & BIT(env->priv)) {
-                return true;
-            }
-        }
-        break;
+        return type6_priv_match(env, tdata1);
     case TRIGGER_TYPE_INST_CNT:
-        if (env->virt_enabled) {
-            /* check VU/VS bit against current privilege level */
-            if ((ctrl >> 25) & BIT(env->priv)) {
-                return true;
-            }
-        } else {
-            /* check U/S/M bit against current privilege level */
-            if ((ctrl >> 6) & BIT(env->priv)) {
-                return true;
-            }
-        }
-        break;
+        return icount_priv_match(env, tdata1);
     case TRIGGER_TYPE_INT:
     case TRIGGER_TYPE_EXCP:
     case TRIGGER_TYPE_EXT_SRC:
@@ -667,17 +669,7 @@ itrigger_set_count(CPURISCVState *env, int index, int value)
 
 static bool check_itrigger_priv(CPURISCVState *env, int index)
 {
-    target_ulong tdata1 = env->tdata1[index];
-    if (env->virt_enabled) {
-        /* check VU/VS bit against current privilege level */
-        return (get_field(tdata1, ITRIGGER_VS) == env->priv) ||
-               (get_field(tdata1, ITRIGGER_VU) == env->priv);
-    } else {
-        /* check U/S/M bit against current privilege level */
-        return (get_field(tdata1, ITRIGGER_M) == env->priv) ||
-               (get_field(tdata1, ITRIGGER_S) == env->priv) ||
-               (get_field(tdata1, ITRIGGER_U) == env->priv);
-    }
+    return icount_priv_match(env, index);
 }
 
 bool riscv_itrigger_enabled(CPURISCVState *env)
Signature · 1 line
2.43.0
#5 lore raw

PATCH 5/17 target/riscv/debug: Update itrigger_enabled after changing privilege

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

The !icount_enabled() path should update itrigger_enabled after changing privilege, which can change whether or not an icount trigger is enabled.

Fixes b2d7a7c7e4 ("target/riscv: Implement privilege mode filtering for cycle/instret")
---
 target/riscv/tcg/cpu_helper.c | 7 +++++++
 target/riscv/tcg/debug.c      | 7 +++++++
 target/riscv/tcg/debug.h      | 3 +++
 3 files changed, 17 insertions(+)
target/riscv/tcg/cpu_helper.c +7 -0
index 07d9222652..6c18526c49 100644
@@ -844,9 +844,12 @@ void riscv_ctr_add_entry(CPURISCVState *env, uint64_t src, uint64_t dst,
 void riscv_cpu_set_mode(CPURISCVState *env, privilege_mode_t newpriv,
                         bool virt_en)
 {
+    bool change = false;
+
     g_assert(newpriv <= PRV_M && newpriv != PRV_RESERVED);
 
     if (newpriv != env->priv || env->virt_enabled != virt_en) {
+        change = true;
         if (icount_enabled()) {
             riscv_itrigger_update_priv(env);
         }
@@ -889,6 +892,10 @@ void riscv_cpu_set_mode(CPURISCVState *env, privilege_mode_t newpriv,
             riscv_cpu_update_mip(env, 0, 0);
         }
     }
+
+    if (change) {
+        riscv_cpu_debug_change_priv(env);
+    }
 }
 
 /*
target/riscv/tcg/debug.c +7 -0
index 3c777694e5..a0222e178b 100644
@@ -1121,6 +1121,13 @@ void riscv_trigger_unrealize(CPURISCVState *env)
     g_free(env->itrigger_timer);
 }
 
+void riscv_cpu_debug_change_priv(CPURISCVState *env)
+{
+    if (!icount_enabled()) {
+        env->itrigger_enabled = riscv_itrigger_enabled(env);
+    }
+}
+
 void riscv_trigger_reset_hold(CPURISCVState *env)
 {
     target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0);
target/riscv/tcg/debug.h +3 -0
index a25d099b37..88d34ecac9 100644
@@ -153,4 +153,7 @@ void riscv_trigger_reset_hold(CPURISCVState *env);
 
 bool riscv_itrigger_enabled(CPURISCVState *env);
 void riscv_itrigger_update_priv(CPURISCVState *env);
+
+void riscv_cpu_debug_change_priv(CPURISCVState *env);
+
 #endif /* RISCV_DEBUG_H */
Signature · 1 line
2.43.0
#6 lore raw

PATCH 6/17 target/riscv/debug: Implement get_trigger_action for icount type trigger

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

ITRIGGER_ACTION has been present for awhile but get_trigger_action isn't implementing it.

Fixes d1c111411e ("target/riscv: debug: Create common trigger actions function")
---
 target/riscv/tcg/debug.c | 2 ++
 1 file changed, 2 insertions(+)
target/riscv/tcg/debug.c +2 -0
index a0222e178b..6aba7e9e12 100644
@@ -112,6 +112,8 @@ static trigger_action_t get_trigger_action(CPURISCVState *env,
         action = (tdata1 & TYPE6_ACTION) >> 12;
         break;
     case TRIGGER_TYPE_INST_CNT:
+        action = (tdata1 & ITRIGGER_ACTION);
+        break;
     case TRIGGER_TYPE_INT:
     case TRIGGER_TYPE_EXCP:
     case TRIGGER_TYPE_EXT_SRC:
Signature · 1 line
2.43.0
#7 lore raw

PATCH 7/17 target/riscv/debug: Fix icount privilege matching icount_enabled() test

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

itrigger_enabled should be set if the privilege does match. The test is inverted.

Fixes 2c9d747121 ("target/riscv: Add itrigger support when icount is not enabled")
---
 target/riscv/tcg/debug.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/tcg/debug.c +1 -1
index 6aba7e9e12..3edbb591ca 100644
@@ -681,7 +681,7 @@ bool riscv_itrigger_enabled(CPURISCVState *env)
         if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) {
             continue;
         }
-        if (check_itrigger_priv(env, i)) {
+        if (!check_itrigger_priv(env, i)) {
             continue;
         }
         count = itrigger_get_count(env, i);
Signature · 1 line
2.43.0
#8 lore raw

PATCH 8/17 target/riscv/debug: Implement icount trigger textra matching

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

icount triggers are subject to textra checks, per spec.

Fixes 2c9d747121 ("target/riscv: Add itrigger support when icount is not enabled")
---
 target/riscv/tcg/debug.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/riscv/tcg/debug.c +2 -1
index 3edbb591ca..74f1739697 100644
@@ -677,11 +677,12 @@ static bool check_itrigger_priv(CPURISCVState *env, int index)
 bool riscv_itrigger_enabled(CPURISCVState *env)
 {
     int count;
+
     for (int i = 0; i < env->num_triggers; i++) {
         if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) {
             continue;
         }
-        if (!check_itrigger_priv(env, i)) {
+        if (!trigger_common_match(env, TRIGGER_TYPE_INST_CNT, i)) {
             continue;
         }
         count = itrigger_get_count(env, i);
Signature · 1 line
2.43.0
#9 lore raw

PATCH 9/17 target/riscv/debug: Maintain itrigger_enabled in helper_itrigger_match()

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

helper_itrigger_match() iterates over all triggers so it can maintain itrigger_enabled rather than calling riscv_itrigger_enabled() which becomes O(N^2) where N is the number of icount triggers.

Fixes 2c9d747121 ("target/riscv: Add itrigger support when icount is not enabled")
---
 target/riscv/tcg/debug.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
target/riscv/tcg/debug.c +12 -1
index 74f1739697..d98e737763 100644
@@ -695,9 +695,18 @@ bool riscv_itrigger_enabled(CPURISCVState *env)
     return false;
 }
 
+/*
+ * This is called by TCG when an instruction completes.
+ * TCG runs in single-step mode when itrigger_enabled = true, so
+ * it can call after each insn.
+ */
 void helper_itrigger_match(CPURISCVState *env)
 {
     int count;
+    bool enabled = false;
+
+    g_assert(env->itrigger_enabled);
+
     for (int i = 0; i < env->num_triggers; i++) {
         if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) {
             continue;
@@ -711,10 +720,12 @@ void helper_itrigger_match(CPURISCVState *env)
         }
         itrigger_set_count(env, i, count--);
         if (!count) {
-            env->itrigger_enabled = riscv_itrigger_enabled(env);
             do_trigger_action(env, i);
+        } else {
+            enabled = true;
         }
     }
+    env->itrigger_enabled = enabled;
 }
 
 static void riscv_itrigger_update_count(CPURISCVState *env)
Signature · 1 line
2.43.0
#10 lore raw

PATCH 10/17 target/riscv/debug: Fix breakpoint matching action

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

The debug exception callback is too late to find the action for the trigger(s) which caused it, and it is actually passing the wrong thing to do_trigger_action(), which expects a trigger index but is given DBG_ACTION_BP (which will be interpreted as trigger 0 and use the action set for that trigger).

It could be possible to derive the trigger index from the bp/wp address, but that is clunky and it is really the action that determines whether an exception should be raised, also multiple triggers may perform their actions in the same cycle, so it is more consistent to check action during the breakpoint matching phase. If a breakpoint exception is to be taken then that is signaled at that time.

Fixes d1c111411e ("target/riscv: debug: Create common trigger actions function")
---
 target/riscv/tcg/debug.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
target/riscv/tcg/debug.c +23 -12
index d98e737763..f671ae67f8 100644
@@ -282,7 +282,8 @@ static target_ulong textra_validate(CPURISCVState *env, target_ulong tdata3)
     return textra;
 }
 
-static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
+/* Return true if an exception should be raised */
+static bool do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
 {
     trigger_action_t action = get_trigger_action(env, trigger_index);
 
@@ -290,8 +291,7 @@ static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
     case DBG_ACTION_NONE:
         break;
     case DBG_ACTION_BP:
-        riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
-        break;
+        return true;
     case DBG_ACTION_DBG_MODE:
     case DBG_ACTION_TRACE0:
     case DBG_ACTION_TRACE1:
@@ -304,6 +304,7 @@ static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
     default:
         g_assert_not_reached();
     }
+    return false;
 }
 
 /*
@@ -720,7 +721,9 @@ void helper_itrigger_match(CPURISCVState *env)
         }
         itrigger_set_count(env, i, count--);
         if (!count) {
-            do_trigger_action(env, i);
+            if (do_trigger_action(env, i)) {
+                riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
+            }
         } else {
             enabled = true;
         }
@@ -967,11 +970,11 @@ void riscv_cpu_debug_excp_handler(CPUState *cs)
 
     if (cs->watchpoint_hit) {
         if (cs->watchpoint_hit->flags & BP_CPU) {
-            do_trigger_action(env, DBG_ACTION_BP);
+            riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
         }
     } else {
         if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) {
-            do_trigger_action(env, DBG_ACTION_BP);
+            riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
         }
     }
 }
@@ -1008,8 +1011,10 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                 pc = env->tdata2[i];
 
                 if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) {
-                    env->badaddr = pc;
-                    return true;
+                    if (do_trigger_action(env, i)) {
+                        env->badaddr = pc;
+                        return true;
+                    }
                 }
                 break;
             case TRIGGER_TYPE_AD_MATCH6:
@@ -1017,8 +1022,10 @@ bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
                 pc = env->tdata2[i];
 
                 if ((ctrl & TYPE6_EXEC) && (bp->pc == pc)) {
-                    env->badaddr = pc;
-                    return true;
+                    if (do_trigger_action(env, i)) {
+                        env->badaddr = pc;
+                        return true;
+                    }
                 }
                 break;
             default:
@@ -1069,7 +1076,9 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
             }
 
             if ((wp->flags & flags) && (wp->vaddr == addr)) {
-                return true;
+                if (do_trigger_action(env, i)) {
+                    return true;
+                }
             }
             break;
         case TRIGGER_TYPE_AD_MATCH6:
@@ -1085,7 +1094,9 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
             }
 
             if ((wp->flags & flags) && (wp->vaddr == addr)) {
-                return true;
+                if (do_trigger_action(env, i)) {
+                    return true;
+                }
             }
             break;
         default:
Signature · 1 line
2.43.0
#11 lore raw

PATCH 11/17 target/riscv/debug: Put mcontrol load/store match address into tval

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

When an mc/mc6 trigger triggers on a load/store and raises a breakpoint exception, put the matching memory address into tval, as is already done for exec addresses matching.

Tval for breakpoint exceptions is optional in the spec, but it seems more consistent to implement it for load/store if exec is implemented.

Fixes a42bd00166 ("target/riscv: debug: Determine the trigger type from tdata1.type")
---
 target/riscv/tcg/debug.c | 2 ++
 1 file changed, 2 insertions(+)
target/riscv/tcg/debug.c +2 -0
index f671ae67f8..7ecbe01fb2 100644
@@ -1077,6 +1077,7 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
 
             if ((wp->flags & flags) && (wp->vaddr == addr)) {
                 if (do_trigger_action(env, i)) {
+                    env->badaddr = wp->vaddr;
                     return true;
                 }
             }
@@ -1095,6 +1096,7 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
 
             if ((wp->flags & flags) && (wp->vaddr == addr)) {
                 if (do_trigger_action(env, i)) {
+                    env->badaddr = wp->vaddr;
                     return true;
                 }
             }
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH for-11.1 11/17] target/riscv/debug: Put mcontrol load/store match address into tval

Quoted 29 lines · Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

When an mc/mc6 trigger triggers on a load/store and raises a breakpoint exception, put the matching memory address into tval, as is already done for exec addresses matching.

Tval for breakpoint exceptions is optional in the spec, but it seems more consistent to implement it for load/store if exec is implemented.

Fixes a42bd00166 ("target/riscv: debug: Determine the trigger type from tdata1.type")
---
  target/riscv/tcg/debug.c | 2 ++
  1 file changed, 2 insertions(+)
target/riscv/tcg/debug.c +1 -0
index f671ae67f8..7ecbe01fb2 100644
@@ -1077,6 +1077,7 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
 
              if ((wp->flags & flags) && (wp->vaddr == addr)) {
                  if (do_trigger_action(env, i)) {
+                    env->badaddr = wp->vaddr;
                      return true;
                  }
              }

target/riscv/tcg/debug.c bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp) · line 1102

Quoted 7 lines
target/riscv/tcg/debug.c +1 -0
@@ -1095,6 +1096,7 @@ bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
 
              if ((wp->flags & flags) && (wp->vaddr == addr)) {
                  if (do_trigger_action(env, i)) {
+                    env->badaddr = wp->vaddr;
                      return true;
                  }
              }

check_watchpoint shouldn't set badaddr; the subsequent call to debug_excp_handler should.

Same vs patch 10 and breakpoints.

Also, wp->hitaddr is the address that actually triggered within [vaddr, vaddr+len).

r~

#13 lore raw

PATCH 12/17 target/riscv/debug: Remove breakpoints on reset

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

Remove TCG breakpoints/watchpoints when the CPU is reset. Currently they get lost.

Fixes b6092544fc ("target/riscv: csr: Hook debug CSR read/write")
---
 target/riscv/tcg/debug.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
target/riscv/tcg/debug.c +13 -2
index 7ecbe01fb2..6cdf89c638 100644
@@ -1162,6 +1162,19 @@ void riscv_trigger_reset_hold(CPURISCVState *env)
 
     /* init to type 2 triggers */
     for (i = 0; i < env->num_triggers; i++) {
+        int trigger_type = get_trigger_type(env, i);
+
+        switch (trigger_type) {
+        case TRIGGER_TYPE_AD_MATCH:
+            type2_breakpoint_remove(env, i);
+            break;
+        case TRIGGER_TYPE_AD_MATCH6:
+            type6_breakpoint_remove(env, i);
+            break;
+        default:
+            break;
+        }
+
         /*
          * type = TRIGGER_TYPE_AD_MATCH
          * dmode = 0 (both debug and M-mode can write tdata)
@@ -1178,8 +1191,6 @@ void riscv_trigger_reset_hold(CPURISCVState *env)
         env->tdata1[i] = tdata1;
         env->tdata2[i] = 0;
         env->tdata3[i] = 0;
-        env->cpu_breakpoint[i] = NULL;
-        env->cpu_watchpoint[i] = NULL;
         timer_del(env->itrigger_timer[i]);
     }
 
Signature · 1 line
2.43.0
#14 lore raw

PATCH 13/17 target/riscv/debug: Fix migration post_load icount_enabled() test

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

itrigger_enabled is relevant to the !icount_enabled() path, the test in debug_post_load() is inverted.

While we're at it, move debug CPU post_load details into debug.c since there's no need for machine.c to know internals of how debug.c maintains debug state.

Fixes 577f028694 ("target/riscv: Add itrigger_enabled field to CPURISCVState")
---
 target/riscv/machine.c   | 13 ++++++++-----
 target/riscv/tcg/debug.c |  9 ++++++++-
 target/riscv/tcg/debug.h |  2 +-
 3 files changed, 17 insertions(+), 7 deletions(-)
target/riscv/machine.c +8 -5
index 0ab613a298..82d2e11eb3 100644
@@ -228,18 +228,21 @@ static bool debug_needed(void *opaque)
 {
     RISCVCPU *cpu = opaque;
 
-    return cpu->cfg.debug;
+    if (kvm_enabled()) {
+        return false;
+    }
+
+    return tcg_enabled() && cpu->cfg.debug;
 }
 
 static int debug_post_load(void *opaque, int version_id)
 {
+#ifdef CONFIG_TCG
     RISCVCPU *cpu = opaque;
     CPURISCVState *env = &cpu->env;
 
-    if (icount_enabled()) {
-        env->itrigger_enabled = riscv_itrigger_enabled(env);
-    }
-
+    riscv_cpu_debug_post_load(env);
+#endif
     return 0;
 }
 
target/riscv/tcg/debug.c +8 -1
index 6cdf89c638..56f9ceb042 100644
@@ -675,7 +675,7 @@ static bool check_itrigger_priv(CPURISCVState *env, int index)
     return icount_priv_match(env, index);
 }
 
-bool riscv_itrigger_enabled(CPURISCVState *env)
+static bool riscv_itrigger_enabled(CPURISCVState *env)
 {
     int count;
 
@@ -1155,6 +1155,13 @@ void riscv_cpu_debug_change_priv(CPURISCVState *env)
     }
 }
 
+void riscv_cpu_debug_post_load(CPURISCVState *env)
+{
+    if (!icount_enabled()) {
+        env->itrigger_enabled = riscv_itrigger_enabled(env);
+    }
+}
+
 void riscv_trigger_reset_hold(CPURISCVState *env)
 {
     target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0);
target/riscv/tcg/debug.h +1 -1
index 88d34ecac9..6246bedff3 100644
@@ -151,9 +151,9 @@ void riscv_trigger_realize(CPURISCVState *env);
 void riscv_trigger_unrealize(CPURISCVState *env);
 void riscv_trigger_reset_hold(CPURISCVState *env);
 
-bool riscv_itrigger_enabled(CPURISCVState *env);
 void riscv_itrigger_update_priv(CPURISCVState *env);
 
 void riscv_cpu_debug_change_priv(CPURISCVState *env);
+void riscv_cpu_debug_post_load(CPURISCVState *env);
 
 #endif /* RISCV_DEBUG_H */
Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH for-11.1 13/17] target/riscv/debug: Fix migration post_load icount_enabled() test

Quoted 23 lines · Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

itrigger_enabled is relevant to the !icount_enabled() path, the test in debug_post_load() is inverted.

While we're at it, move debug CPU post_load details into debug.c since there's no need for machine.c to know internals of how debug.c maintains debug state.

Fixes 577f028694 ("target/riscv: Add itrigger_enabled field to CPURISCVState")
---
  target/riscv/machine.c   | 13 ++++++++-----
  target/riscv/tcg/debug.c |  9 ++++++++-
  target/riscv/tcg/debug.h |  2 +-
  3 files changed, 17 insertions(+), 7 deletions(-)
target/riscv/machine.c +0 -0
index 0ab613a298..82d2e11eb3 100644

target/riscv/machine.c static bool debug_needed(void *opaque) · line 236

Quoted 10 lines
target/riscv/machine.c +5 -1
@@ -228,18 +228,21 @@ static bool debug_needed(void *opaque)
  {
      RISCVCPU *cpu = opaque;
 
-    return cpu->cfg.debug;
+    if (kvm_enabled()) {
+        return false;
+    }
+
+    return tcg_enabled() && cpu->cfg.debug;
  }

kvm_enabled is redundant with tcg_enabled here -- only one will be true.

r~

#16 lore raw

PATCH 14/17 target/riscv/debug: Insert breakpoints after migration

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

After migration, TCG breakpoints should be re-established based on debug register state.

icount trigger timer in the icount_enabled() path is also missing, but that path will be removed subsequently so don't bother to fix it.

Fixes 577f028694 ("target/riscv: Add itrigger_enabled field to CPURISCVState")
---
 target/riscv/tcg/debug.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
target/riscv/tcg/debug.c +14 -0
index 56f9ceb042..09d4cc03f9 100644
@@ -1157,6 +1157,20 @@ void riscv_cpu_debug_change_priv(CPURISCVState *env)
 
 void riscv_cpu_debug_post_load(CPURISCVState *env)
 {
+    for (int i = 0; i < env->num_triggers; i++) {
+        int trigger_type = get_trigger_type(env, i);
+
+        switch (trigger_type) {
+        case TRIGGER_TYPE_AD_MATCH:
+            type2_breakpoint_insert(env, i);
+            break;
+        case TRIGGER_TYPE_AD_MATCH6:
+            type6_breakpoint_insert(env, i);
+            break;
+        default:
+            break;
+        }
+    }
     if (!icount_enabled()) {
         env->itrigger_enabled = riscv_itrigger_enabled(env);
     }
Signature · 1 line
2.43.0
#17 lore raw

PATCH 15/17 target/riscv/debug: Advertise icount trigger type in tinfo

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

icount triggers are supported so tinfo should advertise them.

Fixes c472c142a7 ("target/riscv: debug: Add initial support of type 6 trigger")
---
 target/riscv/tcg/debug.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
target/riscv/tcg/debug.c +6 -1
index 09d4cc03f9..9089d2ab78 100644
@@ -958,8 +958,13 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
 
 target_ulong tinfo_csr_read(CPURISCVState *env)
 {
-    /* assume all triggers support the same types of triggers */
+    /*
+     * Assume all triggers support the same types of triggers
+     * TODO: set 1 (version 1.0) in the version field when we're
+     * ready to support it along with 0.13.
+     */
     return BIT(TRIGGER_TYPE_AD_MATCH) |
+           BIT(TRIGGER_TYPE_INST_CNT) |
            BIT(TRIGGER_TYPE_AD_MATCH6);
 }
 
Signature · 1 line
2.43.0
#18 lore raw

PATCH 16/17 target/riscv/debug: Reset trigger type to unavailable

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

Reset triggers to type 15 (unavailable). The reset value for tdata1 type field is implementation specific, legal values are permitted. Unavailable is a legal value, and some implementations may not support type2 triggers so use that instead.

Fixes b6092544fc ("target/riscv: csr: Hook debug CSR read/write")
---
 target/riscv/tcg/debug.c | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)
target/riscv/tcg/debug.c +2 -15
index 9089d2ab78..50e5a85caf 100644
@@ -1183,10 +1183,10 @@ void riscv_cpu_debug_post_load(CPURISCVState *env)
 
 void riscv_trigger_reset_hold(CPURISCVState *env)
 {
-    target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0);
+    target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_UNAVAIL, 0, 0);
     int i;
 
-    /* init to type 2 triggers */
+    /* Init to type 15 (unavailable) triggers */
     for (i = 0; i < env->num_triggers; i++) {
         int trigger_type = get_trigger_type(env, i);
 
@@ -1201,19 +1201,6 @@ void riscv_trigger_reset_hold(CPURISCVState *env)
             break;
         }
 
-        /*
-         * type = TRIGGER_TYPE_AD_MATCH
-         * dmode = 0 (both debug and M-mode can write tdata)
-         * maskmax = 0 (unimplemented, always 0)
-         * sizehi = 0 (match against any size, RV64 only)
-         * hit = 0 (unimplemented, always 0)
-         * select = 0 (always 0, perform match on address)
-         * timing = 0 (always 0, trigger before instruction)
-         * sizelo = 0 (match against any size)
-         * action = 0 (always 0, raise a breakpoint exception)
-         * chain = 0 (unimplemented, always 0)
-         * match = 0 (always 0, when any compare value equals tdata2)
-         */
         env->tdata1[i] = tdata1;
         env->tdata2[i] = 0;
         env->tdata3[i] = 0;
Signature · 1 line
2.43.0
#19 lore raw

PATCH 17/17 target/riscv/debug: Fix minor comment typos

In reply to Daniel Henrique Barboza

From: Nicholas Piggin <[email protected]>

Fixes a42bd00166 ("target/riscv: debug: Determine the trigger type from tdata1.type")
Fixes d1c111411e ("target/riscv: debug: Create common trigger actions function")
---
 target/riscv/tcg/debug.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/riscv/tcg/debug.c +2 -2
index 50e5a85caf..27c2e0da25 100644
@@ -122,7 +122,7 @@ static trigger_action_t get_trigger_action(CPURISCVState *env,
         break;
     case TRIGGER_TYPE_NO_EXIST:
     case TRIGGER_TYPE_UNAVAIL:
-        qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n",
+        qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exist\n",
                       trigger_type);
         break;
     default:
@@ -944,7 +944,7 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
                       trigger_type);
         break;
     case TRIGGER_TYPE_NO_EXIST:
-        qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n",
+        qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exist\n",
                       trigger_type);
         break;
     default:
Signature · 1 line
2.43.0