@@ -764,10 +764,10 @@ static void cache_set32(struct target *target, unsigned int index, uint32_t data
764764 if (info -> dram_cache [index ].valid &&
765765 info -> dram_cache [index ].data == data ) {
766766 // This is already preset on the target.
767- LOG_DEBUG ("cache[0x%x] = 0x%x (hit)" , index , data );
767+ LOG_DEBUG ("cache[0x%x] = 0x%08x: DASM(0x%x) (hit)" , index , data , data );
768768 return ;
769769 }
770- LOG_DEBUG ("cache[0x%x] = 0x%x " , index , data );
770+ LOG_DEBUG ("cache[0x%x] = 0x%08x: DASM(0x%x) " , index , data , data );
771771 info -> dram_cache [index ].data = data ;
772772 info -> dram_cache [index ].valid = true;
773773 info -> dram_cache [index ].dirty = true;
@@ -1033,6 +1033,7 @@ static int wait_for_state(struct target *target, enum target_state state)
10331033
10341034static int read_csr (struct target * target , uint64_t * value , uint32_t csr )
10351035{
1036+ riscv011_info_t * info = get_info (target );
10361037 cache_set32 (target , 0 , csrr (S0 , csr ));
10371038 cache_set_store (target , 1 , S0 , SLOT0 );
10381039 cache_set_jump (target , 2 );
@@ -1042,6 +1043,13 @@ static int read_csr(struct target *target, uint64_t *value, uint32_t csr)
10421043 * value = cache_get (target , SLOT0 );
10431044 LOG_DEBUG ("csr 0x%x = 0x%" PRIx64 , csr , * value );
10441045
1046+ uint32_t exception = cache_get32 (target , info -> dramsize - 1 );
1047+ if (exception ) {
1048+ LOG_ERROR ("Got exception 0x%x when reading CSR 0x%x" , exception , csr );
1049+ * value = ~0 ;
1050+ return ERROR_FAIL ;
1051+ }
1052+
10451053 return ERROR_OK ;
10461054}
10471055
@@ -1496,6 +1504,104 @@ static void deinit_target(struct target *target)
14961504 info -> version_specific = NULL ;
14971505}
14981506
1507+ static int maybe_add_trigger_t1 (struct target * target , struct trigger * trigger ,
1508+ uint64_t tdata1 )
1509+ {
1510+ riscv011_info_t * info = get_info (target );
1511+
1512+ const uint32_t bpcontrol_x = 1 <<0 ;
1513+ const uint32_t bpcontrol_w = 1 <<1 ;
1514+ const uint32_t bpcontrol_r = 1 <<2 ;
1515+ const uint32_t bpcontrol_u = 1 <<3 ;
1516+ const uint32_t bpcontrol_s = 1 <<4 ;
1517+ const uint32_t bpcontrol_h = 1 <<5 ;
1518+ const uint32_t bpcontrol_m = 1 <<6 ;
1519+ const uint32_t bpcontrol_bpmatch = 0xf << 7 ;
1520+ const uint32_t bpcontrol_bpaction = 0xff << 11 ;
1521+
1522+ if (tdata1 & (bpcontrol_r | bpcontrol_w | bpcontrol_x )) {
1523+ // Trigger is already in use, presumably by user code.
1524+ return ERROR_TARGET_RESOURCE_NOT_AVAILABLE ;
1525+ }
1526+
1527+ tdata1 = set_field (tdata1 , bpcontrol_r , trigger -> read );
1528+ tdata1 = set_field (tdata1 , bpcontrol_w , trigger -> write );
1529+ tdata1 = set_field (tdata1 , bpcontrol_x , trigger -> execute );
1530+ tdata1 = set_field (tdata1 , bpcontrol_u , !!(info -> misa & (1 << ('U' - 'A' ))));
1531+ tdata1 = set_field (tdata1 , bpcontrol_s , !!(info -> misa & (1 << ('S' - 'A' ))));
1532+ tdata1 = set_field (tdata1 , bpcontrol_h , !!(info -> misa & (1 << ('H' - 'A' ))));
1533+ tdata1 |= bpcontrol_m ;
1534+ tdata1 = set_field (tdata1 , bpcontrol_bpmatch , 0 ); // exact match
1535+ tdata1 = set_field (tdata1 , bpcontrol_bpaction , 0 ); // cause bp exception
1536+
1537+ write_csr (target , CSR_TDATA1 , tdata1 );
1538+
1539+ uint64_t tdata1_rb ;
1540+ read_csr (target , & tdata1_rb , CSR_TDATA1 );
1541+ LOG_DEBUG ("tdata1=0x%" PRIx64 , tdata1_rb );
1542+
1543+ if (tdata1 != tdata1_rb ) {
1544+ LOG_DEBUG ("Trigger doesn't support what we need; After writing 0x%"
1545+ PRIx64 " to tdata1 it contains 0x%" PRIx64 ,
1546+ tdata1 , tdata1_rb );
1547+ write_csr (target , CSR_TDATA1 , 0 );
1548+ return ERROR_TARGET_RESOURCE_NOT_AVAILABLE ;
1549+ }
1550+
1551+ write_csr (target , CSR_TDATA2 , trigger -> address );
1552+
1553+ return ERROR_OK ;
1554+ }
1555+
1556+ static int maybe_add_trigger_t2 (struct target * target , struct trigger * trigger ,
1557+ uint64_t tdata1 )
1558+ {
1559+ riscv011_info_t * info = get_info (target );
1560+ // tselect is already set
1561+ if (tdata1 & (MCONTROL_EXECUTE | MCONTROL_STORE | MCONTROL_LOAD )) {
1562+ // Trigger is already in use, presumably by user code.
1563+ return ERROR_TARGET_RESOURCE_NOT_AVAILABLE ;
1564+ }
1565+
1566+ // address/data match trigger
1567+ tdata1 |= MCONTROL_DMODE (riscv_xlen (target ));
1568+ tdata1 = set_field (tdata1 , MCONTROL_ACTION ,
1569+ MCONTROL_ACTION_DEBUG_MODE );
1570+ tdata1 = set_field (tdata1 , MCONTROL_MATCH , MCONTROL_MATCH_EQUAL );
1571+ tdata1 |= MCONTROL_M ;
1572+ if (info -> misa & (1 << ('H' - 'A' )))
1573+ tdata1 |= MCONTROL_H ;
1574+ if (info -> misa & (1 << ('S' - 'A' )))
1575+ tdata1 |= MCONTROL_S ;
1576+ if (info -> misa & (1 << ('U' - 'A' )))
1577+ tdata1 |= MCONTROL_U ;
1578+
1579+ if (trigger -> execute )
1580+ tdata1 |= MCONTROL_EXECUTE ;
1581+ if (trigger -> read )
1582+ tdata1 |= MCONTROL_LOAD ;
1583+ if (trigger -> write )
1584+ tdata1 |= MCONTROL_STORE ;
1585+
1586+ write_csr (target , CSR_TDATA1 , tdata1 );
1587+
1588+ uint64_t tdata1_rb ;
1589+ read_csr (target , & tdata1_rb , CSR_TDATA1 );
1590+ LOG_DEBUG ("tdata1=0x%" PRIx64 , tdata1_rb );
1591+
1592+ if (tdata1 != tdata1_rb ) {
1593+ LOG_DEBUG ("Trigger doesn't support what we need; After writing 0x%"
1594+ PRIx64 " to tdata1 it contains 0x%" PRIx64 ,
1595+ tdata1 , tdata1_rb );
1596+ write_csr (target , CSR_TDATA1 , 0 );
1597+ return ERROR_TARGET_RESOURCE_NOT_AVAILABLE ;
1598+ }
1599+
1600+ write_csr (target , CSR_TDATA2 , trigger -> address );
1601+
1602+ return ERROR_OK ;
1603+ }
1604+
14991605static int add_trigger (struct target * target , struct trigger * trigger )
15001606{
15011607 riscv011_info_t * info = get_info (target );
@@ -1514,51 +1620,23 @@ static int add_trigger(struct target *target, struct trigger *trigger)
15141620 read_csr (target , & tdata1 , CSR_TDATA1 );
15151621 int type = get_field (tdata1 , MCONTROL_TYPE (riscv_xlen (target )));
15161622
1517- if (type != 2 ) {
1518- continue ;
1519- }
1520-
1521- if (tdata1 & (MCONTROL_EXECUTE | MCONTROL_STORE | MCONTROL_LOAD )) {
1522- // Trigger is already in use, presumably by user code.
1523- continue ;
1623+ int result ;
1624+ switch (type ) {
1625+ case 1 :
1626+ result = maybe_add_trigger_t1 (target , trigger , tdata1 );
1627+ break ;
1628+ case 2 :
1629+ result = maybe_add_trigger_t2 (target , trigger , tdata1 );
1630+ break ;
1631+ default :
1632+ LOG_DEBUG ("trigger %d has unknown type %d" , i , type );
1633+ continue ;
15241634 }
15251635
1526- // address/data match trigger
1527- tdata1 |= MCONTROL_DMODE (riscv_xlen (target ));
1528- tdata1 = set_field (tdata1 , MCONTROL_ACTION ,
1529- MCONTROL_ACTION_DEBUG_MODE );
1530- tdata1 = set_field (tdata1 , MCONTROL_MATCH , MCONTROL_MATCH_EQUAL );
1531- tdata1 |= MCONTROL_M ;
1532- if (info -> misa & (1 << ('H' - 'A' )))
1533- tdata1 |= MCONTROL_H ;
1534- if (info -> misa & (1 << ('S' - 'A' )))
1535- tdata1 |= MCONTROL_S ;
1536- if (info -> misa & (1 << ('U' - 'A' )))
1537- tdata1 |= MCONTROL_U ;
1538-
1539- if (trigger -> execute )
1540- tdata1 |= MCONTROL_EXECUTE ;
1541- if (trigger -> read )
1542- tdata1 |= MCONTROL_LOAD ;
1543- if (trigger -> write )
1544- tdata1 |= MCONTROL_STORE ;
1545-
1546- write_csr (target , CSR_TDATA1 , tdata1 );
1547-
1548- uint64_t tdata1_rb ;
1549- read_csr (target , & tdata1_rb , CSR_TDATA1 );
1550- LOG_DEBUG ("tdata1=0x%" PRIx64 , tdata1_rb );
1551-
1552- if (tdata1 != tdata1_rb ) {
1553- LOG_DEBUG ("Trigger %d doesn't support what we need; After writing 0x%"
1554- PRIx64 " to tdata1 it contains 0x%" PRIx64 ,
1555- i , tdata1 , tdata1_rb );
1556- write_csr (target , CSR_TDATA1 , 0 );
1636+ if (result != ERROR_OK ) {
15571637 continue ;
15581638 }
15591639
1560- write_csr (target , CSR_TDATA2 , trigger -> address );
1561-
15621640 LOG_DEBUG ("Using resource %d for bp %d" , i ,
15631641 trigger -> unique_id );
15641642 info -> trigger_unique_id [i ] = trigger -> unique_id ;
@@ -1898,8 +1976,13 @@ static int examine(struct target *target)
18981976 update_reg_list (target );
18991977
19001978 if (read_csr (target , & info -> misa , CSR_MISA ) != ERROR_OK ) {
1901- LOG_ERROR ("Failed to read misa." );
1902- return ERROR_FAIL ;
1979+ LOG_WARNING ("Failed to read misa at 0x%x." , CSR_MISA );
1980+ if (read_csr (target , & info -> misa , 0xf10 ) != ERROR_OK ) {
1981+ // Maybe this is an old core that still has $misa at the old
1982+ // address.
1983+ LOG_ERROR ("Failed to read misa at 0x%x." , 0xf10 );
1984+ return ERROR_FAIL ;
1985+ }
19031986 }
19041987
19051988 info -> never_halted = true;
@@ -2129,6 +2212,9 @@ static int handle_halt(struct target *target, bool announce)
21292212 write_csr (target , CSR_TSELECT , info -> trigger_count );
21302213 uint64_t tselect_rb ;
21312214 read_csr (target , & tselect_rb , CSR_TSELECT );
2215+ // Mask off the top bit, which is used as tdrmode in old
2216+ // implementations.
2217+ tselect_rb &= ~(1ULL << (riscv_xlen (target )- 1 ));
21322218 if (info -> trigger_count != tselect_rb )
21332219 break ;
21342220 uint64_t tdata1 ;
0 commit comments