@@ -648,12 +648,12 @@ static int find_first_trigger_by_id(struct target *target, int unique_id)
648
648
649
649
static unsigned int count_trailing_ones (riscv_reg_t reg )
650
650
{
651
- assert ( sizeof (riscv_reg_t ) * 8 == 64 ) ;
652
- for (unsigned int i = 0 ; i < 64 ; i ++ ) {
651
+ const unsigned int riscv_reg_bits = sizeof (riscv_reg_t ) * CHAR_BIT ;
652
+ for (unsigned int i = 0 ; i < riscv_reg_bits ; i ++ ) {
653
653
if ((1 & (reg >> i )) == 0 )
654
654
return i ;
655
655
}
656
- return 64 ;
656
+ return riscv_reg_bits ;
657
657
}
658
658
659
659
static int set_trigger (struct target * target , unsigned int idx , riscv_reg_t tdata1 , riscv_reg_t tdata2 )
@@ -1586,21 +1586,75 @@ int riscv_remove_watchpoint(struct target *target,
1586
1586
return ERROR_OK ;
1587
1587
}
1588
1588
1589
+ typedef enum {
1590
+ M6_HIT_ERROR ,
1591
+ M6_HIT_NOT_SUPPORTED ,
1592
+ M6_NOT_HIT ,
1593
+ M6_HIT_BEFORE ,
1594
+ M6_HIT_AFTER ,
1595
+ M6_HIT_IMM_AFTER
1596
+ } mctrl6hitstatus ;
1597
+
1598
+ static mctrl6hitstatus check_mcontrol6_hit_status (struct target * target ,
1599
+ riscv_reg_t tdata1 , uint64_t hit_mask )
1600
+ {
1601
+ const uint32_t hit0 = get_field (tdata1 , CSR_MCONTROL6_HIT0 );
1602
+ const uint32_t hit1 = get_field (tdata1 , CSR_MCONTROL6_HIT1 );
1603
+ const uint32_t hit_info = (hit1 << 1 ) | hit0 ;
1604
+ if (hit_info == CSR_MCONTROL6_HIT0_BEFORE )
1605
+ return M6_HIT_BEFORE ;
1606
+
1607
+ if (hit_info == CSR_MCONTROL6_HIT0_AFTER )
1608
+ return M6_HIT_AFTER ;
1609
+
1610
+ if (hit_info == CSR_MCONTROL6_HIT0_IMMEDIATELY_AFTER )
1611
+ return M6_HIT_IMM_AFTER ;
1612
+
1613
+ if (hit_info == CSR_MCONTROL6_HIT0_FALSE ) {
1614
+ /* hit[1..0] equals 0, which can mean one of the following:
1615
+ * - "hit" bits are supported and this trigger has not fired
1616
+ * - "hit" bits are not supported on this trigger
1617
+ * To distinguish these two cases, try writing all non-zero bit
1618
+ * patterns to hit[1..0] to determine if the "hit" bits are supported:
1619
+ */
1620
+ riscv_reg_t tdata1_tests [] = {
1621
+ set_field (tdata1 , CSR_MCONTROL6_HIT0 , 1 ),
1622
+ set_field (tdata1 , CSR_MCONTROL6_HIT1 , 1 ),
1623
+ set_field (tdata1 , CSR_MCONTROL6_HIT0 , 1 ) | field_value (CSR_MCONTROL6_HIT1 , 1 )
1624
+ };
1625
+ riscv_reg_t tdata1_test_rb ;
1626
+ for (uint64_t i = 0 ; i < ARRAY_SIZE (tdata1_tests ); ++ i ) {
1627
+ if (riscv_reg_set (target , GDB_REGNO_TDATA1 , tdata1_tests [i ]) != ERROR_OK )
1628
+ return M6_HIT_ERROR ;
1629
+ if (riscv_reg_get (target , & tdata1_test_rb , GDB_REGNO_TDATA1 ) != ERROR_OK )
1630
+ return M6_HIT_ERROR ;
1631
+ if (tdata1_test_rb == tdata1_tests [i ]) {
1632
+ if (riscv_reg_set (target , GDB_REGNO_TDATA1 , tdata1_test_rb & ~hit_mask ) != ERROR_OK )
1633
+ return M6_HIT_ERROR ;
1634
+ return M6_NOT_HIT ;
1635
+ }
1636
+ }
1637
+ }
1638
+ return M6_HIT_NOT_SUPPORTED ;
1639
+ }
1640
+
1589
1641
/**
1590
1642
* Look at the trigger hit bits to find out which trigger is the reason we're
1591
1643
* halted. Sets *unique_id to the unique ID of that trigger. If *unique_id is
1592
1644
* RISCV_TRIGGER_HIT_NOT_FOUND, no match was found.
1593
1645
*/
1594
1646
1595
- static int riscv_trigger_detect_hit_bits (struct target * target , int64_t * unique_id )
1647
+ static int riscv_trigger_detect_hit_bits (struct target * target , int64_t * unique_id ,
1648
+ bool * need_single_step )
1596
1649
{
1597
1650
/* FIXME: this function assumes that we have only one trigger that can
1598
1651
* have hit bit set. Debug spec allows hit bit to bit set if a trigger has
1599
1652
* matched but did not fire. Such targets will receive erroneous results.
1600
1653
*/
1601
1654
1602
- // FIXME: Add hit bits support detection and caching
1603
1655
RISCV_INFO (r );
1656
+ assert (need_single_step );
1657
+ * need_single_step = false;
1604
1658
1605
1659
riscv_reg_t tselect ;
1606
1660
if (riscv_reg_get (target , & tselect , GDB_REGNO_TSELECT ) != ERROR_OK )
@@ -1626,9 +1680,21 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
1626
1680
break ;
1627
1681
case CSR_TDATA1_TYPE_MCONTROL :
1628
1682
hit_mask = CSR_MCONTROL_HIT ;
1683
+ * need_single_step = true;
1629
1684
break ;
1630
1685
case CSR_TDATA1_TYPE_MCONTROL6 :
1631
1686
hit_mask = CSR_MCONTROL6_HIT0 | CSR_MCONTROL6_HIT1 ;
1687
+ if (r -> tinfo_version == CSR_TINFO_VERSION_0 ) {
1688
+ * need_single_step = true;
1689
+ } else if (r -> tinfo_version == RISCV_TINFO_VERSION_UNKNOWN
1690
+ || r -> tinfo_version == CSR_TINFO_VERSION_1 ) {
1691
+ mctrl6hitstatus hits_status = check_mcontrol6_hit_status (target ,
1692
+ tdata1 , hit_mask );
1693
+ if (hits_status == M6_HIT_ERROR )
1694
+ return ERROR_FAIL ;
1695
+ if (hits_status == M6_HIT_BEFORE || hits_status == M6_HIT_NOT_SUPPORTED )
1696
+ * need_single_step = true;
1697
+ }
1632
1698
break ;
1633
1699
case CSR_TDATA1_TYPE_ICOUNT :
1634
1700
hit_mask = CSR_ICOUNT_HIT ;
@@ -1647,8 +1713,9 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
1647
1713
/* FIXME: this logic needs to be changed to ignore triggers that are not
1648
1714
* the last one in the chain. */
1649
1715
if (tdata1 & hit_mask ) {
1650
- LOG_TARGET_DEBUG (target , "Trigger %u (unique_id=%" PRIi64 ") has hit bit set." ,
1651
- i , r -> trigger_unique_id [i ]);
1716
+ LOG_TARGET_DEBUG (target , "Trigger %u (unique_id=%" PRIi64
1717
+ ") has hit bit set. (need_single_step=%s)" ,
1718
+ i , r -> trigger_unique_id [i ], (* need_single_step ) ? "yes" : "no" );
1652
1719
if (riscv_reg_set (target , GDB_REGNO_TDATA1 , tdata1 & ~hit_mask ) != ERROR_OK )
1653
1720
return ERROR_FAIL ;
1654
1721
@@ -2310,13 +2377,15 @@ static int set_debug_reason(struct target *target, enum riscv_halt_reason halt_r
2310
2377
{
2311
2378
RISCV_INFO (r );
2312
2379
r -> trigger_hit = -1 ;
2380
+ r -> need_single_step = false;
2313
2381
switch (halt_reason ) {
2314
2382
case RISCV_HALT_EBREAK :
2315
2383
target -> debug_reason = DBG_REASON_BREAKPOINT ;
2316
2384
break ;
2317
2385
case RISCV_HALT_TRIGGER :
2318
2386
target -> debug_reason = DBG_REASON_UNDEFINED ;
2319
- if (riscv_trigger_detect_hit_bits (target , & r -> trigger_hit ) != ERROR_OK )
2387
+ if (riscv_trigger_detect_hit_bits (target , & r -> trigger_hit ,
2388
+ & r -> need_single_step ) != ERROR_OK )
2320
2389
return ERROR_FAIL ;
2321
2390
// FIXME: handle multiple hit bits
2322
2391
if (r -> trigger_hit != RISCV_TRIGGER_HIT_NOT_FOUND ) {
@@ -2578,10 +2647,19 @@ static int resume_prep(struct target *target, int current,
2578
2647
if (handle_breakpoints ) {
2579
2648
/* To be able to run off a trigger, we perform a step operation and then
2580
2649
* resume. If handle_breakpoints is true then step temporarily disables
2581
- * pending breakpoints so we can safely perform the step. */
2582
- if (old_or_new_riscv_step_impl (target , current , address , handle_breakpoints ,
2583
- false /* callbacks are not called */ ) != ERROR_OK )
2584
- return ERROR_FAIL ;
2650
+ * pending breakpoints so we can safely perform the step.
2651
+ *
2652
+ * Two cases where single step is needed before resuming:
2653
+ * 1. ebreak used in software breakpoint;
2654
+ * 2. a trigger that is taken just before the instruction that triggered it is retired.
2655
+ */
2656
+ if (target -> debug_reason == DBG_REASON_BREAKPOINT
2657
+ || (target -> debug_reason == DBG_REASON_WATCHPOINT
2658
+ && r -> need_single_step )) {
2659
+ if (old_or_new_riscv_step_impl (target , current , address , handle_breakpoints ,
2660
+ false /* callbacks are not called */ ) != ERROR_OK )
2661
+ return ERROR_FAIL ;
2662
+ }
2585
2663
}
2586
2664
2587
2665
if (r -> get_hart_state ) {
@@ -5848,6 +5926,19 @@ int riscv_enumerate_triggers(struct target *target)
5848
5926
return ERROR_OK ;
5849
5927
}
5850
5928
5929
+ /* Obtaining tinfo.version value once.
5930
+ * No need to enumerate per-trigger.
5931
+ * See https://github.com/riscv/riscv-debug-spec/pull/1081.
5932
+ */
5933
+ riscv_reg_t tinfo ;
5934
+ if (riscv_reg_get (target , & tinfo , GDB_REGNO_TINFO ) == ERROR_OK ) {
5935
+ r -> tinfo_version = get_field (tinfo , CSR_TINFO_VERSION );
5936
+ LOG_TARGET_DEBUG (target , "Trigger tinfo.version = %d." , r -> tinfo_version );
5937
+ } else {
5938
+ r -> tinfo_version = RISCV_TINFO_VERSION_UNKNOWN ;
5939
+ LOG_TARGET_DEBUG (target , "Trigger tinfo.version is unknown." );
5940
+ }
5941
+
5851
5942
unsigned int t = 0 ;
5852
5943
for (; t < ARRAY_SIZE (r -> trigger_tinfo ); ++ t ) {
5853
5944
result = check_if_trigger_exists (target , t );
0 commit comments