@@ -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,13 +1561,69 @@ 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
+ /* TODO Record info:mcontrol6_supports_hit, This way
1574
+ * the number of reads and writes of TDATA1 will be
1575
+ * greatly reduced.
1576
+ */
1577
+ static mctrl6hitstatus check_mcontrol6_hit_status (struct target * target ,
1578
+ riscv_reg_t tdata1 , uint64_t hit_mask )
1579
+ {
1580
+ const uint32_t hit0 = get_field (tdata1 , CSR_MCONTROL6_HIT0 );
1581
+ const uint32_t hit1 = get_field (tdata1 , CSR_MCONTROL6_HIT1 );
1582
+ const uint32_t hit_info = (hit1 << 1 ) | hit0 ;
1583
+ if (hit_info == CSR_MCONTROL6_HIT0_BEFORE )
1584
+ return M6_HIT_BEFORE ;
1585
+
1586
+ if (hit_info == CSR_MCONTROL6_HIT0_AFTER )
1587
+ return M6_HIT_AFTER ;
1588
+
1589
+ if (hit_info == CSR_MCONTROL6_HIT0_IMMEDIATELY_AFTER )
1590
+ return M6_HIT_IMM_AFTER ;
1591
+
1592
+ if (hit_info == CSR_MCONTROL6_HIT0_FALSE ) {
1593
+ /* hit[1..0] equals 0, which can mean one of the following:
1594
+ * - "hit" bits are supported and this trigger has not fired
1595
+ * - "hit" bits are not supported on this trigger
1596
+ * To distinguish these two cases, try writing all non-zero bit
1597
+ * patterns to hit[1..0] to determine if the "hit" bits are supported:
1598
+ */
1599
+ riscv_reg_t tdata1_tests [] = {
1600
+ set_field (tdata1 , CSR_MCONTROL6_HIT0 , 1 ),
1601
+ set_field (tdata1 , CSR_MCONTROL6_HIT1 , 1 ),
1602
+ set_field (tdata1 , CSR_MCONTROL6_HIT0 , 1 ) | field_value (CSR_MCONTROL6_HIT1 , 1 )
1603
+ };
1604
+ riscv_reg_t tdata1_test_rb ;
1605
+ for (uint64_t i = 0 ; i < ARRAY_SIZE (tdata1_tests ); ++ i ) {
1606
+ if (riscv_reg_set (target , GDB_REGNO_TDATA1 , tdata1_tests [i ]) != ERROR_OK )
1607
+ return M6_HIT_ERROR ;
1608
+ if (riscv_reg_get (target , & tdata1_test_rb , GDB_REGNO_TDATA1 ) != ERROR_OK )
1609
+ return M6_HIT_ERROR ;
1610
+ if (riscv_reg_set (target , GDB_REGNO_TDATA1 , tdata1_test_rb & ~hit_mask ) != ERROR_OK )
1611
+ return M6_HIT_ERROR ;
1612
+ if (tdata1_test_rb == tdata1_tests [i ])
1613
+ return M6_NOT_HIT ;
1614
+ }
1615
+ }
1616
+ return M6_HIT_NOT_SUPPORTED ;
1617
+ }
1618
+
1564
1619
/**
1565
1620
* Look at the trigger hit bits to find out which trigger is the reason we're
1566
1621
* halted. Sets *unique_id to the unique ID of that trigger. If *unique_id is
1567
1622
* RISCV_TRIGGER_HIT_NOT_FOUND, no match was found.
1568
1623
*/
1569
1624
1570
- static int riscv_trigger_detect_hit_bits (struct target * target , int64_t * unique_id )
1625
+ static int riscv_trigger_detect_hit_bits (struct target * target , int64_t * unique_id ,
1626
+ bool * need_single_step )
1571
1627
{
1572
1628
/* FIXME: this function assumes that we have only one trigger that can
1573
1629
* have hit bit set. Debug spec allows hit bit to bit set if a trigger has
@@ -1601,9 +1657,21 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
1601
1657
break ;
1602
1658
case CSR_TDATA1_TYPE_MCONTROL :
1603
1659
hit_mask = CSR_MCONTROL_HIT ;
1660
+ r -> need_single_step = true;
1604
1661
break ;
1605
1662
case CSR_TDATA1_TYPE_MCONTROL6 :
1606
1663
hit_mask = CSR_MCONTROL6_HIT0 | CSR_MCONTROL6_HIT1 ;
1664
+ if (r -> trigger_tinfo_version [i ] == CSR_TINFO_VERSION_0 ) {
1665
+ r -> need_single_step = true;
1666
+ } else if (r -> trigger_tinfo_version [i ] == ERROR_TARGET_RESOURCE_NOT_AVAILABLE
1667
+ || r -> trigger_tinfo_version [i ] == CSR_TINFO_VERSION_1 ) {
1668
+ mctrl6hitstatus hits_status = check_mcontrol6_hit_status (target ,
1669
+ tdata1 , hit_mask );
1670
+ if (hits_status == M6_HIT_ERROR )
1671
+ return ERROR_FAIL ;
1672
+ if (hits_status == M6_HIT_BEFORE || hits_status == M6_HIT_NOT_SUPPORTED )
1673
+ r -> need_single_step = true;
1674
+ }
1607
1675
break ;
1608
1676
case CSR_TDATA1_TYPE_ICOUNT :
1609
1677
hit_mask = CSR_ICOUNT_HIT ;
@@ -1622,8 +1690,9 @@ static int riscv_trigger_detect_hit_bits(struct target *target, int64_t *unique_
1622
1690
/* FIXME: this logic needs to be changed to ignore triggers that are not
1623
1691
* the last one in the chain. */
1624
1692
if (tdata1 & hit_mask ) {
1625
- LOG_TARGET_DEBUG (target , "Trigger %u (unique_id=%" PRIi64 ") has hit bit set." ,
1626
- i , r -> trigger_unique_id [i ]);
1693
+ LOG_TARGET_DEBUG (target , "Trigger %u (unique_id=%" PRIi64 ""
1694
+ ", need_single_step=%" PRId32 ") has hit bit set." ,
1695
+ i , r -> trigger_unique_id [i ], r -> need_single_step );
1627
1696
if (riscv_reg_set (target , GDB_REGNO_TDATA1 , tdata1 & ~hit_mask ) != ERROR_OK )
1628
1697
return ERROR_FAIL ;
1629
1698
@@ -2285,13 +2354,15 @@ static int set_debug_reason(struct target *target, enum riscv_halt_reason halt_r
2285
2354
{
2286
2355
RISCV_INFO (r );
2287
2356
r -> trigger_hit = -1 ;
2357
+ r -> need_single_step = false;
2288
2358
switch (halt_reason ) {
2289
2359
case RISCV_HALT_EBREAK :
2290
2360
target -> debug_reason = DBG_REASON_BREAKPOINT ;
2291
2361
break ;
2292
2362
case RISCV_HALT_TRIGGER :
2293
2363
target -> debug_reason = DBG_REASON_UNDEFINED ;
2294
- if (riscv_trigger_detect_hit_bits (target , & r -> trigger_hit ) != ERROR_OK )
2364
+ if (riscv_trigger_detect_hit_bits (target , & r -> trigger_hit ,
2365
+ & r -> need_single_step ) != ERROR_OK )
2295
2366
return ERROR_FAIL ;
2296
2367
// FIXME: handle multiple hit bits
2297
2368
if (r -> trigger_hit != RISCV_TRIGGER_HIT_NOT_FOUND ) {
@@ -2553,10 +2624,19 @@ static int resume_prep(struct target *target, int current,
2553
2624
if (handle_breakpoints ) {
2554
2625
/* To be able to run off a trigger, we perform a step operation and then
2555
2626
* 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 ;
2627
+ * pending breakpoints so we can safely perform the step.
2628
+ *
2629
+ * Two cases where single step is needed before resuming:
2630
+ * 1. ebreak used in software breakpoint;
2631
+ * 2. a trigger that is taken just before the instruction that triggered it is retired.
2632
+ */
2633
+ if (target -> debug_reason == DBG_REASON_BREAKPOINT
2634
+ || (target -> debug_reason == DBG_REASON_WATCHPOINT
2635
+ && r -> need_single_step )) {
2636
+ if (old_or_new_riscv_step_impl (target , current , address , handle_breakpoints ,
2637
+ false /* callbacks are not called */ ) != ERROR_OK )
2638
+ return ERROR_FAIL ;
2639
+ }
2560
2640
}
2561
2641
2562
2642
if (r -> get_hart_state ) {
@@ -5698,19 +5778,21 @@ static int check_if_trigger_exists(struct target *target, unsigned int index)
5698
5778
* to determine trigger types supported by a trigger.
5699
5779
* It is assumed that the trigger is already selected via writing `tselect`.
5700
5780
*/
5701
- static int get_trigger_types (struct target * target , unsigned int * trigger_tinfo ,
5702
- riscv_reg_t tdata1 )
5781
+ static int get_trigger_types_and_version (struct target * target , unsigned int * trigger_tinfo ,
5782
+ int64_t * trigger_tinfo_version , riscv_reg_t tdata1 )
5703
5783
{
5704
5784
assert (trigger_tinfo );
5705
5785
riscv_reg_t tinfo ;
5706
5786
if (riscv_reg_get (target , & tinfo , GDB_REGNO_TINFO ) == ERROR_OK ) {
5787
+ * trigger_tinfo_version = get_field (tinfo , CSR_TINFO_VERSION );
5707
5788
/* tinfo.INFO == 1: trigger doesn’t exist
5708
5789
* tinfo == 0 or tinfo.INFO != 1 and tinfo LSB is set: invalid tinfo */
5709
5790
if (tinfo == 0 || tinfo & 0x1 )
5710
5791
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE ;
5711
5792
* trigger_tinfo = tinfo ;
5712
5793
return ERROR_OK ;
5713
5794
}
5795
+ * trigger_tinfo_version = ERROR_TARGET_RESOURCE_NOT_AVAILABLE ;
5714
5796
const unsigned int type = get_field (tdata1 , CSR_TDATA1_TYPE (riscv_xlen (target )));
5715
5797
if (type == 0 )
5716
5798
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE ;
@@ -5794,7 +5876,8 @@ int riscv_enumerate_triggers(struct target *target)
5794
5876
if (riscv_reg_get (target , & tdata1 , GDB_REGNO_TDATA1 ) != ERROR_OK )
5795
5877
return ERROR_FAIL ;
5796
5878
5797
- result = get_trigger_types (target , & r -> trigger_tinfo [t ], tdata1 );
5879
+ result = get_trigger_types_and_version (target , & r -> trigger_tinfo [t ],
5880
+ & r -> trigger_tinfo_version [t ], tdata1 );
5798
5881
if (result == ERROR_FAIL )
5799
5882
return ERROR_FAIL ;
5800
5883
if (result == ERROR_TARGET_RESOURCE_NOT_AVAILABLE )
0 commit comments