@@ -623,12 +623,12 @@ static int find_first_trigger_by_id(struct target *target, int unique_id)
623
623
624
624
static unsigned int count_trailing_ones (riscv_reg_t reg )
625
625
{
626
- assert ( sizeof (riscv_reg_t ) * 8 == 64 ) ;
627
- for (unsigned int i = 0 ; i < 64 ; i ++ ) {
626
+ const unsigned int riscv_reg_bits = sizeof (riscv_reg_t ) * CHAR_BIT ;
627
+ for (unsigned int i = 0 ; i < riscv_reg_bits ; i ++ ) {
628
628
if ((1 & (reg >> i )) == 0 )
629
629
return i ;
630
630
}
631
- return 64 ;
631
+ return riscv_reg_bits ;
632
632
}
633
633
634
634
static int set_trigger (struct target * target , unsigned int idx , riscv_reg_t tdata1 , riscv_reg_t tdata2 )
@@ -668,6 +668,16 @@ static int set_trigger(struct target *target, unsigned int idx, riscv_reg_t tdat
668
668
const uint32_t type = get_field (tdata1 , CSR_TDATA1_TYPE (riscv_xlen (target )));
669
669
const bool is_mcontrol = type == CSR_TDATA1_TYPE_MCONTROL ;
670
670
671
+ if (type == CSR_TDATA1_TYPE_MCONTROL ) {
672
+ if (get_field (tdata1_rb , CSR_MCONTROL_TIMING ) == CSR_MCONTROL_TIMING_BEFORE )
673
+ r -> halt_before_dpc = true;
674
+ } else if (type == CSR_TDATA1_TYPE_MCONTROL6 ) {
675
+ int hit0 = get_field (tdata1_rb , CSR_MCONTROL6_HIT0 );
676
+ int hit1 = get_field (tdata1_rb , CSR_MCONTROL6_HIT1 );
677
+ if (((hit1 << 1 ) | hit0 ) == CSR_MCONTROL6_HIT0_BEFORE )
678
+ r -> halt_before_dpc = true;
679
+ }
680
+
671
681
/* Determine if tdata1 supports what we need.
672
682
* For mcontrol triggers, we don't care about
673
683
* the value in the read-only "maskmax" field.
@@ -2547,16 +2557,29 @@ static int resume_prep(struct target *target, int current,
2547
2557
assert (target -> state == TARGET_HALTED );
2548
2558
RISCV_INFO (r );
2549
2559
2560
+ riscv_reg_t dcsr ;
2561
+ if (riscv_reg_get (target , & dcsr , GDB_REGNO_DCSR ) != ERROR_OK )
2562
+ return ERROR_FAIL ;
2563
+
2550
2564
if (!current && riscv_reg_set (target , GDB_REGNO_PC , address ) != ERROR_OK )
2551
2565
return ERROR_FAIL ;
2552
2566
2553
2567
if (handle_breakpoints ) {
2554
2568
/* To be able to run off a trigger, we perform a step operation and then
2555
2569
* resume. If handle_breakpoints is true then step temporarily disables
2556
- * pending breakpoints so we can safely perform the step. */
2557
- if (old_or_new_riscv_step_impl (target , current , address , handle_breakpoints ,
2558
- false /* callbacks are not called */ ) != ERROR_OK )
2559
- return ERROR_FAIL ;
2570
+ * pending breakpoints so we can safely perform the step.
2571
+ *
2572
+ * Two cases where single step is needed before resuming:
2573
+ * 1. ebreak used in software breakpoint;
2574
+ * 2. a trigger that is taken just before the instruction that triggered it is retired.
2575
+ */
2576
+ if (get_field (dcsr , CSR_DCSR_CAUSE ) == CSR_DCSR_CAUSE_EBREAK
2577
+ || (get_field (dcsr , CSR_DCSR_CAUSE ) == CSR_DCSR_CAUSE_TRIGGER
2578
+ && r -> halt_before_dpc )) {
2579
+ if (old_or_new_riscv_step_impl (target , current , address , handle_breakpoints ,
2580
+ false /* callbacks are not called */ ) != ERROR_OK )
2581
+ return ERROR_FAIL ;
2582
+ }
2560
2583
}
2561
2584
2562
2585
if (r -> get_hart_state ) {
0 commit comments