@@ -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 )
@@ -1576,6 +1576,7 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
1576
1576
1577
1577
// FIXME: Add hit bits support detection and caching
1578
1578
RISCV_INFO (r );
1579
+ r -> need_single_step = false;
1579
1580
1580
1581
riscv_reg_t tselect ;
1581
1582
if (riscv_reg_get (target , & tselect , GDB_REGNO_TSELECT ) != ERROR_OK )
@@ -1601,9 +1602,17 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
1601
1602
break ;
1602
1603
case CSR_TDATA1_TYPE_MCONTROL :
1603
1604
hit_mask = CSR_MCONTROL_HIT ;
1605
+ if (get_field (tdata1 , CSR_MCONTROL_TIMING ) == CSR_MCONTROL_TIMING_BEFORE )
1606
+ r -> need_single_step = true;
1604
1607
break ;
1605
1608
case CSR_TDATA1_TYPE_MCONTROL6 :
1606
1609
hit_mask = CSR_MCONTROL6_HIT0 | CSR_MCONTROL6_HIT1 ;
1610
+ int hit0 = get_field (tdata1 , CSR_MCONTROL6_HIT0 );
1611
+ int hit1 = get_field (tdata1 , CSR_MCONTROL6_HIT1 );
1612
+ int trigger_retired_info = (hit1 << 1 ) | hit0 ;
1613
+ if (trigger_retired_info == CSR_MCONTROL6_HIT0_FALSE
1614
+ || trigger_retired_info == CSR_MCONTROL6_HIT0_BEFORE )
1615
+ r -> need_single_step = true;
1607
1616
break ;
1608
1617
case CSR_TDATA1_TYPE_ICOUNT :
1609
1618
hit_mask = CSR_ICOUNT_HIT ;
@@ -2547,16 +2556,29 @@ static int resume_prep(struct target *target, int current,
2547
2556
assert (target -> state == TARGET_HALTED );
2548
2557
RISCV_INFO (r );
2549
2558
2559
+ riscv_reg_t dcsr ;
2560
+ if (riscv_reg_get (target , & dcsr , GDB_REGNO_DCSR ) != ERROR_OK )
2561
+ return ERROR_FAIL ;
2562
+
2550
2563
if (!current && riscv_reg_set (target , GDB_REGNO_PC , address ) != ERROR_OK )
2551
2564
return ERROR_FAIL ;
2552
2565
2553
2566
if (handle_breakpoints ) {
2554
2567
/* To be able to run off a trigger, we perform a step operation and then
2555
2568
* 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 ;
2569
+ * pending breakpoints so we can safely perform the step.
2570
+ *
2571
+ * Two cases where single step is needed before resuming:
2572
+ * 1. ebreak used in software breakpoint;
2573
+ * 2. a trigger that is taken just before the instruction that triggered it is retired.
2574
+ */
2575
+ if (get_field (dcsr , CSR_DCSR_CAUSE ) == CSR_DCSR_CAUSE_EBREAK
2576
+ || (get_field (dcsr , CSR_DCSR_CAUSE ) == CSR_DCSR_CAUSE_TRIGGER
2577
+ && r -> need_single_step )) {
2578
+ if (old_or_new_riscv_step_impl (target , current , address , handle_breakpoints ,
2579
+ false /* callbacks are not called */ ) != ERROR_OK )
2580
+ return ERROR_FAIL ;
2581
+ }
2560
2582
}
2561
2583
2562
2584
if (r -> get_hart_state ) {
0 commit comments