@@ -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 )
@@ -1561,6 +1561,52 @@ int riscv_remove_watchpoint(struct target *target,
1561
1561
return ERROR_OK ;
1562
1562
}
1563
1563
1564
+ typedef enum {
1565
+ M6_HIT_ERROR ,
1566
+ M6_HIT_NOT_SUPPORTED ,
1567
+ M6_NOT_HIT ,
1568
+ M6_HIT_BEFORE ,
1569
+ M6_HIT_AFTER ,
1570
+ M6_HIT_IMM_AFTER
1571
+ } mctrl6hitstatus ;
1572
+
1573
+ static mctrl6hitstatus check_mcontrol6_hit_status (struct target * target ,
1574
+ riscv_reg_t tdata1 , uint64_t hit_mask )
1575
+ {
1576
+ uint32_t hit0 = get_field (tdata1 , CSR_MCONTROL6_HIT0 );
1577
+ uint32_t hit1 = get_field (tdata1 , CSR_MCONTROL6_HIT1 );
1578
+ uint32_t hit_info = (hit1 << 1 ) | hit0 ;
1579
+ if (hit_info == CSR_MCONTROL6_HIT0_BEFORE )
1580
+ return M6_HIT_BEFORE ;
1581
+
1582
+ if (hit_info == CSR_MCONTROL6_HIT0_AFTER )
1583
+ return M6_HIT_AFTER ;
1584
+
1585
+ if (hit_info == CSR_MCONTROL6_HIT0_IMMEDIATELY_AFTER )
1586
+ return M6_HIT_IMM_AFTER ;
1587
+
1588
+ if (hit_info == CSR_MCONTROL6_HIT0_FALSE ) {
1589
+ riscv_reg_t tdata1_tests [] = {
1590
+ set_field (tdata1 , CSR_MCONTROL6_HIT0 , 1 ),
1591
+ set_field (tdata1 , CSR_MCONTROL6_HIT1 , 1 ),
1592
+ set_field (tdata1 , CSR_MCONTROL6_HIT0 , 1 ) | field_value (CSR_MCONTROL6_HIT1 , 1 )
1593
+ };
1594
+ riscv_reg_t tdata1_test_rb ;
1595
+ for (uint64_t i = 0 ; i < ARRAY_SIZE (tdata1_tests ); ++ i ) {
1596
+ if (riscv_reg_set (target , GDB_REGNO_TDATA1 , tdata1_tests [i ]) != ERROR_OK )
1597
+ return M6_HIT_ERROR ;
1598
+ if (riscv_reg_get (target , & tdata1_test_rb , GDB_REGNO_TDATA1 ) != ERROR_OK )
1599
+ return M6_HIT_ERROR ;
1600
+ if (riscv_reg_set (target , GDB_REGNO_TDATA1 , tdata1_test_rb & ~hit_mask ) != ERROR_OK )
1601
+ return M6_HIT_ERROR ;
1602
+ if (tdata1_test_rb == tdata1_tests [i ])
1603
+ return M6_NOT_HIT ;
1604
+ }
1605
+ }
1606
+ return M6_HIT_NOT_SUPPORTED ;
1607
+ }
1608
+
1609
+
1564
1610
/**
1565
1611
* Look at the trigger hit bits to find out which trigger is the reason we're
1566
1612
* halted. Sets *unique_id to the unique ID of that trigger. If *unique_id is
@@ -1576,6 +1622,7 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
1576
1622
1577
1623
// FIXME: Add hit bits support detection and caching
1578
1624
RISCV_INFO (r );
1625
+ r -> need_single_step = false;
1579
1626
1580
1627
riscv_reg_t tselect ;
1581
1628
if (riscv_reg_get (target , & tselect , GDB_REGNO_TSELECT ) != ERROR_OK )
@@ -1601,9 +1648,24 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
1601
1648
break ;
1602
1649
case CSR_TDATA1_TYPE_MCONTROL :
1603
1650
hit_mask = CSR_MCONTROL_HIT ;
1651
+ r -> need_single_step = true;
1604
1652
break ;
1605
1653
case CSR_TDATA1_TYPE_MCONTROL6 :
1606
- hit_mask = CSR_MCONTROL6_HIT0 | CSR_MCONTROL6_HIT1 ;
1654
+ riscv_reg_t tinfo , tinfo_version ;
1655
+ int result = riscv_reg_get (target , & tinfo , GDB_REGNO_TINFO );
1656
+ if (result == ERROR_OK ) {
1657
+ tinfo_version = get_field (tinfo , CSR_TINFO_VERSION );
1658
+ if (tinfo_version == CSR_TINFO_VERSION_0 )
1659
+ r -> need_single_step = true;
1660
+ }
1661
+ if (result != ERROR_OK || tinfo_version == CSR_TINFO_VERSION_1 ) {
1662
+ mctrl6hitstatus hits_status = check_mcontrol6_hit_status (target ,
1663
+ tdata1 , hit_mask );
1664
+ if (hits_status == M6_HIT_ERROR )
1665
+ return ERROR_FAIL ;
1666
+ if (hits_status == M6_HIT_BEFORE || hits_status == M6_HIT_NOT_SUPPORTED )
1667
+ r -> need_single_step = true;
1668
+ }
1607
1669
break ;
1608
1670
case CSR_TDATA1_TYPE_ICOUNT :
1609
1671
hit_mask = CSR_ICOUNT_HIT ;
@@ -2553,10 +2615,19 @@ static int resume_prep(struct target *target, int current,
2553
2615
if (handle_breakpoints ) {
2554
2616
/* To be able to run off a trigger, we perform a step operation and then
2555
2617
* 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 ;
2618
+ * pending breakpoints so we can safely perform the step.
2619
+ *
2620
+ * Two cases where single step is needed before resuming:
2621
+ * 1. ebreak used in software breakpoint;
2622
+ * 2. a trigger that is taken just before the instruction that triggered it is retired.
2623
+ */
2624
+ if (target -> debug_reason == DBG_REASON_BREAKPOINT
2625
+ || (target -> debug_reason == DBG_REASON_WATCHPOINT
2626
+ && r -> need_single_step )) {
2627
+ if (old_or_new_riscv_step_impl (target , current , address , handle_breakpoints ,
2628
+ false /* callbacks are not called */ ) != ERROR_OK )
2629
+ return ERROR_FAIL ;
2630
+ }
2560
2631
}
2561
2632
2562
2633
if (r -> get_hart_state ) {
0 commit comments